0

I have a graph that allows -ve values on x and y axis so the graph can be considered as having four quadrants, and where the x, y plot can be considered an angle that goes from 0-360 counter clockwise such that x=100, y=0 would be angle zero, x=0, y=100 would be angle 90, x=-100, y=0 would be angle 180, and x=0, y=-100 would be angle 270

Given a a value of x and y I want to calculate the angle starting from so given a value of x=30, y=60 I can do

Math.toDegrees(Math.atan((double)y/x));

i.e.

System.out.println(Math.toDegrees(Math.atan((double)60/30)));

giving

63.434

but how do generalize this work with negative numbers, i.e if i had y=60 and x=-30 then the answer should be 116.56. If i had y=-60 and x=-30 then it would be 206.56

Im using Java

The reasons I go counter clockwise is because of the algorithm Im trying to implement, it seems counter inuitive to be that is the way they do it.

I got something working in the following clunky way

 if(x> 0 & y> 0)
        {
            angle= (Math.toDegrees(Math.atan((double)Math.abs(y)/ Math.abs(x))));
        }
        else if(x< 0 & y> 0)
        {
            angle= (Math.toDegrees(Math.atan((double)Math.abs(x)/ Math.abs(y)))) + 90;
        }
        else if(x< 0 & y< 0)
        {
            angle= (Math.toDegrees(Math.atan((double)Math.abs(y)/ Math.abs(x)))) + 180;
        }
        else if(x> 0 & y< 0)
        {
            angle= (Math.toDegrees(Math.atan((double)Math.abs(x)/ Math.abs(y)))) + 270;
        }
Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
  • 2
    Possible duplicate of [Java: Calculating the angle between two points in degrees](http://stackoverflow.com/questions/9970281/java-calculating-the-angle-between-two-points-in-degrees) – Nico Schertler Dec 01 '15 at 11:50
  • 1
    @NicoSchertler you are probably right it seems atan2 instead of atan is perhaps the thing I should be using. – Paul Taylor Dec 01 '15 at 12:58

0 Answers0