可以使用以下公式计算两点之间的距离:

distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))

其中,(x1, y1) 和 (x2, y2) 分别是两个点的坐标,sqrt() 函数是求平方根的函数。

以下是一个计算两点距离的 C++ 代码示例:

#include <iostream>
#include <cmath>

using namespace std;

int main() {
    double x1, y1, x2, y2, distance;
    cout << 'Enter the coordinates of the first point: ';
    cin >> x1 >> y1;
    cout << 'Enter the coordinates of the second point: ';
    cin >> x2 >> y2;
    distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
    cout << 'The distance between the two points is: ' << distance << endl;
    return 0;
}
C++ 计算两点距离:公式及代码示例

原文地址: https://www.cveoy.top/t/topic/kUNj 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录