Solution using Modulo
A simple solution that catches all cases.
degrees = (degrees + 360) % 360; // +360 for implementations where mod returns negative numbers
Explanation
Positive: 1 to 180
If you mod any positive number between 1 and 180 by 360, you will get the exact same number you put in. Mod here just ensures these positive numbers are returned as the same value.
Negative: -180 to -1
Using mod here will return values in the range of 180 and 359 degrees.
Special cases: 0 and 360
Using mod means that 0 is returned, making this a safe 0-359 degrees solution.
Answer from Liam George Betsworth on Stack OverflowSolution using Modulo
A simple solution that catches all cases.
degrees = (degrees + 360) % 360; // +360 for implementations where mod returns negative numbers
Explanation
Positive: 1 to 180
If you mod any positive number between 1 and 180 by 360, you will get the exact same number you put in. Mod here just ensures these positive numbers are returned as the same value.
Negative: -180 to -1
Using mod here will return values in the range of 180 and 359 degrees.
Special cases: 0 and 360
Using mod means that 0 is returned, making this a safe 0-359 degrees solution.
(x > 0 ? x : (2*PI + x)) * 360 / (2*PI)
You can multiply the result by (180/pi) to convert the magnitude to degrees. If it's negative then you would have to add 360 to it afterwards (assuming you want a range of 0 to 360.)
For example, -pi/2 becomes -90 after the multiplication, and adding that to 360 results in 270, which gives you the amount of degrees to rotate counter-clockwise from the positive x-direction to reach the point (which is what you're looking for I think.)
You can add 180 to your converted result to give a range of 0 to 360.
math - Convert atan2 value to standard 360-degree-system value - Stack Overflow
mathematics - How does atan2 work when getting angle of a vector? - Game Development Stack Exchange
How to get 0-360 degree from two points - Questions & Answers - Unity Discussions
Highest scored 'atan2' questions - Stack Overflow
From the Wikipedia page:
It is returning the shortest angle of rotation from the positive x direction to the point, where a positive angle indicates the counter-clockwise direction. Therefore it makes sense that it would only return values between 0 and 180, since anything greater than 180 can be 'better' approached by a smaller angle in the opposite direction.
Returning -90 instead of 270 also makes sense now, since moving 90 degrees clockwise is less than moving 270 degrees counter-clockwise to reach the same point. This also suggests that your interpretation has mixed up the positive and negative 90 degree directions but is otherwise fine.
The referenced Wikipedia article provides the logic of the atan2 function, which has been optimized computationally (it works faster than implementing the statement explicitly). To convert to 0-to-360 degree values, first you must adjust the range of atan2 results, then convert to degrees, shown here as a vectorized operation in pseudo-code:
rad = atan2(y,x)
rad[rad < 0] = rad[rad < 0] + 2*pi
deg = rad*(180/pi)
Hope that is helpful.
To translate degrees range 0..360 into radians range -Pi..Pi:
if(angle > 180)
angle -= 360;
angle_radians = Math.toRadians(angle);
If your angle may be beyond standard range (you did not mentioned this), use integer modulo instead of loops
angle = angle % 360
(check how modulo operator works with negative numbers in your language)
Though your question is not very clear, it seems that you want to retrieve the angle before the statement
if (angle < 0)
angle += 360;
As atan2 returns a value in the range [-π,π), i.e. [-180°,180°), the subrange [-180°,0°) is mapped to [180°,360°). Then you invert with
if (angle >= 180°)
angle-= 360;
You also need to convert back to radians.
The atan function only gives half the unit circle between -pi/2 and +pi/2 (0 on x axis), there is another library function that can give the whole unit circle between -pi and + pi, atan2
I would think you are better of using atan2 to get the right quadrant rather than branching yourself, then just scale as you have been, something like
Math.atan2(y2 - y1, x2 - x1) * 180 / Math.PI + 180
The multiply by 180 over pi is just the scale from radians to degrees as in the question (but with the division by a division simplified), the +180 makes sure its always positive i.e. 0-360 deg rather than -180 to 180 deg
Math.atan limits you to the two rightmost quadrants on the unit circle. To get the full 0-360 degrees:
if x < 0 add 180 to the angle
else if y < 0 add 360 to the angle.
Your coordinate system is rotated and inverted compared to mine (and compared to convention). Positive x is to the right, positive y is up. 0 degrees is to the right (x>0, y=0, 90 degrees is up (x=0,y>0) 135 degrees is up and to the left (y>0, x=-y), etc. Where are your x- and y-axes pointing?