【问题标题】:Find intersecting point of three circles programmatically [closed]以编程方式查找三个圆的交点[关闭]
【发布时间】:2013-11-01 09:00:02
【问题描述】:

正如标题所说, 我有 3 个圈子

每一个都有不同的半径。我知道每个圆的半径。

还知道每个圆的中心点

现在我需要知道如何以编程方式计算三个圆的交点,有什么公式吗?

可能如下图所示

【问题讨论】:

  • 这根本不是一个编程问题,只要搜索 stackoverflow 或 google 就能得到很多好的结果。
  • @Alex Chengalan 你想要插入点的坐标吗?
  • 我已经找到了正确的解决方案...检查这个sg.answers.yahoo.com/question/index?qid=20110127015240AA9RjyZ 但现在重要的是如何在 JAVA 中做到这一点。@Alex Chengalan
  • 这个问题似乎离题了,因为它与编程无关
  • @RichardTingle:信不信由你,编程不仅仅是破解代码。它涉及解决问题,有时还涉及您必须实施的数学。这道题显然是一道编程题,用来实现一道高难度的数学题,对于我们这些有数学技能并在这方面工作的人来说,非常感谢。

标签: java android formula


【解决方案1】:

您可以从这个C code 获得帮助。将其移植到 Java 应该不难。解释是here。搜索/滚动到:两个圆的交集

使用这个方法,找到任意两个圆的交点,比如(x,y)。现在第三个圆只有在其center 和点x,y 之间的距离等于r 时才会在x,y 处相交。

  1. 如果distance(center,point) == r,那么x,y就是交点。

  2. 如果distance(center,point) != r,则不存在该点。

代码(来自here;所有功劳归原作者所有):

private boolean calculateThreeCircleIntersection(double x0, double y0, double r0,
                                                 double x1, double y1, double r1,
                                                 double x2, double y2, double r2)
{
    double a, dx, dy, d, h, rx, ry;
    double point2_x, point2_y;

    /* dx and dy are the vertical and horizontal distances between
    * the circle centers.
    */
    dx = x1 - x0;
    dy = y1 - y0;

    /* Determine the straight-line distance between the centers. */
    d = Math.sqrt((dy*dy) + (dx*dx));

    /* Check for solvability. */
    if (d > (r0 + r1))
    {
        /* no solution. circles do not intersect. */
        return false;
    }
    if (d < Math.abs(r0 - r1))
    {
        /* no solution. one circle is contained in the other */
        return false;
    }

    /* 'point 2' is the point where the line through the circle
    * intersection points crosses the line between the circle
    * centers.
    */

    /* Determine the distance from point 0 to point 2. */
    a = ((r0*r0) - (r1*r1) + (d*d)) / (2.0 * d) ;

    /* Determine the coordinates of point 2. */
    point2_x = x0 + (dx * a/d);
    point2_y = y0 + (dy * a/d);

    /* Determine the distance from point 2 to either of the
    * intersection points.
    */
    h = Math.sqrt((r0*r0) - (a*a));

    /* Now determine the offsets of the intersection points from
    * point 2.
    */
    rx = -dy * (h/d);
    ry = dx * (h/d);

    /* Determine the absolute intersection points. */
    double intersectionPoint1_x = point2_x + rx;
    double intersectionPoint2_x = point2_x - rx;
    double intersectionPoint1_y = point2_y + ry;
    double intersectionPoint2_y = point2_y - ry;

    Log.d("INTERSECTION Circle1 AND Circle2:", "(" + intersectionPoint1_x + "," + intersectionPoint1_y + ")" + " AND (" + intersectionPoint2_x + "," + intersectionPoint2_y + ")");

    /* Lets determine if circle 3 intersects at either of the above intersection points. */
    dx = intersectionPoint1_x - x2;
    dy = intersectionPoint1_y - y2;
    double d1 = Math.sqrt((dy*dy) + (dx*dx));

    dx = intersectionPoint2_x - x2;
    dy = intersectionPoint2_y - y2;
    double d2 = Math.sqrt((dy*dy) + (dx*dx));

    if(Math.abs(d1 - r2) < EPSILON) {
        Log.d("INTERSECTION Circle1 AND Circle2 AND Circle3:", "(" + intersectionPoint1_x + "," + intersectionPoint1_y + ")");
    }
    else if(Math.abs(d2 - r2) < EPSILON) {
        Log.d("INTERSECTION Circle1 AND Circle2 AND Circle3:", "(" + intersectionPoint2_x + "," + intersectionPoint2_y + ")"); //here was an error
    }
    else {
        Log.d("INTERSECTION Circle1 AND Circle2 AND Circle3:", "NONE");
    }
    return true;
}

调用该方法如下:

calculateThreeCircleIntersection(-2.0, 0.0, 2.0, // circle 1 (center_x, center_y, radius)
                                  1.0, 0.0, 1.0, // circle 2 (center_x, center_y, radius)
                                  0.0, 4.0, 4.0);// circle 3 (center_x, center_y, radius)

另外,将EPSILON 定义为您的应用程序要求可接受的小值

private static final double EPSILON = 0.000001;

注意:也许有人应该测试并验证结果是否正确。我找不到任何简单的方法来做到这一点..适用于我尝试过的基本案例。

【讨论】:

  • 谢谢你的努力兄弟!我会在这里检查并发表评论。
  • 完美运行!谢谢!
  • 我将以上内容翻译成 Python,但使用您提供的数字,我得到:$ INFO:log:INTERSECTION Circle1 AND Circle2: (0.0,0.0) AND (0.0,0.0) $ INFO:log:INTERSECTION Circle1 AND Circle2 AND Circle3: (0.0,0.0)。考虑到您的函数的上述输入,我应该期待什么输出?
【解决方案2】:

您可以使用以下条件:

(x - x0) ^ 2 + (y - y0) ^ 2 <= R ^ 2

其中 x 和 y - 点的坐标,x0 和 y0 - 圆心的坐标,R - 圆的半径,^2 - 平方。如果满足条件,则该点在内部(或在左右相等的情况下在圆周上)。如果不满足,则该点在圆外。

/ / Point, which hit the circle is necessary to determine
PointF p = ...;

/ / Center of the circle
PointF center = new PointF (10, 10);

/ / The radius of the circle
float r = 5F;

/ / "Normalize" the situation relative to the center point of the circle
float dx = p.x - center.x;
float dy = p.y - center.y;

/ / Compare the distance from the point to the center of a circle to its radius
boolean result =  ((r * r) <= (dx * dx + dy * dy))) ? true : false;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-22
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    相关资源
    最近更新 更多