Obliczyć kąt między dwoma wektorami?
Zajmuję się tworzeniem aplikacji na iOS, która musi pokazywać POI (punkty szczególne) w zależności od pozycji urządzenia. użyłemCLLocationManager
aby uzyskać lokalizację i nagłówek użytkownika. Mam jedną parę współrzędnych celu. Na tej podstawie obliczam, która ćwiartka to i zwracanie wartości pływaka odchylenia od południa (0 stopni) w stopniach. Mam - / + 180 stopni na północy i 0 na południu. Oto fragment kodu:
-(float)updateTargetLongitude:(float)lon Latitude:(float)lat{
// //longitude = x
// //latitude = y
NSLog(@"current location = (%.5f, %.5f)", [[NSUserDefaults standardUserDefaults] doubleForKey:@"currentLongitude"], [[NSUserDefaults standardUserDefaults] doubleForKey:@"currentLatitude"]);
float x = lon - [[NSUserDefaults standardUserDefaults] doubleForKey:@"currentLongitude"];
float y = lat - [[NSUserDefaults standardUserDefaults] doubleForKey:@"currentLatitude"];
float angle;
NSLog(@"Searching angle from source (%.5f, %.5f) to destination (%.5f, %.5f)", locationManager.location.coordinate.longitude, locationManager.location.coordinate.latitude, lon, lat);
if (x == 0 && y == 0) {
NSLog(@"you're there already!");
return -0.1;
}
if(x == 0 && y > 0){
NSLog(@"look north");
angle = 180.0;
}
if (x == 0 && y < 0) {
NSLog(@"look south");
angle = 0;
}
if (x > 0 && y == 0) {
NSLog(@"look east");
angle = 90.0;
}
if (x < 0 && y == 0) {
NSLog(@"look west");
angle = -90;
}
if (x > 0 && y > 0) {
NSLog(@"first quarter");
angle = -atan2f(y, x) - M_PI_2;
}
if (x < 0 && y > 0) {
NSLog(@"second quarter");
angle = atan2f(y, x) + M_PI_2;
}
if (x < 0 && y < 0) {
NSLog(@"third quarter");
angle = atan2f(x, y);
}
if (x > 0 && y < 0) {
NSLog(@"fourth quarter");
angle = -atan2f(x, y);
}
NSLog(@"returning radians angle = %.4f for (%.5f, %.5f) :: degrees = %.3f", angle, y, x, angle * 180 / M_PI);
return angle * 180 / M_PI ;
}
Jakoś mam sytuację, gdy cel jest w czwartym kwartale, ale wynosi -93 stopnie od południa. Zgubiłem się i nie mam pojęcia, jak to naprawić ...
edytować: przez ćwiartkę rozumiem kartezjański układ współrzędnych, gdzie + y to północ, + x to wschód i tak dalej!
p.s .: Czytałem, że kompas iPhone'a jest naprawdę zły, ale jeśli tak, to jak aplikacja Google Maps działa poprawnie?
edytuj2: Popełniłem błąd z kątami. Oficjalnie mają -90 stopni na wschodzie i 90 na zachodzie.