Javascript 在圆中没有多边形点的情况下,如何求圆和多边形的交点

Javascript 在圆中没有多边形点的情况下,如何求圆和多边形的交点,javascript,google-maps,Javascript,Google Maps,假设我有一个多边形和一个google drawingmanager(google地图)的圆。场景是,圆中没有多边形点,但仍然相交。在这种情况下,我如何检查十字路口![圆和多边形相交,且圆内没有多边形点][1]检查多边形的每个点是否存在这种情况: dx^2+dy^2 < r^2 where dx = px[i] - cx, dy = py[i] - cy, px[i], py[i] - i-point of polygon, cx,cy - circle center, r - radiu

假设我有一个多边形和一个google drawingmanager(google地图)的圆。场景是,圆中没有多边形点,但仍然相交。在这种情况下,我如何检查十字路口![圆和多边形相交,且圆内没有多边形点][1]

检查多边形的每个点是否存在这种情况:

dx^2+dy^2 < r^2

where dx = px[i] - cx, dy = py[i] - cy,
px[i], py[i] - i-point of polygon, cx,cy - circle center, r - radius
是的,可能需要对t0a和t0b进行排序,但不能保证t0a
您需要对每条多边形线运行此检查。

True!如果多边形中至少有一个点在圆内。但圆内没有多边形点。可以说是多边形线与圆相交。我们如何识别我看到的,所以我更新了答案。这会有点困难,但你们需要通过解二次方程来检查多边形的每一条线和圆。谢谢你们的时间。我需要问的是,你真的认为这些方程可以应用到谷歌drawingmanager吗?在那里,我们用了lat-long代替了x和y,是的,地球肯定不是平面。这可行吗?@Yousef这是基础数学,低级计算。我确信有很多js库实现了这一点,但不能说是哪一个。尝试检查与游戏相关的LIB,因为这样的检查通常在游戏碰撞检测中执行。但在我看来,由你自己来实施会更快。
line equation is l(t) = [lx(t), ly(t)]
where lx = x0 + t*(x1-x0), ly = y0 + t*(y1-y0), and t here is variable between 0 and 1

if line intersects circle, there will be such value of t (say, t0), with which
l0x = lx(t0), l0y = ly(t0) fills condition

(l0x - cx)^2 + (l0y - cy)^2 < r^2

we need to find t0, and check if it's in range 0..1, here how we do it:

(x0 + t0*(x1-x0) - cx)^2 + (y0 + t0*(y1-y0) - cy)^2 = r^2

by solving this quadratic equation we will find 0, 1 or 2 solutions of t0
if it's 0 solutions - circle never intersects line
if it's 1 solution - circle touches line in 1 point
if it's 2 solution (t0a, t0b) - circle intersects line in 2 points, and additional check will be needed to check if range [t0a, t0b] intersects with range [0, 1]

to solve this equation we need normalize it:
(x0 + t0*(x1-x0) - cx)^2 + (y0 + t0*(y1-y0) - cy)^2 = r^2       equal to
((x0-cx) + t0*(x1-x0))^2 + ((y0 - cy) + t0*(y1-y0))^2 - r^2 = 0      equal to
(x0-cx)^2 + (t0*(x1-x0))^2 + 2*t0*(x1-x0)*(x0-cx) + (y0-cy)^2 + (t0*(y1-y0))^2 + 2*t0*(y1-y0)*(y0-cy) - r^2 = 0    equal to

t0^2 * A + t0 * B + C = 0, where
A = (x1-x0)^2 + (y1-y0)^2
B = 2*(x1-x0)*(x0-cx) + 2*(y1-y0)*(y0-cy)
C = (x0-cx)^2 + (y0-cy)^2 - r^2
t0a = -3.4, t1a = 2.1 - intersection occurs, 
t0a = -3.4, t1a = -2.1 - intersection not occurs, 
t0a = 0, t1a = 0.5 - intersection occurs