fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void bresenhamLine(int x1, int y1, int x2, int y2) {
  5. int dx = x2 - x1;
  6. int dy = y2 - y1;
  7. int d = 2 * dy - dx; // Initial decision parameter
  8. int x = x1;
  9. int y = y1;
  10.  
  11. // Plot initial point
  12. printf("Point: (%d, %d)\n", x, y);
  13.  
  14. while (x < x2) {
  15. x++; // Move east
  16.  
  17. if (d < 0) {
  18. // Choose East pixel
  19. d = d + 2 * dy;
  20. } else {
  21. // Choose Northeast pixel
  22. y++;
  23. d = d + 2 * (dy - dx);
  24. }
  25.  
  26. printf("Point: (%d, %d)\n", x, y);
  27. }
  28. }
  29.  
  30. int main() {
  31. int x1, y1, x2, y2;
  32.  
  33. // Get input coordinates
  34. printf("Enter starting point (x1 y1): ");
  35. scanf("%d %d", &x1, &y1);
  36.  
  37. printf("Enter ending point (x2 y2): ");
  38. scanf("%d %d", &x2, &y2);
  39.  
  40. // Draw the line
  41. printf("\nPlotting line from (%d,%d) to (%d,%d)\n", x1, y1, x2, y2);
  42. bresenhamLine(x1, y1, x2, y2);
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0s 5296KB
stdin
Standard input is empty
stdout
Enter starting point (x1 y1): Enter ending point (x2 y2): 
Plotting line from (1870463280,21947) to (511920176,32764)
Point: (1870463280, 21947)