Advertisement
steverobinson

Distance between two points

Oct 12th, 2010
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. /* Distance Between Two Points*/
  2. /*       Language: C++        */
  3.  
  4.  
  5.  
  6. #include <iostream.h>
  7. #include <conio.h>
  8. #include <math.h>
  9.  
  10. class coordinates
  11. {
  12.     float a,b;
  13.  
  14.     public:
  15.         coordinates(float x,float y)
  16.         {
  17.             a=x;
  18.             b=y;
  19.         }
  20.         friend float distance(coordinates &point1,coordinates &point2);
  21. };
  22.  
  23.  
  24. float distance(coordinates &point1,coordinates &point2)
  25. {
  26.     float distance;
  27.     float temp;
  28.     temp=((point1.a-point2.a)*(point1.a-point2.a))+((point1.b-point2.b)*(point1.b-point2.b));
  29.     distance=sqrt(temp);
  30.     return distance;
  31. }
  32.  
  33.  
  34. int main()
  35. {
  36.     system("cls");
  37.     float a1,b1;
  38.     float a2,b2;
  39.     cout<<"\nEnter Coordinates of Point 1: \n";
  40.     cin>>a1>>b1;
  41.     cout<<"\nEnter Coordinates of Point 2: \n";
  42.     cin>>a2>>b2;
  43.     coordinates point1(a1,b1);
  44.     coordinates point2(a2,b2);
  45.     float distance_result=distance(point1,point2);
  46.     cout<<"\n\nDistance between the two given points is: "<<distance_result;
  47.     getch();
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement