fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main() {
  5. int x1, y1, x2, y2;
  6. int dx, dy, p, x, y;
  7.  
  8. printf("Enter x1 y1: ");
  9. scanf("%d %d", &x1, &y1);
  10.  
  11. printf("Enter x2 y2: ");
  12. scanf("%d %d", &x2, &y2);
  13.  
  14. dx = abs(x2 - x1);
  15. dy = abs(y2 - y1);
  16.  
  17. x = x1;
  18. y = y1;
  19.  
  20. // Assume slope < 1 (simple version)
  21. p = 2 * dy - dx;
  22.  
  23. printf("\nPoints on the line:\n");
  24.  
  25. while (x <= x2) {
  26. printf("(%d, %d)\n", x, y);
  27.  
  28. x++;
  29.  
  30. if (p < 0) {
  31. p = p + 2 * dy;
  32. } else {
  33. y++;
  34. p = p + 2 * (dy - dx);
  35. }
  36. }
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
Enter x1 y1: Enter x2 y2: 
Points on the line: