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.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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 % 360will give you a value between-359and359for 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-359and359. Adding360will modify the range to between1and729. Then the final% 360will bring it back to the range0through359.