7
- (float)angleFromCoordinate:(CLLocationCoordinate2D)first toCoordinate:(CLLocationCoordinate2D)second {
//first is origin
//second is point
float longitudinalDifference = second.longitude - first.longitude;
float latitudinalDifference = second.latitude - first.latitude;
float possibleAzimuth = (M_PI * .5f) - atan(latitudinalDifference/longitudinalDifference);
if (longitudinalDifference > 0)
{
return possibleAzimuth;
}
else if (longitudinalDifference < 0)
{
return possibleAzimuth + M_PI;
}
else if (latitudinalDifference < 0)
{
return M_PI;
}
return 0.0f;
}
上面的代碼(取自開源的ARkit項目)計算從興趣點到原點(用戶位置)的角度。鑑於弧度計算的方位角(航向),在AR應用IM建築我可以檢測一個點是否是它的視口內使用下面的代碼:增強現實方位/標題/方位混亂。 (iphone ARKit代碼)
- (BOOL)viewportContainsCoordinate:(ARCoordinate *)coordinate {
double centerAzimuth = self.centerCoordinate.azimuth;
double leftAzimuth = centerAzimuth - VIEWPORT_WIDTH_RADIANS/2.0;
if (leftAzimuth < 0.0) {
leftAzimuth = 2 * M_PI + leftAzimuth;
}
double rightAzimuth = centerAzimuth + VIEWPORT_WIDTH_RADIANS/2.0;
if (rightAzimuth > 2 * M_PI) {
rightAzimuth = rightAzimuth - 2 * M_PI;
}
BOOL result = (coordinate.azimuth > leftAzimuth && coordinate.azimuth < rightAzimuth); //THIS LINE
if(leftAzimuth > rightAzimuth) {
result = (coordinate.azimuth < rightAzimuth || coordinate.azimuth > leftAzimuth);
}
double centerInclination = self.centerCoordinate.inclination;
double bottomInclination = centerInclination - VIEWPORT_HEIGHT_RADIANS/2.0;
double topInclination = centerInclination + VIEWPORT_HEIGHT_RADIANS/2.0;
//check the height.
result = result && (coordinate.inclination > bottomInclination && coordinate.inclination < topInclination);
//NSLog(@"coordinate: %@ result: %@", coordinate, [email protected]"YES":@"NO");
return result;
}
是我不明白的問題完全它是如何工作的具體在它檢查座標(興趣點)方位角(航向)是否在原點標題的左右方位角之間的視角端口範圍內的線上。
因此,舉個例子來說明我的誤解 - 以度爲單位,如果座標(興趣點)計算90度原點的方位,然後原點視口以270度自己的方位面對它座標,這將意味着用戶正在看點,但我不明白爲什麼代碼仍然工作,因爲它會檢查座標方位軸承是否在原點視口內,我認爲這將是例如250 - (270) - 290度。顯然,座標原點的座標方位角是90,所以它應該是錯誤的。
任何幫助瞭解真正發生在這裏的是什麼非常感謝。