[英]A JavaScript function that returns the x,y points of intersection between two circles?
I got the (x,y) center location of two circles and their radius but I need to find their intersection points (marked with red) using JavaScript. 我得到了两个圆的(x,y)中心位置及其半径,但我需要使用JavaScript找到它们的交点(用红色标记)。
I think the best explanation as far as the math is concerned is found here (Intersection of two circles), but I don't really understand the math so I'm not able to implement it. 我认为就数学而言最好的解释是在这里找到的(两个圆圈的交点),但我并不真正理解数学,所以我无法实现它。
For example d = ||P1 - P0|| 例如,d = || P1 - P0 || , what do the ||
什么做了|| stand for?
代表? Does it mean that the resulting number is always a positive?
这是否意味着最终的数字总是积极的?
And also P2 = P0 + a ( P1 - P0 ) / d , aren't the P's here something like (10, 50)? 并且P2 = P0 + a(P1-P0)/ d,这里的P不是(10,50)? But doing (10,50)+13 in JavaScript gives you 63, so it just ignores the first number, so what's suppose to happen?
但是在JavaScript中执行(10,50)+13会给你63,所以它只是忽略了第一个数字,那么假设会发生什么? Should the outcome be (23,63) here or?
结果应该是(23,63)这里还是? And also the P1-P0 part or (40,30)-(10,60), how do you express that in JavaScript?
还有P1-P0部分或(40,30) - (10,60),您如何在JavaScript中表达?
Translated the C function on the site to 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];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.