如何在圆中找不到多边形点的圆与多边形的交点

时间:2014-04-03 06:14:17

标签: javascript google-maps

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

1 个答案:

答案 0 :(得分:0)

检查此条件的多边形的每个点:

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

如果对于至少一个i来说它是真的,则交叉点就到了。

<强>更新

如果没有一个点直接在圈子里,那就越来越难了。您需要针对交叉点检查每条线对着圆圈。

要检测线与圆的交点,请使用以下检查:

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

我不会在这里写一下如何解决这样的标准二次方程式,它是非公开的。

如果您有一个包含2个值的解决方案,比如t0a和t0b,那么您需要根据范围[0,1]进行检查。

例如:

t0a = -3.4, t1a = 2.1 - intersection occurs, 
t0a = -3.4, t1a = -2.1 - intersection not occurs, 
t0a = 0, t1a = 0.5 - intersection occurs 

是的,可能你需要排序t0a和t0b,没有保证t0a&lt; T0B

您需要为每条多边形线运行此检查。