Advertisement
dmilicev

distance between two points and the midpoint v1.c

Oct 15th, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.65 KB | None | 0 0
  1. /*
  2.     Distance between two points and the midpoint v1.c
  3.  
  4. The distance formula is an algebraic expression used to determine
  5. the distance between two points with the coordinates
  6. A(x1, y1) and B(x2, y2).
  7.  
  8. distance = sqrt( (x2-x1)^2 +(y2-y1)^2 )
  9.  
  10. If the end points of a line segment are A(x1, y1) and B(x2, y2)
  11. then the midpoint M(xm,ym) of the line segment AB has the coordinates:
  12.  
  13. xm = (x1+x2)/2
  14.  
  15. ym = (y1+y2)/2
  16.  
  17. M(xm,ym) = M( (x1+x2)/2 , (y1+y2)/2 )
  18.  
  19.  
  20.     o-----------------o-----------------o
  21.     A                 M                 B
  22.  
  23.  
  24. */
  25.  
  26. #include<stdio.h>
  27. #include <math.h>   // because of sqrt()
  28.  
  29. int main(void)
  30. {
  31. /*
  32.     A(x1,y1)    point A is at the beginning of line segment AB
  33.     B(x2,y2)    point B is at the end of line segment AB
  34.     M(xm,ym)    point M is in the middle between A and B
  35.     distance    is the distance between points A and B
  36. */
  37.     double x1, y1, x2, y2, xm, ym, distance;
  38.  
  39.     printf("\n A and B are two points with the coordinates A(x1, y1) and B(x2, y2) \n");
  40.  
  41.     printf("\n Enter coordinate x1 = ");
  42.     scanf("%lf",&x1);
  43.  
  44.     printf("\n Enter coordinate y1 = ");
  45.     scanf("%lf",&y1);
  46.  
  47.     printf("\n Enter coordinate x2 = ");
  48.     scanf("%lf",&x2);
  49.  
  50.     printf("\n Enter coordinate y2 = ");
  51.     scanf("%lf",&y2);
  52.  
  53.     distance = sqrt( pow( (x2-x1),2 ) + pow( (y2-y1),2) );
  54.  
  55.     xm = (x1+x2) / 2;
  56.  
  57.     ym = (y1+y2) / 2;
  58.  
  59.  
  60.     printf("\n A( %.2lf , %.2lf ) \t B( %.2lf , %.2lf ) \n", x1, y1, x2, y2 );
  61.  
  62.     printf("\n Distance between points A and B = sqrt( (x2-x1)^2 +(y2-y1)^2 ) = %.2lf \n", distance);
  63.  
  64.     printf("\n In the middle between A and B is point M( (x1+x2)/2 , (y1+y2)/2 ) \n");
  65.  
  66.     printf("\n M( %.2lf , %.2lf ) \n", xm, ym);
  67.  
  68.  
  69.     printf("\n");
  70.  
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement