TA的每日心情 | 怒 2021-1-31 01:02 |
---|
签到天数: 3 天 [LV.2]偶尔看看I
|
#include <iostream>
#include <cmath>
using namespace std;
const double PI = 3.14159265358979323846;
// 计算两点之间的距离
double distance(double x1, double y1, double x2, double y2) {
return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
}
int main() {
double x1, y1, x2, y2, r;
cout << "请输入起点坐标(x1, y1)、终点坐标(x2, y2)和半径r: ";
cin >> x1 >> y1 >> x2 >> y2 >> r;
double px, py; // 最接近的半径坐标
double minDist = distance(x2, y2, r, 0); // 初始最小距离为终点到x轴半径的距离
for (double angle = 0; angle <= 2 * PI; angle += 0.01) {
double cx = r * cos(angle);
double cy = r * sin(angle);
double dist = distance(x2, y2, cx, cy);
if (dist < minDist) {
minDist = dist;
px = cx;
py = cy;
}
}
cout << "最接近的半径坐标为(" << px << ", " << py << ")" << endl;
if (minDist <= r) {
cout << "终点坐标没有超过规定半径范围" << endl;
} else {
cout << "终点坐标已超过规定半径范围,已赋值成最接近的半径坐标" << endl;
x2 = x1 + px; // 以起点为中心,最接近半径坐标为终点的相对坐标
y2 = y1 + py;
}
cout << "当前终点坐标为(" << x2 << ", " << y2 << ")" << endl;
return 0;
}
请输入起点坐标(x1, y1)、终点坐标(x2, y2)和半径r: 0 0 6 7 5
最接近的半径坐标为(-4.9999, 3.0001)
终点坐标已超过规定半径范围,已赋值成最接近的半径坐标
当前终点坐标为(-4.9999, 3.0001)
|
|