两条线之间的顺时针角度
我想计算两条线段 A 和 B 之间的顺时针角度。因此所得角度必须在 0 到 360-1 度之间。我已经在 SO 中看到了所有其他答案,但它们给了我负面的角度。谢谢。
I want to calculate a clockwise angle between two line segment A and B. So the resulting angle must be between 0 to 360-1 degrees. I've seen all other answers in SO but they gave me negative angles. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要将 C# 中的任何角度转换为 0-359 范围,您可以使用以下“算法”:
C# 遵循与 C 和 C++ 相同的规则,
i % 360
将为您提供-359
和359
对于任何整数,第二行是确保它在 0 到 359 的范围内(包括 0 和 359)。一行上的偷偷摸摸的版本:
它将在所有条件下标准化它。我不确定我是否会过多担心使用内联单行代码,除非性能至关重要,但我会解释一下。
从% 360 会将其带回到
度% 360
开始,您将得到-359
和359
之间的数字。添加360
会将范围修改为1
和729
之间。然后最终的0
到359
的范围。For turning any angle into a 0-359 range in C#, you can use the following "algorithm":
C# follows the same rules as C and C++ and
i % 360
will give you a value between-359
and359
for any integer, then the second line is to ensure it's in the range 0 through 359 inclusive.A sneaky version on one line:
which would normalise it under all conditions. I'm not sure I'd worry too much about using the inline one-liner unless performance was critical, but I will explain it.
From
degrees % 360
, you will get a number between-359
and359
. Adding360
will modify the range to between1
and729
. Then the final% 360
will bring it back to the range0
through359
.我会尝试:
I would try :
来使任何具有负角度的解决方案始终为 0-360
当然,您可以通过调整: float Positive = (angle < 0) ? (360+角度):角度
Surely you could adapt any solution with negative angles to always be 0-360 by adjusting:
float positive = (angle < 0) ? (360 + angle) : angle