我得到了两个圆的 (x,y) 中心位置及其半径,但我需要使用 JavaScript 找到它们的交点(用红色标记)。
我认为就数学而言最好的解释已经找到here http://paulbourke.net/geometry/circlesphere/(两个圆的交点),但我不太理解数学,所以我无法实现它。
例如 d = ||P1 - P0|| ,|| 做什么?代表?这是否意味着结果总是正数?
还有 P2 = P0 + a ( P1 - P0 ) / d ,这里的 P 不是类似于 (10, 50) 吗?但是在 JavaScript 中执行 (10,50)+13 会得到 63,因此它只是忽略第一个数字,那么会发生什么呢?结果应该是 (23,63) 还是?还有 P1-P0 部分或 (40,30)-(10,60),你如何在 JavaScript 中表达它?
将网站上的 C 函数翻译为 JavaScript:
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];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)