2010-08-25 97 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