converting atan2 output to 360 deg
264 views (last 30 days)
Hi, I'm using the function atan2, however my output is from -180 to 180 degrees (I converted from radians) How do I modify it such that it outputs a value from 0-360 degrees?
winddir = (atan2(Vi,Ui))*(180/pi);
John D'Errico on 14 Apr 2015
Edited: John D'Errico on 14 Apr 2015
Since it will be periodic, just add 360 if the value is less than 0. This will suffice to correct the negative angles.
winddir = winddir + (winddir < 0)*360;
You can use atan2d if you prefer to work in degrees, but it will map to the same range, [-180,180], so you will still need to correct for the negative angles.
If you wanted a simple expression that works in one line of code, this should do:
winddir = atan2d(Vi,Ui) + 360*(Vi<0);
More Answers (2)
theodore panagos on 7 Nov 2018
You can use the formula:
That formula give the angle from 0 to 360 degrees for any value of x and y.
x=x2-x1 and y=y2-y1
For x=y=0 the result is undefined.