Convert from Radians to Degrees in Java

前端 未结 3 998
广开言路
广开言路 2020-12-30 19:52

I\'m trying to get the alpha angle in degrees from x,y when user creates an object.

I wrote the following constructor:

public class Point
{
    priva         


        
相关标签:
3条回答
  • 2020-12-30 20:39

    Why not use the built-in method Math.toDegrees(), it comes with the Java SE.

    0 讨论(0)
  • 2020-12-30 20:41

    The idea looks ok, but I would suggest using Math.atan2 instead of Math.atan.

    0 讨论(0)
  • 2020-12-30 20:44

    This should be by far the shortest and simplest way:

            _radius = Math.hypot(x, y);
            _alpha = Math.toDegrees(Math.atan2(y, x));
    

    Keep in mind that when computed this way, _alpha will have values between -180 and 180 degrees.

    0 讨论(0)
提交回复
热议问题