2010-08-25 95 views
1

任何人都知道如何確定同一圓的兩個扇區是否相交?確定給定圓的兩個扇區是否相交?

比方說,我有一個扇區A,由開始和結束角度A1和A2,以及一個扇區B,由開始角度B1和結束角度B2表示。所有角度範圍從0到2 * PI弧度(或0到360度)。

如何確定角度A是否與角度B相交?

我已經試過two rectangle intersection problem像下面的變化:

if(a1 <= b2 && a2 >= b1) { 
    // the sectors intersect 
} else { 
    // the sectores doesn't intersect 
} 

此方法是好的只要沒有扇區越過0度點。但是如果任何一個部門超過它,計算就會變得不正確。

潛在的問題是創建一個定向(基於標題)的增強現實應用程序。扇區A是對象,而扇區B是視口。角度獲得如下:

A0 = bearing of the object 
A1 = A0 - objectWidthInRadians 
A2 = A0 + objectWidthInRadians 

B0 = heading of the user (device) 
B1 = B0 - viewportWidthInRadians 
B2 = B0 + viewportWidthInRadians 

在此先感謝。

回答

1

你真正關心的是在軸承中最短的差是否大於碰撞範圍較小:

// absolute difference in bearings gives the path staying within the 0..2*pi range 
float oneWay = abs(A0 - B0); 

// .. but this may not be the shortest, so try the other way around too 
float otherWay = 2 * pi - oneWay; 

if (min(oneWay, otherWay) < (objectWidthInRadians + viewPortWidthInRadians)) 
{ 
    // object is visible... 
} 

請注意,您width定義是一個有點奇怪(似乎是真的半角)並顯示了A1等實際上不夾入規定[0..2*pi]範圍的計算...

+0

嗨, 我試過你的代碼,但似乎並沒有爲這些測試值工作(所有值都爲度)軸承A0 = 337.5標題B0 = 97.37 objectWidth = 72.86 vie方位(A)範圍爲[337.5..37.5],而航向(B)範圍爲[97.37..170.23]。很明顯,航向範圍(B)與方位(A)不重疊,但上面的答案通過了它。 oneWay = 245.13,otherWay = 114.87,objectWidth + viewPortWidth = 132.86 – adib 2010-08-25 10:16:04

+0

@adib這裏顯示的範圍是單向計算的,即'[heading,heading + width]',而問題中顯示的代碼是雙向的, '[heading-width,heading + width]'。 (這是我關於半角的說明。)在單向情況下,您需要減半用於測試碰撞的寬度。 – walkytalky 2010-08-25 10:35:34

+0

感謝您的解釋(我認爲你的答案是[標題,標題+寬度]慣例,所以我改變了我的代碼)。粗略測試表明你的配方似乎工作。謝謝你的回答。 – adib 2010-08-25 13:07:35