【问题标题】:How can we find intersection of circle and polygon for no point of polygon in circle对于圆中没有多边形的点,我们如何找到圆和多边形的交点
【发布时间】:2014-04-03 06:14:17
【问题描述】:

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

【问题讨论】:

    标签: javascript google-maps


    【解决方案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

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

    【讨论】:

    • 真的!如果多边形的至少一个点在圆内。但是圆内没有多边形点。您可以说是与圆相交的折线。我们如何识别
    • 我明白了,所以我更新了一个答案。这会有点困难,但您需要通过求解二次方程来检查多边形的每一行与圆。
    • 感谢您的宝贵时间。我需要问你真的认为这些方程可以应用于谷歌绘图管理器吗?因为我们有 lat longs 而不是 x 和 y,是的,地球肯定不是平面。这可行吗?
    • @Yousef 这是基础数学,低级计算。我敢肯定有很多 js 库实现了这一点,但不能说是哪些。尝试检查与游戏相关的库,因为这种检查通常在游戏碰撞检测中执行。但在我看来,自己实现会更快。
    猜你喜欢
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 2015-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多