假设我有两个圆圈,其中(x 1 ,y 1 ,r 1 )和 (x 2 ,y 2 ,r 2 )分别为其坐标和半径。这样,它们之间的最大和最小距离是多少。到目前为止,我所做的事情:
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坐标的数组。这给了我错误的答案。
答案 0 :(得分:1)
else路径错误。
此:
double maxDistance = rmax + (centreDistance-rmin);
应为:
double maxDistance = rmax + centreDistance + rmin;
您还缺少圆圈相交的情况。
centreDistance < r1+r2 && centredistance + rmin >= rmax
在这种情况下
minDistance = 0
maxDistance = rmax + centreDistance + rmin
centreDistance == 0也是错误的,它应该是maxDistance = r1 + r2
(因为您应该比较中心相对侧的点。无论如何,这只是else路径的特例,可以被忽略。
实际情况是:
centreDistance>=r[i]+r[j] (centres are so far apart the circles don't overlap, or touch in one point)
centreDistance < r1+r2 && centredistance + rmin >= rmax (circles intersect)
centreDistance < r1+r2 && centredistance + rmin < rmax (one circle inside the other)
答案 1 :(得分:0)
在圆圈接触的情况下,您必须进行一些调整
if (centreDistance >= r[i] + r[j]) {
// the circle dont touch, min and max distance are the closest and most far apart parts of the circles
// code is correct
double maxDistance = centreDistance + (r[i] + r[j]);
double minDistance = centreDistance - (r[i] + r[j]);
} else if (centreDistance == 0) {
// the circle are on top of each other
// check if they have the same radius -> max min dist = 0
// if not max and min distance = Math.abs(r[i] - r[j]);
// code is correct
double minDistance = Math.abs(r[i] - r[j]);
double maxDistance = Math.abs(r[i] - r[j]);
} else {
// the circles touch
// max distance is the same as in case where they dont touch
// and min distance is 0 because they touch
// code has to be adjusted
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);
}
答案 2 :(得分:0)
问题是Array
(
[0] => 1428
[1] => 1428
)
Array
(
[0] => 217
)
和rmax
是整数。返回max和min的结果时,它会四舍五入为rmin
值。