计算两点之间角度的错误

发布于 2024-12-21 04:03:24 字数 2338 浏览 0 评论 0原文

我有一些代码可以很好地找到两点 tempnewpoint 之间的角度:

        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

这两点之间的距离非常接近水平,因此您会期望有一个准水平的角度。然而,角度几乎是垂直的。我不是三角函数高手,但显然有一个数学问题只影响这里的某些点。有什么想法吗?

I have some code that is working pretty well to find the angle between two points temp and 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);

It correctly works most of the time, but for some points it has errors. Here is part of the log:

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

This line in particular is an example of the problem:

2011-12-13 11:05:21.418 Bells[2303:c503] now: 23.930172,27.319054 to:85.588303,15.000000 degs:167.615768

This distance between these two points is very approximately horizontal, thus you'd expect an angle that is quasi horizontal. Yet, the angle is nearly vertical. I'm not a whiz at trig but clearly there is a math issue that is affecting only some points here. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不醒的梦 2024-12-28 04:03:24

您可以使用 ccpAngle(<#CGPoint a#>, <#CGPoint b#>) 或 ccpAngleSigned(<#CGPoint a#>, <#CGPoint b#>),是的,两个点之间有一个角度,一个点是一个向量。

请记住,cocos 使用度数,而使用 CGPoints 完成的数学使用弧度。还要记住,cocos 使用顺时针旋转,而 CGPoints 角度是逆时针旋转。

您可以使用建议的助手 CC_RADIANS_TO_DEGREES

you can use ccpAngle(<#CGPoint a#>, <#CGPoint b#>) or ccpAngleSigned(<#CGPoint a#>, <#CGPoint b#>), and yes there is an angle between 2 points, a point is a vector.

remember that cocos use degrees and math done with CGPoints uses Radians. also remember that cocos uses rotations clockwise and CGPoints angles are anticlockwise.

You can use as suggested helpers as CC_RADIANS_TO_DEGREES

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文