Objective c 使用iosapi获取从北向的角度
以下是我想要的: 我知道O点和X点的坐标。是否有可能使用iOS方法找到V角(从北向的角度)?是的Objective c 使用iosapi获取从北向的角度,objective-c,ios,math,geolocation,gps,Objective C,Ios,Math,Geolocation,Gps,以下是我想要的: 我知道O点和X点的坐标。是否有可能使用iOS方法找到V角(从北向的角度)?是的 #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 +
#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=-1*atan2(y1-y0,x1-x0);
如果(a>=0){
a+=M_PI/2;
}否则如果(a<0&&a>=-M\u PI/2){
a+=M_PI/2;
}否则{
a+=2*M_-PI+M_-PI/2;
}
如果(a>2*M_-PI)a-=2*M_-PI;
现在a
将包含以弧度为单位的角度,间隔0…2 PI
甚至不需要任何特定于iOS的API。请记住:iOS仍然具有libc的所有功能。是的
#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=-1*atan2(y1-y0,x1-x0);
如果(a>=0){
a+=M_PI/2;
}否则如果(a<0&&a>=-M\u PI/2){
a+=M_PI/2;
}否则{
a+=2*M_-PI+M_-PI/2;
}
如果(a>2*M_-PI)a-=2*M_-PI;
现在a
将包含以弧度为单位的角度,间隔0…2 PI
甚至不需要任何特定于iOS的API。请记住:iOS仍然具有libc的所有功能。不确定user529758的回答是否解决了这个问题,我认为这是关于将东方的0度改为北方的0度。下面的代码工作-关键线是第四条线,从东向北变化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;
}
不确定user529758的答案是否解决了这个问题,我认为这个问题是关于将东方的角度改为0度,将北方的角度改为0度。下面的代码工作-关键线是第四条线,从东向北变化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;
}
@VladBogdan事实上我花了45分钟才弄明白如何正确地做。你要求坐标旋转-PI/2…,这并没有让我的生活更轻松:D@VladBogdan事实上,我花了45分钟才弄明白如何正确地做这件事。你要求坐标旋转-PI/2…,这并没有让我的生活更轻松:D