定义一个记录平面点坐标的结构体,编写一个函数求两点距离。用C++做,谢谢了!!!

2025-01-07 00:41:15
推荐回答(2个)
回答1:

#include
#include
using namespace std;
typedef struct Point
{
double x;
double y;
}Point;
double Distance(Point p1, Point p2)
{
return sqrt(pow((p1.x - p2.x), 2) + pow((p1.y - p2.y), 2));
}
int main()
{
Point p1, p2;
cin >> p1.x >> p1.y >> p2.x >> p2.y;
cout << "distance: " << Distance(p1, p2) << endl;
return 0;
}

回答2:

struct point
{
float x;
float y;
};
float getDistance(point p1,point p2)
{
return sqrt ( (p2.y-p1.y)*(p2.y-p1.y) + (p2.x-p1.x)*(p2.x-p1.x) ) ;
}