簡體   English   中英

如何找到圓中多邊形無點的圓和多邊形的交點

[英]How can we find intersection of circle and polygon for no point of polygon in circle

假設我有一個多邊形和一個圓圈的Google Drawingmanager(谷歌地圖)。 場景是多邊形中沒有點,但仍然相交。 在這種情況下,如何檢查路口? ![圓和多邊形相交,並且圓內沒有多邊形點] [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 <t0b

您將需要為多邊形的每條線運行此檢查。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM