gpt4 book ai didi

ios - 计算与位置的角度

转载 作者:行者123 更新时间:2023-12-01 16:35:56 31 4
gpt4 key购买 nike

我有一个箭头图像,其行为类似于特定位置的指南针。有时它可以正常工作,而其他时候它可以被镜像。因此,如果我面朝东方,且位置在我的正东,它应该指向上方,但有时指向下方。

#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)heading
{
// update direction of arrow
CGFloat degrees = [self p_calculateAngleBetween:_myLocation
and:_otherLocation];
CGFloat rads = (degrees - heading.trueHeading) * M_PI / 180;


CGAffineTransform tr = CGAffineTransformIdentity;
tr = CGAffineTransformConcat(tr, CGAffineTransformMakeRotation(rads) );

[_directionArrowView setTransform:tr];
}

-(CGFloat) p_calculateAngleBetween:(CLLocationCoordinate2D)coords0 and:(CLLocationCoordinate2D)coords1 {
double x = 0, y = 0 , deg = 0, deltaLon = 0;

deltaLon = coords1.longitude - coords0.longitude;
y = sin(deltaLon) * cos(coords1.latitude);
x = cos(coords0.latitude) * sin(coords1.latitude) - sin(coords0.latitude) * cos(coords1.latitude) * cos(deltaLon);
deg = RADIANS_TO_DEGREES(atan2(y, x));

if(deg < 0)
{
deg = -deg;
}
else
{
deg = 360 - deg;
}

return deg;
}

这是计算我与另一个位置的角度的正确方法吗?还是我错过了一步?由于有时箭头直接指向相反的方向,所以我认为这是我的数学问题。

最佳答案

要从x和y计算弧度:

double r = atan(y/x);
if (x<0)
r = M_PI + r;
else if (x>0 && y<0)
r = 2 * M_PI + r;

当X为零时不存在除以0的问题,因为atan函数可以正确处理此问题:

如果参数为正无穷大(负无穷大),则返回+ pi / 2(-pi / 2)。

关于ios - 计算与位置的角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28332333/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com