Berechnen des Winkels zwischen zwei Vektoren?

Ich entwickle eine iOS-App, die POIs (Points of Interest) abhängig von der Überschrift des Geräts anzeigen muss. ich benutzteCLLocationManager um den Standort und die Überschrift des Benutzers abzurufen. Ich habe ein Paar Zielkoordinaten. Darauf aufbauend berechne ich, welches Quartal das ist und gebe den Abweichungswert vom Süden (0 Grad) in Grad zurück. Ich habe - / + 180 Grad im Norden und 0 im Süden. Hier ist ein Codeausschnitt:

-(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 ;
}

Irgendwie habe ich Situation, wenn das Ziel im vierten Quartal ist, aber -93 Grad von Süden ist. Ich bin verloren und ich habe keine Ahnung, wie ich das beheben soll ...

bearbeiten: Mit Quartal meine ich das kartesische Koordinatensystem, wobei + y Norden ist, + x Osten ist und so weiter!

ps .: Ich habe gelesen, dass der iPhone-Kompass wirklich schlecht ist, aber wenn ja, wie funktioniert eine App wie Google Maps richtig?

edit2: Ich habe einen Fehler mit Winkeln gemacht. Offiziell haben Sie -90 Grad im Osten und 90 Grad im Westen.

Antworten auf die Frage(3)

Ihre Antwort auf die Frage