【发布时间】:2011-12-13 17:25:52
【问题描述】:
我有一些代码可以很好地找到两点 temp 和 newpoint 之间的角度:
CGPoint newpoint=CGPointMake(newX,newY );
CGPoint diff = ccpSub(temp.position, newpoint);
float rads = atan2f( diff.y, diff.x);
float degs = CC_RADIANS_TO_DEGREES(rads);
CCLOG(@"now: %f,%f to:%f,%f degs:%f",temp.position.x,temp.position.y,newX,newY,degs);
它在大多数情况下都能正常工作,但在某些方面它有错误。以下是部分日志:
2011-12-13 11:05:19.401 Bells[2303:c503] now: 368.603271,147.813110 to:265.566284,210.476471 degs:-30.655481
2011-12-13 11:05:20.417 Bells[2303:c503] now: 77.030159,167.413544 to:184.491669,117.072098 degs:154.254135
2011-12-13 11:05:20.417 Bells[2303:c503] now: 355.119720,121.963585 to:307.802826,49.591988 degs:56.634174
2011-12-13 11:05:20.418 Bells[2303:c503] now: 465.000000,89.771843 to:373.258270,15.000000 degs:39.249508
2011-12-13 11:05:20.419 Bells[2303:c503] now: 307.277588,69.095749 to:363.791168,174.386093 degs:-118.026299
2011-12-13 11:05:21.417 Bells[2303:c503] now: 190.993607,194.947479 to:101.720551,255.007492 degs:-33.194214
2011-12-13 11:05:21.418 Bells[2303:c503] now: 23.930172,27.319054 to:85.588303,15.000000 degs:167.615768
2011-12-13 11:05:21.418 Bells[2303:c503] now: 365.033630,218.904007 to:277.799194,141.605042 degs:41.585751
2011-12-13 11:05:21.419 Bells[2303:c503] now: 221.239227,153.457306 to:116.308037,87.374733 degs:32.344673
2011-12-13 11:05:22.433 Bells[2303:c503] now: 140.302948,260.575500 to:191.313812,208.167419 degs:133.118576
2011-12-13 11:05:22.433 Bells[2303:c503] now: 16.781921,224.473648 to:90.456161,153.657745 degs:135.340363
2011-12-13 11:05:22.434 Bells[2303:c503] now: 307.802826,49.591988 to:391.818512,15.000000 degs:156.793350
2011-12-13 11:05:22.435 Bells[2303:c503] now: 265.566284,210.476471 to:311.953583,114.193481 degs:115.011368
2011-12-13 11:05:22.435 Bells[2303:c503] now: 140.599457,296.268768 to:39.459061,305.000000 degs:-4.328590
2011-12-13 11:05:22.436 Bells[2303:c503] now: 459.438629,190.859222 to:465.000000,96.320923 degs:92.733452
这一行尤其是问题的一个例子:
2011-12-13 11:05:21.418 Bells[2303:c503] now: 23.930172,27.319054 to:85.588303,15.000000 degs:167.615768
这两点之间的距离非常接近水平,因此您会期望一个准水平的角度。然而,这个角度几乎是垂直的。我不是 trig 方面的高手,但显然存在一个仅影响此处某些点的数学问题。有什么想法吗?
【问题讨论】:
-
两点之间没有夹角。两条线之间只能有一个角度。
-
啊!这是有道理的。我会玩这个。
-
显然,这段代码实际上应该根据以下内容查看两行; stackoverflow.com/questions/7191900/… 但角度并不总是正确
-
事实证明,我的代码只有 3 个字符。作为最终公式,它非常适用:
float degs =90- CC_RADIANS_TO_DEGREES(rads);
标签: cocos2d-iphone trigonometry