【问题标题】:What is the maximum and minimum distance between two points on different circles?不同圆上两点之间的最大和最小距离是多少?
【发布时间】: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 ...太愚蠢了,没有注意到那个...我会再次运行它,看看它是否正确

标签: java math geometry


【解决方案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)

【讨论】:

  • 你是对的,除了我的第一个if 条件还涵盖了圆圈相互接触的情况。
  • @Ryosuke 你说得对,我会在最后一个列表中编辑那部分。
  • @Ryosuke Sooo,你会接受这个作为答案吗?
【解决方案2】:

在圆圈接触的情况下,你必须做一些调整

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);
    }

【讨论】:

    【解决方案3】:

    问题是rmaxrmin 是整数。当您返回 max 和 min 的结果时,它会四舍五入为 int 值。

    【讨论】:

      猜你喜欢
      • 2021-10-24
      • 2017-09-12
      • 1970-01-01
      • 2012-09-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多