计算两个向量之间的角度? [英] Calculating angle between two vectors?

查看:635
本文介绍了计算两个向量之间的角度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个iOS应用,该应用必须根据设备的标题显示POI(兴趣点)。我使用 CLLocationManager 来获取用户的位置和标题。我有一对目的地的坐标。基于此,我要计算出那个四分之一,然后返回以度为单位的与南(0度)的偏差的浮点值。我在北部为-/ + 180度,在南部为0。这是代码段:

I'm developing an iOS app, which has to show POIs (points of interest) depending on device heading. I used CLLocationManager to get user's location and heading. I have one pair of destination's coordinates. Based on this I'm calculating which quarter is that and returning float value of deviation from south (0 degrees) in degrees. I have -/+180 degrees in the north and 0 in the south. Here is code snippet:

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

我有点情况,目标是第四季度,但是-93南边的度数。我迷路了,我也不知道该如何解决...

Somehow I have situation, when target is in fourth quarter, but is -93 degrees from south. I'm lost and I don't have any idea how to fix that...

编辑:按季度,我的意思是笛卡尔坐标系,+ y在北,+ x在东,依此类推!

edit: by quarter I mean Cartesian coordinate system, where +y is north, +x is east and so on!

ps:我读过iPhone指南针确实很糟糕,但如果这样,像Google这样的应用程序地图工作正常?

p.s.: I've read that iPhone compass is really bad, but if so how app like Google maps is working properly?

edit2 :我在角度上弄错了。官方上o向东为-90度,西向为90度。

edit2: I made a mistake with angles. Oficially o have -90 degrees in east and 90 in west.

推荐答案

如果我正确看到的话,公式

If I see it correctly, the formula

angle = atan2(x, -y) * 180.0/M_PI;

应在所有象限中均有效,如果<$ c $

should work in all quadrants, making all the if statements unnecessary.

atan2(y,x)返回向量(x,y)与正x轴之间的夹角,返回值始终在 -pi pi

atan2(y, x) returns the angle between the vector (x, y) and the positive x-axis, the return value is always between -pi and pi.

替换(y,x)通过参数
中的(x,-y)表示矢量旋转了90度,因此上述公式
的结果是相对于负y轴的角度,即您想要的角度。

Replacing (y, x) by (x, -y) in the arguments means that the vector is rotated by 90 degrees, therefore the result of the above formula is the angle measured to the negative y-axis, which is what you wanted.

更新(根据问题中的 edit2):如果要求是 south = 0 deg, east = -90 deg, west = +90 deg,则公式为

Update (according to "edit2" in the question): If the requirement is "south = 0 deg", "east = -90 deg", "west = +90 deg" then the formula would be

angle = atan2(-x, -y) * 180.0/M_PI;

这篇关于计算两个向量之间的角度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆