【发布时间】:2018-10-16 15:17:11
【问题描述】:
假设我有两个圆圈 (x1,y1,r1) 和 (x2,y2,r2) 分别作为它们的坐标和半径。有了这个,它们之间的最大和最小距离是多少。到目前为止我做了什么:
double centreDistance = Math.sqrt(Math.pow((x[i] - x[j]), 2) + Math.pow((x[i] - y[j]), 2));
if (centreDistance >= r[i] + r[j]) {
double maxDistance = centreDistance + (r[i] + r[j]);
double minDistance = centreDistance - (r[i] + r[j]);
} else if (centreDistance == 0) {
double minDistance = Math.abs(r[i] - r[j]);
double maxDistance = Math.abs(r[i] - r[j]);
} else {
int rmax = Math.max(r[i], r[j]);
int rmin = Math.min(r[i], r[j]);
double minDistance = rmax - (centreDistance + rmin);
double maxDistance = rmax + (centreDistance - rmin);
}
这里的 x,y,r 是分别具有每个圆心和半径的 x,y 坐标的数组。这给了我错误的答案。
【问题讨论】:
-
最小距离为零,因为它们可以在同一个位置,最大距离是……基本上无法测量
-
@Stultuske 我认为 OP 的意思是给定圆的坐标,圆上点之间的最小/最大距离是多少。
-
应该
x[i]-y[j]不是y[i]-y[j]? -
我想要一对给定圆的最小和最大距离。对于不相交的圆,最小距离是它们的中心之间的距离减去它们的半径之和,最大距离是它们的中心之间的距离加上它们的半径之和。我希望我的代码能够处理所有可能性:不相交的圆、同心圆、内圆等。
-
哦该死@TiiJ7 ...太愚蠢了,没有注意到那个...我会再次运行它,看看它是否正确