ios – 从2个位置获取角度

前端之家收集整理的这篇文章主要介绍了ios – 从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.

希望这可以帮助.

原文链接:https://www.f2er.com/iOS/336868.html

猜你在找的iOS相关文章