使用iOS api从北方获取角度

时间:2012-10-17 18:24:40

标签: objective-c ios math geolocation gps

这就是我想要的:

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

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

2 个答案:

答案 0 :(得分:2)

是的。

#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的所有功能。

答案 1 :(得分:1)

不确定user529758的答案是否解决了这个问题,我将其视为将0度向东方向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;

}