两条线之间的顺时针角度

发布于 2024-09-07 19:51:46 字数 89 浏览 1 评论 0原文

我想计算两条线段 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 技术交流群。

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

发布评论

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

评论(3

闻呓 2024-09-14 19:51:46

要将 C# 中的任何角度转换为 0-359 范围,您可以使用以下“算法”:

public int Normalise (int degrees) {
    int retval = degrees % 360;
    if (retval < 0)
        retval += 360;
    return retval;
}

C# 遵循与 C 和 C++ 相同的规则,i % 360 将为您提供 -359359 对于任何整数,第二行是确保它在 0 到 359 的范围内(包括 0 和 359)。

一行上的偷偷摸摸的版本:

    degrees = ((degrees % 360) + 360) % 360;

它将在所有条件下标准化它。我不确定我是否会过多担心使用内联单行代码,除非性能至关重要,但我解释一下。

度% 360开始,您将得到-359359之间的数字。添加 360 会将范围修改为 1729 之间。然后最终的% 360 会将其带回到 0359 的范围。

For turning any angle into a 0-359 range in C#, you can use the following "algorithm":

public int Normalise (int degrees) {
    int retval = degrees % 360;
    if (retval < 0)
        retval += 360;
    return retval;
}

C# follows the same rules as C and C++ and i % 360 will give you a value between -359 and 359 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:

    degrees = ((degrees % 360) + 360) % 360;

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 and 359. Adding 360 will modify the range to between 1 and 729. Then the final % 360 will bring it back to the range 0 through 359.

開玄 2024-09-14 19:51:46

我会尝试:

if degrees is between [-360, 360]
    degrees = (degrees + 360) % 360;
else degrees = (degrees % 360) + 360) % 360;

I would try :

if degrees is between [-360, 360]
    degrees = (degrees + 360) % 360;
else degrees = (degrees % 360) + 360) % 360;
安人多梦 2024-09-14 19:51:46

来使任何具有负角度的解决方案始终为 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

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