- (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应用程序中,根据以弧度计算的方位角(航向),我可以使用以下代码检测一个点是否在其视口内:
- (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, result?@"YES":@"NO");
return result;
}
问题在于我不完全理解它是如何工作的,特别是检查坐标(兴趣点)方位角(航向)是否在原点航向的左右方位角之间的视口范围处的那一行。
举个例子来说明我的误解-以度数为单位,如果坐标(兴趣点)计算到原点的方位为90度,然后原点视口面向坐标自身的方位为270度,则这意味着用户正在观察该点,但我不明白为什么代码仍然能够按照检查坐标方位角是否在原点视口范围内工作,而我认为应该是例如250 -(270)-290度。很明显,坐标方位角到原点的方位角是90度,所以应该是false。
非常感谢你对这里正在发生的事情有所帮助。