从2个位置获得角度 [英] Get angle from 2 positions

查看:97
本文介绍了从2个位置获得角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个对象,当我移动一个,我想从另一个角度。

I have got 2 objects and when i move one I want to get the angle from the other.

例如

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

我已经尝试了以下和每个变化under the sun

I have tried the following and every variation under the sun

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

但我只是得到2.949023返回,我想要45度等等

But I just get 2.949023 returned where I want something like 45 degrees etc

请帮助

感谢

Jonathan

推荐答案

此解答是否有帮助?

如何将atan2()映射到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。

这有助于。

这篇关于从2个位置获得角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆