【问题标题】:Get angle from north using iOS api使用iOS api从北方获取角度
【发布时间】:2012-10-17 18:24:40
【问题描述】:

这是我想要的:

http://postimage.org/image/9pq8m79hx/

我知道 O 点和 X 点的坐标。是否有可能使用 iOS 方法找到 V 角(从北方向的角度)?

【问题讨论】:

    标签: objective-c ios math geolocation gps


    【解决方案1】:

    是的。

    #import <math.h>
    
    float a = -1 * atan2(y1 - y0, x1 - x0);
    if (a >= 0) {
        a += M_PI / 2;
    } else if (a < 0 && a >= -M_PI / 2) {
        a += M_PI / 2;
    } else {
        a += 2 * M_PI + M_PI / 2;
    }
    if (a > 2 * M_PI) a -= 2 * M_PI;
    

    现在a 将包含以弧度表示的角度,在区间0...2 PI 中。

    甚至不需要任何 iOS 特定的 API。请记住:iOS 仍然具有 libc 的所有功能。

    【讨论】:

    • @VladBogdan 实际上我花了 45 分钟才弄清楚如何正确地做到这一点。您要求坐标旋转 -PI/2... 并没有让我的生活更轻松...:D
    【解决方案2】:

    不确定 user529758 的答案是否解决了这个问题,我读到的是将东 0 度改为北 0 度。下面的代码有效 - 关键线是第 4 行,从东到北变化 0 度

    -(CGFloat) bearingFromNorthBetweenStartPoint: (CGPoint)startPoint andEndPoint:(CGPoint) endPoint {
    
    // get origin point of the Vector
    CGPoint origin = CGPointMake(endPoint.x - startPoint.x, endPoint.y - startPoint.y);
    
    // get bearing in radians
    CGFloat bearingInRadians = atan2f(origin.y, origin.x);
    
    // convert to bearing in radians to degrees
    CGFloat bearingInDegrees = bearingInRadians * (180.0 / M_PI);
    
    // convert the bearing so that it takes from North as 0 / 360 degrees, rather than from East as 0 degrees
    bearingInDegrees = 90 + bearingInDegrees;
    
    // debug comments:
    if (bearingInDegrees >= 0)
    {
        NSLog(@"Bearing >=0 in Degrees %.1f degrees", bearingInDegrees );
    }
    
    else
    {
        bearingInDegrees = 360 + bearingInDegrees;
        NSLog(@"Bearing in Degrees %.1f degrees", bearingInDegrees );
    }
    
    return bearingInDegrees;
    
    }
    

    【讨论】:

      猜你喜欢
      • 2020-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-14
      相关资源
      最近更新 更多