的JavaScript函数返回X,两个圆之间的交叉点ÿ?(A JavaScript function

2019-06-17 23:27发布

我得到了(X,Y)中心的两个圆及其半径的位置,但我需要找到使用JavaScript的交叉点(用红色标出)。

我认为最好的解释,就数学而言被发现这里 (两个圆的交点),但我真的不明白的数学,所以我不能够实现它。

例如d = || P1 - P0 || 什么做|| 代表? 这是否意味着得到的数字始终是一个积极的?

而且还P2 = P0 + A(P1 - P0)/ d,不是在P东西在这里像(10,50)? 但这样做(10,50)在JavaScript +13给你63,所以它只是忽略了第一号,那么我们假设发生什么呢? 如果结果为(23,63),这里还是? 同时,也是P1-P0部分或(40,30) - (10,60),你如何表达在JavaScript?

Answer 1:

翻译网站上的JavaScript C函数:

function intersection(x0, y0, r0, x1, y1, r1) {
        var a, dx, dy, d, h, rx, ry;
        var x2, y2;

        /* 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. */
        x2 = x0 + (dx * a/d);
        y2 = 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. */
        var xi = x2 + rx;
        var xi_prime = x2 - rx;
        var yi = y2 + ry;
        var yi_prime = y2 - ry;

        return [xi, xi_prime, yi, yi_prime];
    }


文章来源: A JavaScript function that returns the x,y points of intersection between two circles?