我有两个对象,当我移动一个我想要从另一个角度.
例如
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.jb51.cc/iOS/336868.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。