fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void plotLine(int x1, int y1, int x2, int y2) {
  5. // Calculate the differences
  6. int dx = abs(x2 - x1);
  7. int dy = abs(y2 - y1);
  8.  
  9. // Determine the direction of the line
  10. int sx = (x1 < x2) ? 1 : -1;
  11. int sy = (y1 < y2) ? 1 : -1;
  12.  
  13. // Decision variable
  14. int err = dx - dy;
  15.  
  16. while (1) {
  17. // Plot the current pixel (replace this with actual pixel plot in a graphics environment)
  18. printf("Plotting pixel at (%d, %d)\n", x1, y1);
  19.  
  20. // Check if the end point is reached
  21. if (x1 == x2 && y1 == y2) break;
  22.  
  23. // Calculate the error value for the next pixel
  24. int e2 = 2 * err;
  25.  
  26. if (e2 > -dy) {
  27. err -= dy;
  28. x1 += sx;
  29. }
  30.  
  31. if (e2 < dx) {
  32. err += dx;
  33. y1 += sy;
  34. }
  35. }
  36. }
  37.  
  38. int main() {
  39. int x1 = 10, y1 = 10, x2 = 50, y2 = 30;
  40. printf("Drawing line from (%d, %d) to (%d, %d)\n", x1, y1, x2, y2);
  41. plotLine(x1, y1, x2, y2);
  42. return 0;
  43. }
  44.  
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
Drawing line from (10, 10) to (50, 30)
Plotting pixel at (10, 10)
Plotting pixel at (11, 10)
Plotting pixel at (12, 11)
Plotting pixel at (13, 11)
Plotting pixel at (14, 12)
Plotting pixel at (15, 12)
Plotting pixel at (16, 13)
Plotting pixel at (17, 13)
Plotting pixel at (18, 14)
Plotting pixel at (19, 14)
Plotting pixel at (20, 15)
Plotting pixel at (21, 15)
Plotting pixel at (22, 16)
Plotting pixel at (23, 16)
Plotting pixel at (24, 17)
Plotting pixel at (25, 17)
Plotting pixel at (26, 18)
Plotting pixel at (27, 18)
Plotting pixel at (28, 19)
Plotting pixel at (29, 19)
Plotting pixel at (30, 20)
Plotting pixel at (31, 20)
Plotting pixel at (32, 21)
Plotting pixel at (33, 21)
Plotting pixel at (34, 22)
Plotting pixel at (35, 22)
Plotting pixel at (36, 23)
Plotting pixel at (37, 23)
Plotting pixel at (38, 24)
Plotting pixel at (39, 24)
Plotting pixel at (40, 25)
Plotting pixel at (41, 25)
Plotting pixel at (42, 26)
Plotting pixel at (43, 26)
Plotting pixel at (44, 27)
Plotting pixel at (45, 27)
Plotting pixel at (46, 28)
Plotting pixel at (47, 28)
Plotting pixel at (48, 29)
Plotting pixel at (49, 29)
Plotting pixel at (50, 30)