【发布时间】:2014-04-03 06:14:17
【问题描述】:
假设我有一个多边形和一圈谷歌绘图管理器(谷歌地图)。场景是没有多边形的点在圆中但仍然相交。在这种情况下如何检查交叉路口? ![圆和多边形相交,圆内没有多边形点][1]
【问题讨论】:
假设我有一个多边形和一圈谷歌绘图管理器(谷歌地图)。场景是没有多边形的点在圆中但仍然相交。在这种情况下如何检查交叉路口? ![圆和多边形相交,圆内没有多边形点][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
如果至少有一个 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
您需要对每条多边形线运行此检查。
【讨论】: