答案 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;
}