java - 以编程方式查找三个圆的交点

标签 java android formula

<分区>

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

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

还知道每个圆的中心点

现在我需要知道如何以编程方式计算三个圆的交点,是否有任何公式或其他东西?

它可能看起来像下图 enter image description here

最佳答案

你可以从这个 C code 得到帮助.将它移植到 Java 应该不会有挑战性。解释是here .搜索/滚动到:两个圆的交点

使用此方法,找到任意两个圆的交点,比方说 (x,y)。现在第三个圆将相交于点 x,y 只有当它的 center 和点 x,y 之间的距离等于 r

  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;

注意:也许有人应该测试和验证结果是否正确。我找不到任何简单的方法...适用于我尝试过的基本情况。

关于java - 以编程方式查找三个圆的交点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19723641/

相关文章:

null - Maximo 公式可以返回 null 吗?

java - SSLHandshakeException : Certificate Unknown (Java Spring Boot & Android)

android - 如何通过应用程序获取内存使用率和cpu使用率?

android - 如何更改相机预览回调缓冲区的方向?

arrays - 使用大型 Excel 的数组公式

excel - 查找单个项目总计的公式 Excel

java - 在 google app engine 中出现此错误 405 此 URL 不支持 HTTP 方法 GET

Java Swing - JFreeChart 应用运行缓慢

java - Try Catch 不断循环而不是请求另一个值?

android - 如何将自定义 header 添加到 retrofit 请求正文部分?