Master C
Master C

Reputation: 1546

Polar representation from x,y in Java

I am building the following:

public class Point2
{
    private double _radius , _alpha;    

    public Point2 ( int x , int y )
    {
        _radius = Math.sqrt ( Math.pow(x,2) + Math.pow (y,2) ) ;
        _alpha = Math.atan(y/x) ;
    }

}

The only thing that stuck me is the _alpha calc. I'm trying to think how to operate this one using Math class the shortest and readable way... Is my way OK ? thnx

Upvotes: 0

Views: 191

Answers (2)

Sean Reilly
Sean Reilly

Reputation: 21836

Mr E is correct, atan2 seems to be the way to go. Here is the link to the appropriate javadoc for atan2.

Upvotes: 0

YXD
YXD

Reputation: 32521

I don't know Java but for sure just use atan2

_alpha = Math.atan2(y,x);

Upvotes: 1

Related Questions