gpt4 book ai didi

ios - 从 2 个位置获取角度

转载 作者:IT王子 更新时间:2023-10-29 08:04:25 26 4
gpt4 key购买 nike

我有 2 个物体,当我移动一个物体时,我想获得另一个物体的角度。

例如:

Object1X = 211.000000, Object1Y = 429.000000
Object2X = 246.500000, Object2Y = 441.500000

我已经尝试了以下以及在阳光下的每一种变体:

double radians = ccpAngle(Object1,Object2);
double degrees = ((radians * 180) / Pi);

但我只得到 2.949023 返回我想要的东西,比如 45 度等。

最佳答案

这个其他答案有帮助吗?

How to map atan2() to degrees 0-360

我是这样写的:

- (CGFloat) pointPairToBearingDegrees:(CGPoint)startingPoint secondPoint:(CGPoint) endingPoint
{
CGPoint originPoint = CGPointMake(endingPoint.x - startingPoint.x, endingPoint.y - startingPoint.y); // get origin point to origin by subtracting end from start
float bearingRadians = atan2f(originPoint.y, originPoint.x); // get bearing in radians
float bearingDegrees = bearingRadians * (180.0 / M_PI); // convert to degrees
bearingDegrees = (bearingDegrees > 0.0 ? bearingDegrees : (360.0 + bearingDegrees)); // correct discontinuity
return bearingDegrees;
}

运行代码:

CGPoint p1 = CGPointMake(10, 10);
CGPoint p2 = CGPointMake(20,20);

CGFloat f = [self pointPairToBearingDegrees:p1 secondPoint:p2];

这将返回 45。

希望这对您有所帮助。

关于ios - 从 2 个位置获取角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6064630/

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