- (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アプリイムの建物で、私はポイントは以下のコードを使用して、そのビューポート内にあるかどうかを検出することができますラジアンで計算した方位(見出し)、与えられた:拡張現実ベアリング/見出し/方位角の混乱。 (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;
}
問題は、私はそれが特にどのように動作するかを十分に理解しないで座標(地点)方位(方位)が原点の左右の方位角の間のビューポート範囲内にあるかどうかをチェックする行に表示されます。
私の誤解の例を挙げてみましょう。座標(point of interest)が90度の原点に対する方位を計算し、原点ビューポートが270度の方位に直面している場合座標には、これは、ユーザーがポイントを見ていることを意味するが、私は、座標方位の支配が私が仮定する元のビューポート内にあるかどうかをチェックするようにコードがまだ動作する理由を理解していない250 - (270) - 290度。そして明らかに、原点を支配する座標方位は90であったので、それは誤っているはずです。
ここで本当に起こっていることを理解する助けがあれば、大歓迎です。