fork download
  1. /*
  2.   Given two coordinates, Print the line equation.
  3.   Objective ->
  4.   Given two coordinates (x1,y1) and (x2,y2), write a C program to construct a line equation that
  5.   passes through the given coordinates.
  6.   A(3,7) B(5,11)
  7.   A(2,2) B(4,12)_
  8.  */
  9. #include <stdio.h>
  10.  
  11. struct TPoint {
  12. float x,
  13. y;
  14. };
  15.  
  16. typedef struct TPoint Point;
  17.  
  18. void read(Point *P) {
  19.  
  20. printf("Abs -> ");
  21. scanf("%f", &P->x);
  22.  
  23. printf("Ord -> ");
  24. scanf("%f", &P->y);
  25. }
  26.  
  27. float computeSlope(Point A, Point B) {
  28.  
  29. return (B.y - A.y) / (B.x - A.x);
  30. }
  31.  
  32. void createEquation(Point A, Point B) {
  33.  
  34. float m, a, b;
  35.  
  36. m = computeSlope(A, B);
  37.  
  38. a = m;
  39. b = A.y - m * A.x;
  40.  
  41. if(m == 0) printf("Line Equation -> y = %.2f\n", A.y);
  42.  
  43. else {
  44. if(b > 0) printf("Line Equation -> y = %.2f * x + %.2f\n", a, b);
  45.  
  46. else
  47.  
  48. printf("Line Equation -> y = %.2f * x - %.2f\n", a, b*(-1));
  49. }
  50. };
  51.  
  52. int main() {
  53.  
  54. Point A, B;
  55.  
  56. read(&A);
  57. read(&B);
  58. createEquation(A, B);
  59.  
  60. return(0);
  61. }
Success #stdin #stdout 0s 4300KB
stdin
2
2
4
12
stdout
Abs -> Ord -> Abs -> Ord -> Line Equation  -> y = 5.00 * x - 8.00