How to map atan2() to degrees 0-360












85















atan2(y,x) has that discontinuity at 180° where it switches to -180°..0° going clockwise.



How do I map the range of values to 0°..360°?



here is my code:



CGSize deltaPoint = CGSizeMake(endPoint.x - startPoint.x, endPoint.y - startPoint.y);
float swipeBearing = atan2f(deltaPoint.height, deltaPoint.width);


I'm calculating the direction of a swiping touch event given the startPoint and endPoint, both XY point structs. The code is for the iPhone but any language that supports atan2f() will do.



Thanks for your help guys, with both the general solution and code.



Update: I made erikkallen's answer into a function with nice long variable names so I'll comprehend it 6 months from now. Maybe it will help some other iPhone noob.



float PointPairToBearingDegrees(CGPoint startingPoint, CGPoint endingPoint)
{
CGPoint originPoint = CGPointMake(endingPoint.x - startingPoint.x, endingPoint.y - startingPoint.y); // get origin point to origin by subtracting end from start
float bearingRadians = atan2f(originPoint.y, originPoint.x); // get bearing in radians
float bearingDegrees = bearingRadians * (180.0 / M_PI); // convert to degrees
bearingDegrees = (bearingDegrees > 0.0 ? bearingDegrees : (360.0 + bearingDegrees)); // correct discontinuity
return bearingDegrees;
}









share|improve this question

























  • Note: The posted update method will not return zero degrees, but values from just above 0 to 360.0.

    – chux
    Sep 27 '13 at 2:37






  • 1





    > [How to Get angle from 2 positions][1] [1]: stackoverflow.com/questions/9457988/…

    – MAnoj Sarnaik
    Apr 8 '15 at 8:45











  • This function works great, however the angle of "bearingDegrees" calculation is flipped. for example, 45 degrees would typically by the 1st quadrant, however this in the 4th quadrant. 135 degrees would typically be in the 2nd quadrant but this function returns it to be in the 3rd quadrant. i can simply take the function return value x and negate that from 360 to get the correct angle value however I'm curious to know why this is happening in the first place?

    – goelv
    May 18 '15 at 10:16


















85















atan2(y,x) has that discontinuity at 180° where it switches to -180°..0° going clockwise.



How do I map the range of values to 0°..360°?



here is my code:



CGSize deltaPoint = CGSizeMake(endPoint.x - startPoint.x, endPoint.y - startPoint.y);
float swipeBearing = atan2f(deltaPoint.height, deltaPoint.width);


I'm calculating the direction of a swiping touch event given the startPoint and endPoint, both XY point structs. The code is for the iPhone but any language that supports atan2f() will do.



Thanks for your help guys, with both the general solution and code.



Update: I made erikkallen's answer into a function with nice long variable names so I'll comprehend it 6 months from now. Maybe it will help some other iPhone noob.



float PointPairToBearingDegrees(CGPoint startingPoint, CGPoint endingPoint)
{
CGPoint originPoint = CGPointMake(endingPoint.x - startingPoint.x, endingPoint.y - startingPoint.y); // get origin point to origin by subtracting end from start
float bearingRadians = atan2f(originPoint.y, originPoint.x); // get bearing in radians
float bearingDegrees = bearingRadians * (180.0 / M_PI); // convert to degrees
bearingDegrees = (bearingDegrees > 0.0 ? bearingDegrees : (360.0 + bearingDegrees)); // correct discontinuity
return bearingDegrees;
}









share|improve this question

























  • Note: The posted update method will not return zero degrees, but values from just above 0 to 360.0.

    – chux
    Sep 27 '13 at 2:37






  • 1





    > [How to Get angle from 2 positions][1] [1]: stackoverflow.com/questions/9457988/…

    – MAnoj Sarnaik
    Apr 8 '15 at 8:45











  • This function works great, however the angle of "bearingDegrees" calculation is flipped. for example, 45 degrees would typically by the 1st quadrant, however this in the 4th quadrant. 135 degrees would typically be in the 2nd quadrant but this function returns it to be in the 3rd quadrant. i can simply take the function return value x and negate that from 360 to get the correct angle value however I'm curious to know why this is happening in the first place?

    – goelv
    May 18 '15 at 10:16
















85












85








85


21






atan2(y,x) has that discontinuity at 180° where it switches to -180°..0° going clockwise.



How do I map the range of values to 0°..360°?



here is my code:



CGSize deltaPoint = CGSizeMake(endPoint.x - startPoint.x, endPoint.y - startPoint.y);
float swipeBearing = atan2f(deltaPoint.height, deltaPoint.width);


I'm calculating the direction of a swiping touch event given the startPoint and endPoint, both XY point structs. The code is for the iPhone but any language that supports atan2f() will do.



Thanks for your help guys, with both the general solution and code.



Update: I made erikkallen's answer into a function with nice long variable names so I'll comprehend it 6 months from now. Maybe it will help some other iPhone noob.



float PointPairToBearingDegrees(CGPoint startingPoint, CGPoint endingPoint)
{
CGPoint originPoint = CGPointMake(endingPoint.x - startingPoint.x, endingPoint.y - startingPoint.y); // get origin point to origin by subtracting end from start
float bearingRadians = atan2f(originPoint.y, originPoint.x); // get bearing in radians
float bearingDegrees = bearingRadians * (180.0 / M_PI); // convert to degrees
bearingDegrees = (bearingDegrees > 0.0 ? bearingDegrees : (360.0 + bearingDegrees)); // correct discontinuity
return bearingDegrees;
}









share|improve this question
















atan2(y,x) has that discontinuity at 180° where it switches to -180°..0° going clockwise.



How do I map the range of values to 0°..360°?



here is my code:



CGSize deltaPoint = CGSizeMake(endPoint.x - startPoint.x, endPoint.y - startPoint.y);
float swipeBearing = atan2f(deltaPoint.height, deltaPoint.width);


I'm calculating the direction of a swiping touch event given the startPoint and endPoint, both XY point structs. The code is for the iPhone but any language that supports atan2f() will do.



Thanks for your help guys, with both the general solution and code.



Update: I made erikkallen's answer into a function with nice long variable names so I'll comprehend it 6 months from now. Maybe it will help some other iPhone noob.



float PointPairToBearingDegrees(CGPoint startingPoint, CGPoint endingPoint)
{
CGPoint originPoint = CGPointMake(endingPoint.x - startingPoint.x, endingPoint.y - startingPoint.y); // get origin point to origin by subtracting end from start
float bearingRadians = atan2f(originPoint.y, originPoint.x); // get bearing in radians
float bearingDegrees = bearingRadians * (180.0 / M_PI); // convert to degrees
bearingDegrees = (bearingDegrees > 0.0 ? bearingDegrees : (360.0 + bearingDegrees)); // correct discontinuity
return bearingDegrees;
}






math quartz-2d atan2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '13 at 21:04









Bob Gilmore

5,816104046




5,816104046










asked Aug 21 '09 at 9:57









willc2willc2

17.7k218098




17.7k218098













  • Note: The posted update method will not return zero degrees, but values from just above 0 to 360.0.

    – chux
    Sep 27 '13 at 2:37






  • 1





    > [How to Get angle from 2 positions][1] [1]: stackoverflow.com/questions/9457988/…

    – MAnoj Sarnaik
    Apr 8 '15 at 8:45











  • This function works great, however the angle of "bearingDegrees" calculation is flipped. for example, 45 degrees would typically by the 1st quadrant, however this in the 4th quadrant. 135 degrees would typically be in the 2nd quadrant but this function returns it to be in the 3rd quadrant. i can simply take the function return value x and negate that from 360 to get the correct angle value however I'm curious to know why this is happening in the first place?

    – goelv
    May 18 '15 at 10:16





















  • Note: The posted update method will not return zero degrees, but values from just above 0 to 360.0.

    – chux
    Sep 27 '13 at 2:37






  • 1





    > [How to Get angle from 2 positions][1] [1]: stackoverflow.com/questions/9457988/…

    – MAnoj Sarnaik
    Apr 8 '15 at 8:45











  • This function works great, however the angle of "bearingDegrees" calculation is flipped. for example, 45 degrees would typically by the 1st quadrant, however this in the 4th quadrant. 135 degrees would typically be in the 2nd quadrant but this function returns it to be in the 3rd quadrant. i can simply take the function return value x and negate that from 360 to get the correct angle value however I'm curious to know why this is happening in the first place?

    – goelv
    May 18 '15 at 10:16



















Note: The posted update method will not return zero degrees, but values from just above 0 to 360.0.

– chux
Sep 27 '13 at 2:37





Note: The posted update method will not return zero degrees, but values from just above 0 to 360.0.

– chux
Sep 27 '13 at 2:37




1




1





> [How to Get angle from 2 positions][1] [1]: stackoverflow.com/questions/9457988/…

– MAnoj Sarnaik
Apr 8 '15 at 8:45





> [How to Get angle from 2 positions][1] [1]: stackoverflow.com/questions/9457988/…

– MAnoj Sarnaik
Apr 8 '15 at 8:45













This function works great, however the angle of "bearingDegrees" calculation is flipped. for example, 45 degrees would typically by the 1st quadrant, however this in the 4th quadrant. 135 degrees would typically be in the 2nd quadrant but this function returns it to be in the 3rd quadrant. i can simply take the function return value x and negate that from 360 to get the correct angle value however I'm curious to know why this is happening in the first place?

– goelv
May 18 '15 at 10:16







This function works great, however the angle of "bearingDegrees" calculation is flipped. for example, 45 degrees would typically by the 1st quadrant, however this in the 4th quadrant. 135 degrees would typically be in the 2nd quadrant but this function returns it to be in the 3rd quadrant. i can simply take the function return value x and negate that from 360 to get the correct angle value however I'm curious to know why this is happening in the first place?

– goelv
May 18 '15 at 10:16














14 Answers
14






active

oldest

votes


















51














(x > 0 ? x : (2*PI + x)) * 360 / (2*PI)





share|improve this answer





















  • 2





    That's wrong, the sign of x should be positive.

    – starblue
    Aug 21 '09 at 12:56






  • 3





    Probably also want x >= 0 for the x = 0 case.

    – bpw1621
    Nov 19 '11 at 16:14






  • 10





    For those not comfortable with this notation, and without the conversion to degrees built in: if(x>0) {radians = x;} else {radians = 2*PI + x;} so we are just adding 2PI to the result if it is less than 0.

    – David Doria
    Sep 25 '12 at 19:05






  • 1





    Or (x >= 0 ? x : (2*PI + x)) * 180/PI as in (x < 0 ? 2*PI + x : x) * 180/PI

    – user3342816
    Nov 9 '14 at 9:06





















77














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.






share|improve this answer





















  • 3





    awesome solutions :)

    – Qadir Hussain
    Jun 12 '15 at 12:46






  • 5





    I don't believe it's necessary to add 360. -1 % 360 is still 359 :)

    – pleasemorebacon
    Feb 29 '16 at 0:28











  • @pleasemorebacon right you are, how silly of me!

    – Liam George Betsworth
    Mar 21 '16 at 10:58






  • 8





    I don't think this is correct in all languages. In Javascript -1 % 360 = -1

    – Startec
    Sep 14 '16 at 9:36











  • Also not a viable approach in Java

    – Hulk
    Feb 10 '17 at 16:30



















34














Just add 360° if the answer from atan2 is less than 0°.






share|improve this answer



















  • 3





    Which is the same as "just add 2 * PI" if you're having one of those days.

    – Chris O
    Oct 8 '14 at 14:43



















30














Or if you don't like branching, just negate the two parameters and add 180° to the answer.






share|improve this answer



















  • 2





    Thanks, this is just what I was looking for.

    – Jeremy Herrman
    Nov 22 '11 at 6:58






  • 2





    +1 for simple code, but not as straightforward to grok.

    – Fuhrmanator
    Apr 21 '12 at 2:14






  • 1





    I'd rather modify my code to use denormalized angles (<0, >=360) but there always seems to be someone aiming for that fake "optimized" feel; that's why I wanted to add this. (Or was it because this was the quicker way around some temporary debug code I used? hmm)

    – aib
    Apr 22 '12 at 0:26






  • 1





    Definitely not straightforward to grok, as I can concur after 2+ years. So: Adding 180° to the return value puts it nicely in the 0-360 range, but flips the angle. Negating both input parameters flips it back.

    – aib
    Nov 19 '14 at 10:57













  • This can have some issues when $x = 0$ and $y > 0$ iirc

    – Trinidad
    Feb 10 '15 at 11:44



















20














@erikkallen is close but not quite right.



theta_rad = atan2(y,x);
theta_deg = (theta_rad/M_PI*180) + (theta_rad > 0 ? 0 : 360);


This should work in C++: (depending on how fmod is implemented, it may be faster or slower than the conditional expression)



theta_deg = fmod(atan2(y,x)/M_PI*180,360);


Alternatively you could do this:



theta_deg = atan2(-y,-x)/M_PI*180 + 180;


since (x,y) and (-x,-y) differ in angles by 180 degrees.






share|improve this answer



















  • 2





    note: just realized my 3rd eqn is what @aib said.

    – Jason S
    Aug 21 '09 at 19:15











  • if I understood you correctly in Fortran it is atan2(-y,-x) * 180/PI + 180. Is that correct ?

    – gansub
    Jul 19 '16 at 16:09











  • sorry I don't know FORTRAN. But your math looks right.

    – Jason S
    Jul 20 '16 at 2:55



















6














@Jason S: your "fmod" variant will not work on a standards-compliant implementation. The C standard is explicit and clear (7.12.10.1, "the fmod functions"):




if y is nonzero, the result has the same sign as x




thus,



fmod(atan2(y,x)/M_PI*180,360)


is actually just a verbose rewriting of:



atan2(y,x)/M_PI*180


Your third suggestion, however, is spot on.






share|improve this answer































    6














    I have 2 solutions that seem to work for all combinations of positive and negative x and y.



    1) Abuse atan2()



    According to the docs atan2 takes parameters y and x in that order. However if you reverse them you can do the following:



    double radians = std::atan2(x, y);
    double degrees = radians * 180 / M_PI;
    if (radians < 0)
    {
    degrees += 360;
    }


    2) Use atan2() correctly and convert afterwards



    double degrees = std::atan2(y, x) * 180 / M_PI;
    if (degrees > 90)
    {
    degrees = 450 - degrees;
    }
    else
    {
    degrees = 90 - degrees;
    }





    share|improve this answer































      2














      This is what I normally do:



      float rads = atan2(y, x);
      if (y < 0) rads = M_PI*2.f + rads;
      float degrees = rads*180.f/M_PI;





      share|improve this answer































        1














        angle = Math.atan2(x,y)*180/Math.PI;


        I have made a Formula for orienting angle into 0 to 360



        angle + Math.ceil( -angle / 360 ) * 360;





        share|improve this answer































          1














          An alternative solution is to use the mod () function defined as:



          function mod(a, b) {return a - Math.floor (a / b) * b;}



          Then, with the following function, the angle between ini(x,y) and end(x,y) points is obtained. The angle is expressed in degrees normalized to [0, 360] deg. and North referencing 360 deg.



              function angleInDegrees(ini, end) {
          var radian = Math.atan2((end.y - ini.y), (end.x - ini.x));//radian [-PI,PI]
          return mod(radian * 180 / Math.PI + 90, 360);
          }





          share|improve this answer

































            0














            The R packages geosphere will calculate bearingRhumb, which is a constant bearing line given an origin point and easting/northing. The easting and northing must be in a matrix or vector. The origin point for a wind rose is 0,0. The following code seems to readily resolve the issue:



            windE<-wind$uasE
            windN<-wind$vasN
            wind_matrix<-cbind(windE, windN)
            wind$wind_dir<-bearingRhumb(c(0,0), wind_matrix)
            wind$wind_dir<-round(wind$wind_dir, 0)





            share|improve this answer































              0














              theta_rad = Math.Atan2(y,x);
              if(theta_rad < 0)
              theta_rad = theta_rad + 2 * Math.PI; //if neg., add 2 PI to it
              theta_deg = (theta_rad/M_PI*180) ; //convert from radian to degree

              //or
              theta_rad = Math.Atan2(y,x);
              theta_rad = (theta_rad < 0) ? theta_rad + 2 * Math.PI : theta_rad;
              theta_deg = (theta_rad/M_PI*180) ;


              -1 deg becomes (-1 + 360) = 359 deg

              -179 deg becomes (-179 + 360) = 181 deg






              share|improve this answer
























              • What's Math.PI? Is it the same as M_PI?

                – Pang
                Feb 3 '17 at 4:13



















              0














              double degree = fmodf((atan2(x, y) * (180.0 / M_PI)) + 360, 360);


              This will return degree from 0°-360° counter-clockwise, 0° is at 3 o'clock.






              share|improve this answer

































                0














                A formula to have the range of values from 0 to 360 degrees.



                f(x,y)=180-90*(1+sign(x))* (1-sign(y^2))-45*(2+sign(x))*sign(y)



                     -(180/pi())*sign(x*y)*atan((abs(x)-abs(y))/(abs(x)+abs(y)))





                share|improve this answer
























                • Can you please explain how this relates to the question?

                  – Klaus Gütter
                  Nov 25 '18 at 13:44











                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1311049%2fhow-to-map-atan2-to-degrees-0-360%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                14 Answers
                14






                active

                oldest

                votes








                14 Answers
                14






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                51














                (x > 0 ? x : (2*PI + x)) * 360 / (2*PI)





                share|improve this answer





















                • 2





                  That's wrong, the sign of x should be positive.

                  – starblue
                  Aug 21 '09 at 12:56






                • 3





                  Probably also want x >= 0 for the x = 0 case.

                  – bpw1621
                  Nov 19 '11 at 16:14






                • 10





                  For those not comfortable with this notation, and without the conversion to degrees built in: if(x>0) {radians = x;} else {radians = 2*PI + x;} so we are just adding 2PI to the result if it is less than 0.

                  – David Doria
                  Sep 25 '12 at 19:05






                • 1





                  Or (x >= 0 ? x : (2*PI + x)) * 180/PI as in (x < 0 ? 2*PI + x : x) * 180/PI

                  – user3342816
                  Nov 9 '14 at 9:06


















                51














                (x > 0 ? x : (2*PI + x)) * 360 / (2*PI)





                share|improve this answer





















                • 2





                  That's wrong, the sign of x should be positive.

                  – starblue
                  Aug 21 '09 at 12:56






                • 3





                  Probably also want x >= 0 for the x = 0 case.

                  – bpw1621
                  Nov 19 '11 at 16:14






                • 10





                  For those not comfortable with this notation, and without the conversion to degrees built in: if(x>0) {radians = x;} else {radians = 2*PI + x;} so we are just adding 2PI to the result if it is less than 0.

                  – David Doria
                  Sep 25 '12 at 19:05






                • 1





                  Or (x >= 0 ? x : (2*PI + x)) * 180/PI as in (x < 0 ? 2*PI + x : x) * 180/PI

                  – user3342816
                  Nov 9 '14 at 9:06
















                51












                51








                51







                (x > 0 ? x : (2*PI + x)) * 360 / (2*PI)





                share|improve this answer















                (x > 0 ? x : (2*PI + x)) * 360 / (2*PI)






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Oct 19 '18 at 11:55









                BartoszKP

                26.7k1066103




                26.7k1066103










                answered Aug 21 '09 at 10:20









                erikkallenerikkallen

                25.8k1271108




                25.8k1271108








                • 2





                  That's wrong, the sign of x should be positive.

                  – starblue
                  Aug 21 '09 at 12:56






                • 3





                  Probably also want x >= 0 for the x = 0 case.

                  – bpw1621
                  Nov 19 '11 at 16:14






                • 10





                  For those not comfortable with this notation, and without the conversion to degrees built in: if(x>0) {radians = x;} else {radians = 2*PI + x;} so we are just adding 2PI to the result if it is less than 0.

                  – David Doria
                  Sep 25 '12 at 19:05






                • 1





                  Or (x >= 0 ? x : (2*PI + x)) * 180/PI as in (x < 0 ? 2*PI + x : x) * 180/PI

                  – user3342816
                  Nov 9 '14 at 9:06
















                • 2





                  That's wrong, the sign of x should be positive.

                  – starblue
                  Aug 21 '09 at 12:56






                • 3





                  Probably also want x >= 0 for the x = 0 case.

                  – bpw1621
                  Nov 19 '11 at 16:14






                • 10





                  For those not comfortable with this notation, and without the conversion to degrees built in: if(x>0) {radians = x;} else {radians = 2*PI + x;} so we are just adding 2PI to the result if it is less than 0.

                  – David Doria
                  Sep 25 '12 at 19:05






                • 1





                  Or (x >= 0 ? x : (2*PI + x)) * 180/PI as in (x < 0 ? 2*PI + x : x) * 180/PI

                  – user3342816
                  Nov 9 '14 at 9:06










                2




                2





                That's wrong, the sign of x should be positive.

                – starblue
                Aug 21 '09 at 12:56





                That's wrong, the sign of x should be positive.

                – starblue
                Aug 21 '09 at 12:56




                3




                3





                Probably also want x >= 0 for the x = 0 case.

                – bpw1621
                Nov 19 '11 at 16:14





                Probably also want x >= 0 for the x = 0 case.

                – bpw1621
                Nov 19 '11 at 16:14




                10




                10





                For those not comfortable with this notation, and without the conversion to degrees built in: if(x>0) {radians = x;} else {radians = 2*PI + x;} so we are just adding 2PI to the result if it is less than 0.

                – David Doria
                Sep 25 '12 at 19:05





                For those not comfortable with this notation, and without the conversion to degrees built in: if(x>0) {radians = x;} else {radians = 2*PI + x;} so we are just adding 2PI to the result if it is less than 0.

                – David Doria
                Sep 25 '12 at 19:05




                1




                1





                Or (x >= 0 ? x : (2*PI + x)) * 180/PI as in (x < 0 ? 2*PI + x : x) * 180/PI

                – user3342816
                Nov 9 '14 at 9:06







                Or (x >= 0 ? x : (2*PI + x)) * 180/PI as in (x < 0 ? 2*PI + x : x) * 180/PI

                – user3342816
                Nov 9 '14 at 9:06















                77














                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.






                share|improve this answer





















                • 3





                  awesome solutions :)

                  – Qadir Hussain
                  Jun 12 '15 at 12:46






                • 5





                  I don't believe it's necessary to add 360. -1 % 360 is still 359 :)

                  – pleasemorebacon
                  Feb 29 '16 at 0:28











                • @pleasemorebacon right you are, how silly of me!

                  – Liam George Betsworth
                  Mar 21 '16 at 10:58






                • 8





                  I don't think this is correct in all languages. In Javascript -1 % 360 = -1

                  – Startec
                  Sep 14 '16 at 9:36











                • Also not a viable approach in Java

                  – Hulk
                  Feb 10 '17 at 16:30
















                77














                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.






                share|improve this answer





















                • 3





                  awesome solutions :)

                  – Qadir Hussain
                  Jun 12 '15 at 12:46






                • 5





                  I don't believe it's necessary to add 360. -1 % 360 is still 359 :)

                  – pleasemorebacon
                  Feb 29 '16 at 0:28











                • @pleasemorebacon right you are, how silly of me!

                  – Liam George Betsworth
                  Mar 21 '16 at 10:58






                • 8





                  I don't think this is correct in all languages. In Javascript -1 % 360 = -1

                  – Startec
                  Sep 14 '16 at 9:36











                • Also not a viable approach in Java

                  – Hulk
                  Feb 10 '17 at 16:30














                77












                77








                77







                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.






                share|improve this answer















                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.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Feb 14 '17 at 11:00

























                answered Sep 8 '14 at 13:06









                Liam George BetsworthLiam George Betsworth

                15.6k33242




                15.6k33242








                • 3





                  awesome solutions :)

                  – Qadir Hussain
                  Jun 12 '15 at 12:46






                • 5





                  I don't believe it's necessary to add 360. -1 % 360 is still 359 :)

                  – pleasemorebacon
                  Feb 29 '16 at 0:28











                • @pleasemorebacon right you are, how silly of me!

                  – Liam George Betsworth
                  Mar 21 '16 at 10:58






                • 8





                  I don't think this is correct in all languages. In Javascript -1 % 360 = -1

                  – Startec
                  Sep 14 '16 at 9:36











                • Also not a viable approach in Java

                  – Hulk
                  Feb 10 '17 at 16:30














                • 3





                  awesome solutions :)

                  – Qadir Hussain
                  Jun 12 '15 at 12:46






                • 5





                  I don't believe it's necessary to add 360. -1 % 360 is still 359 :)

                  – pleasemorebacon
                  Feb 29 '16 at 0:28











                • @pleasemorebacon right you are, how silly of me!

                  – Liam George Betsworth
                  Mar 21 '16 at 10:58






                • 8





                  I don't think this is correct in all languages. In Javascript -1 % 360 = -1

                  – Startec
                  Sep 14 '16 at 9:36











                • Also not a viable approach in Java

                  – Hulk
                  Feb 10 '17 at 16:30








                3




                3





                awesome solutions :)

                – Qadir Hussain
                Jun 12 '15 at 12:46





                awesome solutions :)

                – Qadir Hussain
                Jun 12 '15 at 12:46




                5




                5





                I don't believe it's necessary to add 360. -1 % 360 is still 359 :)

                – pleasemorebacon
                Feb 29 '16 at 0:28





                I don't believe it's necessary to add 360. -1 % 360 is still 359 :)

                – pleasemorebacon
                Feb 29 '16 at 0:28













                @pleasemorebacon right you are, how silly of me!

                – Liam George Betsworth
                Mar 21 '16 at 10:58





                @pleasemorebacon right you are, how silly of me!

                – Liam George Betsworth
                Mar 21 '16 at 10:58




                8




                8





                I don't think this is correct in all languages. In Javascript -1 % 360 = -1

                – Startec
                Sep 14 '16 at 9:36





                I don't think this is correct in all languages. In Javascript -1 % 360 = -1

                – Startec
                Sep 14 '16 at 9:36













                Also not a viable approach in Java

                – Hulk
                Feb 10 '17 at 16:30





                Also not a viable approach in Java

                – Hulk
                Feb 10 '17 at 16:30











                34














                Just add 360° if the answer from atan2 is less than 0°.






                share|improve this answer



















                • 3





                  Which is the same as "just add 2 * PI" if you're having one of those days.

                  – Chris O
                  Oct 8 '14 at 14:43
















                34














                Just add 360° if the answer from atan2 is less than 0°.






                share|improve this answer



















                • 3





                  Which is the same as "just add 2 * PI" if you're having one of those days.

                  – Chris O
                  Oct 8 '14 at 14:43














                34












                34








                34







                Just add 360° if the answer from atan2 is less than 0°.






                share|improve this answer













                Just add 360° if the answer from atan2 is less than 0°.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 21 '09 at 10:17









                dave4420dave4420

                40.6k695133




                40.6k695133








                • 3





                  Which is the same as "just add 2 * PI" if you're having one of those days.

                  – Chris O
                  Oct 8 '14 at 14:43














                • 3





                  Which is the same as "just add 2 * PI" if you're having one of those days.

                  – Chris O
                  Oct 8 '14 at 14:43








                3




                3





                Which is the same as "just add 2 * PI" if you're having one of those days.

                – Chris O
                Oct 8 '14 at 14:43





                Which is the same as "just add 2 * PI" if you're having one of those days.

                – Chris O
                Oct 8 '14 at 14:43











                30














                Or if you don't like branching, just negate the two parameters and add 180° to the answer.






                share|improve this answer



















                • 2





                  Thanks, this is just what I was looking for.

                  – Jeremy Herrman
                  Nov 22 '11 at 6:58






                • 2





                  +1 for simple code, but not as straightforward to grok.

                  – Fuhrmanator
                  Apr 21 '12 at 2:14






                • 1





                  I'd rather modify my code to use denormalized angles (<0, >=360) but there always seems to be someone aiming for that fake "optimized" feel; that's why I wanted to add this. (Or was it because this was the quicker way around some temporary debug code I used? hmm)

                  – aib
                  Apr 22 '12 at 0:26






                • 1





                  Definitely not straightforward to grok, as I can concur after 2+ years. So: Adding 180° to the return value puts it nicely in the 0-360 range, but flips the angle. Negating both input parameters flips it back.

                  – aib
                  Nov 19 '14 at 10:57













                • This can have some issues when $x = 0$ and $y > 0$ iirc

                  – Trinidad
                  Feb 10 '15 at 11:44
















                30














                Or if you don't like branching, just negate the two parameters and add 180° to the answer.






                share|improve this answer



















                • 2





                  Thanks, this is just what I was looking for.

                  – Jeremy Herrman
                  Nov 22 '11 at 6:58






                • 2





                  +1 for simple code, but not as straightforward to grok.

                  – Fuhrmanator
                  Apr 21 '12 at 2:14






                • 1





                  I'd rather modify my code to use denormalized angles (<0, >=360) but there always seems to be someone aiming for that fake "optimized" feel; that's why I wanted to add this. (Or was it because this was the quicker way around some temporary debug code I used? hmm)

                  – aib
                  Apr 22 '12 at 0:26






                • 1





                  Definitely not straightforward to grok, as I can concur after 2+ years. So: Adding 180° to the return value puts it nicely in the 0-360 range, but flips the angle. Negating both input parameters flips it back.

                  – aib
                  Nov 19 '14 at 10:57













                • This can have some issues when $x = 0$ and $y > 0$ iirc

                  – Trinidad
                  Feb 10 '15 at 11:44














                30












                30








                30







                Or if you don't like branching, just negate the two parameters and add 180° to the answer.






                share|improve this answer













                Or if you don't like branching, just negate the two parameters and add 180° to the answer.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 21 '09 at 12:08









                aibaib

                33.6k106374




                33.6k106374








                • 2





                  Thanks, this is just what I was looking for.

                  – Jeremy Herrman
                  Nov 22 '11 at 6:58






                • 2





                  +1 for simple code, but not as straightforward to grok.

                  – Fuhrmanator
                  Apr 21 '12 at 2:14






                • 1





                  I'd rather modify my code to use denormalized angles (<0, >=360) but there always seems to be someone aiming for that fake "optimized" feel; that's why I wanted to add this. (Or was it because this was the quicker way around some temporary debug code I used? hmm)

                  – aib
                  Apr 22 '12 at 0:26






                • 1





                  Definitely not straightforward to grok, as I can concur after 2+ years. So: Adding 180° to the return value puts it nicely in the 0-360 range, but flips the angle. Negating both input parameters flips it back.

                  – aib
                  Nov 19 '14 at 10:57













                • This can have some issues when $x = 0$ and $y > 0$ iirc

                  – Trinidad
                  Feb 10 '15 at 11:44














                • 2





                  Thanks, this is just what I was looking for.

                  – Jeremy Herrman
                  Nov 22 '11 at 6:58






                • 2





                  +1 for simple code, but not as straightforward to grok.

                  – Fuhrmanator
                  Apr 21 '12 at 2:14






                • 1





                  I'd rather modify my code to use denormalized angles (<0, >=360) but there always seems to be someone aiming for that fake "optimized" feel; that's why I wanted to add this. (Or was it because this was the quicker way around some temporary debug code I used? hmm)

                  – aib
                  Apr 22 '12 at 0:26






                • 1





                  Definitely not straightforward to grok, as I can concur after 2+ years. So: Adding 180° to the return value puts it nicely in the 0-360 range, but flips the angle. Negating both input parameters flips it back.

                  – aib
                  Nov 19 '14 at 10:57













                • This can have some issues when $x = 0$ and $y > 0$ iirc

                  – Trinidad
                  Feb 10 '15 at 11:44








                2




                2





                Thanks, this is just what I was looking for.

                – Jeremy Herrman
                Nov 22 '11 at 6:58





                Thanks, this is just what I was looking for.

                – Jeremy Herrman
                Nov 22 '11 at 6:58




                2




                2





                +1 for simple code, but not as straightforward to grok.

                – Fuhrmanator
                Apr 21 '12 at 2:14





                +1 for simple code, but not as straightforward to grok.

                – Fuhrmanator
                Apr 21 '12 at 2:14




                1




                1





                I'd rather modify my code to use denormalized angles (<0, >=360) but there always seems to be someone aiming for that fake "optimized" feel; that's why I wanted to add this. (Or was it because this was the quicker way around some temporary debug code I used? hmm)

                – aib
                Apr 22 '12 at 0:26





                I'd rather modify my code to use denormalized angles (<0, >=360) but there always seems to be someone aiming for that fake "optimized" feel; that's why I wanted to add this. (Or was it because this was the quicker way around some temporary debug code I used? hmm)

                – aib
                Apr 22 '12 at 0:26




                1




                1





                Definitely not straightforward to grok, as I can concur after 2+ years. So: Adding 180° to the return value puts it nicely in the 0-360 range, but flips the angle. Negating both input parameters flips it back.

                – aib
                Nov 19 '14 at 10:57







                Definitely not straightforward to grok, as I can concur after 2+ years. So: Adding 180° to the return value puts it nicely in the 0-360 range, but flips the angle. Negating both input parameters flips it back.

                – aib
                Nov 19 '14 at 10:57















                This can have some issues when $x = 0$ and $y > 0$ iirc

                – Trinidad
                Feb 10 '15 at 11:44





                This can have some issues when $x = 0$ and $y > 0$ iirc

                – Trinidad
                Feb 10 '15 at 11:44











                20














                @erikkallen is close but not quite right.



                theta_rad = atan2(y,x);
                theta_deg = (theta_rad/M_PI*180) + (theta_rad > 0 ? 0 : 360);


                This should work in C++: (depending on how fmod is implemented, it may be faster or slower than the conditional expression)



                theta_deg = fmod(atan2(y,x)/M_PI*180,360);


                Alternatively you could do this:



                theta_deg = atan2(-y,-x)/M_PI*180 + 180;


                since (x,y) and (-x,-y) differ in angles by 180 degrees.






                share|improve this answer



















                • 2





                  note: just realized my 3rd eqn is what @aib said.

                  – Jason S
                  Aug 21 '09 at 19:15











                • if I understood you correctly in Fortran it is atan2(-y,-x) * 180/PI + 180. Is that correct ?

                  – gansub
                  Jul 19 '16 at 16:09











                • sorry I don't know FORTRAN. But your math looks right.

                  – Jason S
                  Jul 20 '16 at 2:55
















                20














                @erikkallen is close but not quite right.



                theta_rad = atan2(y,x);
                theta_deg = (theta_rad/M_PI*180) + (theta_rad > 0 ? 0 : 360);


                This should work in C++: (depending on how fmod is implemented, it may be faster or slower than the conditional expression)



                theta_deg = fmod(atan2(y,x)/M_PI*180,360);


                Alternatively you could do this:



                theta_deg = atan2(-y,-x)/M_PI*180 + 180;


                since (x,y) and (-x,-y) differ in angles by 180 degrees.






                share|improve this answer



















                • 2





                  note: just realized my 3rd eqn is what @aib said.

                  – Jason S
                  Aug 21 '09 at 19:15











                • if I understood you correctly in Fortran it is atan2(-y,-x) * 180/PI + 180. Is that correct ?

                  – gansub
                  Jul 19 '16 at 16:09











                • sorry I don't know FORTRAN. But your math looks right.

                  – Jason S
                  Jul 20 '16 at 2:55














                20












                20








                20







                @erikkallen is close but not quite right.



                theta_rad = atan2(y,x);
                theta_deg = (theta_rad/M_PI*180) + (theta_rad > 0 ? 0 : 360);


                This should work in C++: (depending on how fmod is implemented, it may be faster or slower than the conditional expression)



                theta_deg = fmod(atan2(y,x)/M_PI*180,360);


                Alternatively you could do this:



                theta_deg = atan2(-y,-x)/M_PI*180 + 180;


                since (x,y) and (-x,-y) differ in angles by 180 degrees.






                share|improve this answer













                @erikkallen is close but not quite right.



                theta_rad = atan2(y,x);
                theta_deg = (theta_rad/M_PI*180) + (theta_rad > 0 ? 0 : 360);


                This should work in C++: (depending on how fmod is implemented, it may be faster or slower than the conditional expression)



                theta_deg = fmod(atan2(y,x)/M_PI*180,360);


                Alternatively you could do this:



                theta_deg = atan2(-y,-x)/M_PI*180 + 180;


                since (x,y) and (-x,-y) differ in angles by 180 degrees.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 21 '09 at 19:14









                Jason SJason S

                107k135488820




                107k135488820








                • 2





                  note: just realized my 3rd eqn is what @aib said.

                  – Jason S
                  Aug 21 '09 at 19:15











                • if I understood you correctly in Fortran it is atan2(-y,-x) * 180/PI + 180. Is that correct ?

                  – gansub
                  Jul 19 '16 at 16:09











                • sorry I don't know FORTRAN. But your math looks right.

                  – Jason S
                  Jul 20 '16 at 2:55














                • 2





                  note: just realized my 3rd eqn is what @aib said.

                  – Jason S
                  Aug 21 '09 at 19:15











                • if I understood you correctly in Fortran it is atan2(-y,-x) * 180/PI + 180. Is that correct ?

                  – gansub
                  Jul 19 '16 at 16:09











                • sorry I don't know FORTRAN. But your math looks right.

                  – Jason S
                  Jul 20 '16 at 2:55








                2




                2





                note: just realized my 3rd eqn is what @aib said.

                – Jason S
                Aug 21 '09 at 19:15





                note: just realized my 3rd eqn is what @aib said.

                – Jason S
                Aug 21 '09 at 19:15













                if I understood you correctly in Fortran it is atan2(-y,-x) * 180/PI + 180. Is that correct ?

                – gansub
                Jul 19 '16 at 16:09





                if I understood you correctly in Fortran it is atan2(-y,-x) * 180/PI + 180. Is that correct ?

                – gansub
                Jul 19 '16 at 16:09













                sorry I don't know FORTRAN. But your math looks right.

                – Jason S
                Jul 20 '16 at 2:55





                sorry I don't know FORTRAN. But your math looks right.

                – Jason S
                Jul 20 '16 at 2:55











                6














                @Jason S: your "fmod" variant will not work on a standards-compliant implementation. The C standard is explicit and clear (7.12.10.1, "the fmod functions"):




                if y is nonzero, the result has the same sign as x




                thus,



                fmod(atan2(y,x)/M_PI*180,360)


                is actually just a verbose rewriting of:



                atan2(y,x)/M_PI*180


                Your third suggestion, however, is spot on.






                share|improve this answer




























                  6














                  @Jason S: your "fmod" variant will not work on a standards-compliant implementation. The C standard is explicit and clear (7.12.10.1, "the fmod functions"):




                  if y is nonzero, the result has the same sign as x




                  thus,



                  fmod(atan2(y,x)/M_PI*180,360)


                  is actually just a verbose rewriting of:



                  atan2(y,x)/M_PI*180


                  Your third suggestion, however, is spot on.






                  share|improve this answer


























                    6












                    6








                    6







                    @Jason S: your "fmod" variant will not work on a standards-compliant implementation. The C standard is explicit and clear (7.12.10.1, "the fmod functions"):




                    if y is nonzero, the result has the same sign as x




                    thus,



                    fmod(atan2(y,x)/M_PI*180,360)


                    is actually just a verbose rewriting of:



                    atan2(y,x)/M_PI*180


                    Your third suggestion, however, is spot on.






                    share|improve this answer













                    @Jason S: your "fmod" variant will not work on a standards-compliant implementation. The C standard is explicit and clear (7.12.10.1, "the fmod functions"):




                    if y is nonzero, the result has the same sign as x




                    thus,



                    fmod(atan2(y,x)/M_PI*180,360)


                    is actually just a verbose rewriting of:



                    atan2(y,x)/M_PI*180


                    Your third suggestion, however, is spot on.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Aug 21 '09 at 20:37









                    Stephen CanonStephen Canon

                    87.8k15146241




                    87.8k15146241























                        6














                        I have 2 solutions that seem to work for all combinations of positive and negative x and y.



                        1) Abuse atan2()



                        According to the docs atan2 takes parameters y and x in that order. However if you reverse them you can do the following:



                        double radians = std::atan2(x, y);
                        double degrees = radians * 180 / M_PI;
                        if (radians < 0)
                        {
                        degrees += 360;
                        }


                        2) Use atan2() correctly and convert afterwards



                        double degrees = std::atan2(y, x) * 180 / M_PI;
                        if (degrees > 90)
                        {
                        degrees = 450 - degrees;
                        }
                        else
                        {
                        degrees = 90 - degrees;
                        }





                        share|improve this answer




























                          6














                          I have 2 solutions that seem to work for all combinations of positive and negative x and y.



                          1) Abuse atan2()



                          According to the docs atan2 takes parameters y and x in that order. However if you reverse them you can do the following:



                          double radians = std::atan2(x, y);
                          double degrees = radians * 180 / M_PI;
                          if (radians < 0)
                          {
                          degrees += 360;
                          }


                          2) Use atan2() correctly and convert afterwards



                          double degrees = std::atan2(y, x) * 180 / M_PI;
                          if (degrees > 90)
                          {
                          degrees = 450 - degrees;
                          }
                          else
                          {
                          degrees = 90 - degrees;
                          }





                          share|improve this answer


























                            6












                            6








                            6







                            I have 2 solutions that seem to work for all combinations of positive and negative x and y.



                            1) Abuse atan2()



                            According to the docs atan2 takes parameters y and x in that order. However if you reverse them you can do the following:



                            double radians = std::atan2(x, y);
                            double degrees = radians * 180 / M_PI;
                            if (radians < 0)
                            {
                            degrees += 360;
                            }


                            2) Use atan2() correctly and convert afterwards



                            double degrees = std::atan2(y, x) * 180 / M_PI;
                            if (degrees > 90)
                            {
                            degrees = 450 - degrees;
                            }
                            else
                            {
                            degrees = 90 - degrees;
                            }





                            share|improve this answer













                            I have 2 solutions that seem to work for all combinations of positive and negative x and y.



                            1) Abuse atan2()



                            According to the docs atan2 takes parameters y and x in that order. However if you reverse them you can do the following:



                            double radians = std::atan2(x, y);
                            double degrees = radians * 180 / M_PI;
                            if (radians < 0)
                            {
                            degrees += 360;
                            }


                            2) Use atan2() correctly and convert afterwards



                            double degrees = std::atan2(y, x) * 180 / M_PI;
                            if (degrees > 90)
                            {
                            degrees = 450 - degrees;
                            }
                            else
                            {
                            degrees = 90 - degrees;
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Aug 20 '14 at 6:35









                            FibblesFibbles

                            758717




                            758717























                                2














                                This is what I normally do:



                                float rads = atan2(y, x);
                                if (y < 0) rads = M_PI*2.f + rads;
                                float degrees = rads*180.f/M_PI;





                                share|improve this answer




























                                  2














                                  This is what I normally do:



                                  float rads = atan2(y, x);
                                  if (y < 0) rads = M_PI*2.f + rads;
                                  float degrees = rads*180.f/M_PI;





                                  share|improve this answer


























                                    2












                                    2








                                    2







                                    This is what I normally do:



                                    float rads = atan2(y, x);
                                    if (y < 0) rads = M_PI*2.f + rads;
                                    float degrees = rads*180.f/M_PI;





                                    share|improve this answer













                                    This is what I normally do:



                                    float rads = atan2(y, x);
                                    if (y < 0) rads = M_PI*2.f + rads;
                                    float degrees = rads*180.f/M_PI;






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Feb 6 '17 at 14:59









                                    andrésandrés

                                    136311




                                    136311























                                        1














                                        angle = Math.atan2(x,y)*180/Math.PI;


                                        I have made a Formula for orienting angle into 0 to 360



                                        angle + Math.ceil( -angle / 360 ) * 360;





                                        share|improve this answer




























                                          1














                                          angle = Math.atan2(x,y)*180/Math.PI;


                                          I have made a Formula for orienting angle into 0 to 360



                                          angle + Math.ceil( -angle / 360 ) * 360;





                                          share|improve this answer


























                                            1












                                            1








                                            1







                                            angle = Math.atan2(x,y)*180/Math.PI;


                                            I have made a Formula for orienting angle into 0 to 360



                                            angle + Math.ceil( -angle / 360 ) * 360;





                                            share|improve this answer













                                            angle = Math.atan2(x,y)*180/Math.PI;


                                            I have made a Formula for orienting angle into 0 to 360



                                            angle + Math.ceil( -angle / 360 ) * 360;






                                            share|improve this answer












                                            share|improve this answer



                                            share|improve this answer










                                            answered Feb 4 '15 at 7:58









                                            Saad AhmedSaad Ahmed

                                            1,00597




                                            1,00597























                                                1














                                                An alternative solution is to use the mod () function defined as:



                                                function mod(a, b) {return a - Math.floor (a / b) * b;}



                                                Then, with the following function, the angle between ini(x,y) and end(x,y) points is obtained. The angle is expressed in degrees normalized to [0, 360] deg. and North referencing 360 deg.



                                                    function angleInDegrees(ini, end) {
                                                var radian = Math.atan2((end.y - ini.y), (end.x - ini.x));//radian [-PI,PI]
                                                return mod(radian * 180 / Math.PI + 90, 360);
                                                }





                                                share|improve this answer






























                                                  1














                                                  An alternative solution is to use the mod () function defined as:



                                                  function mod(a, b) {return a - Math.floor (a / b) * b;}



                                                  Then, with the following function, the angle between ini(x,y) and end(x,y) points is obtained. The angle is expressed in degrees normalized to [0, 360] deg. and North referencing 360 deg.



                                                      function angleInDegrees(ini, end) {
                                                  var radian = Math.atan2((end.y - ini.y), (end.x - ini.x));//radian [-PI,PI]
                                                  return mod(radian * 180 / Math.PI + 90, 360);
                                                  }





                                                  share|improve this answer




























                                                    1












                                                    1








                                                    1







                                                    An alternative solution is to use the mod () function defined as:



                                                    function mod(a, b) {return a - Math.floor (a / b) * b;}



                                                    Then, with the following function, the angle between ini(x,y) and end(x,y) points is obtained. The angle is expressed in degrees normalized to [0, 360] deg. and North referencing 360 deg.



                                                        function angleInDegrees(ini, end) {
                                                    var radian = Math.atan2((end.y - ini.y), (end.x - ini.x));//radian [-PI,PI]
                                                    return mod(radian * 180 / Math.PI + 90, 360);
                                                    }





                                                    share|improve this answer















                                                    An alternative solution is to use the mod () function defined as:



                                                    function mod(a, b) {return a - Math.floor (a / b) * b;}



                                                    Then, with the following function, the angle between ini(x,y) and end(x,y) points is obtained. The angle is expressed in degrees normalized to [0, 360] deg. and North referencing 360 deg.



                                                        function angleInDegrees(ini, end) {
                                                    var radian = Math.atan2((end.y - ini.y), (end.x - ini.x));//radian [-PI,PI]
                                                    return mod(radian * 180 / Math.PI + 90, 360);
                                                    }






                                                    share|improve this answer














                                                    share|improve this answer



                                                    share|improve this answer








                                                    edited Feb 3 '16 at 22:01

























                                                    answered Feb 3 '16 at 21:47









                                                    A. Torrez GarayA. Torrez Garay

                                                    112




                                                    112























                                                        0














                                                        The R packages geosphere will calculate bearingRhumb, which is a constant bearing line given an origin point and easting/northing. The easting and northing must be in a matrix or vector. The origin point for a wind rose is 0,0. The following code seems to readily resolve the issue:



                                                        windE<-wind$uasE
                                                        windN<-wind$vasN
                                                        wind_matrix<-cbind(windE, windN)
                                                        wind$wind_dir<-bearingRhumb(c(0,0), wind_matrix)
                                                        wind$wind_dir<-round(wind$wind_dir, 0)





                                                        share|improve this answer




























                                                          0














                                                          The R packages geosphere will calculate bearingRhumb, which is a constant bearing line given an origin point and easting/northing. The easting and northing must be in a matrix or vector. The origin point for a wind rose is 0,0. The following code seems to readily resolve the issue:



                                                          windE<-wind$uasE
                                                          windN<-wind$vasN
                                                          wind_matrix<-cbind(windE, windN)
                                                          wind$wind_dir<-bearingRhumb(c(0,0), wind_matrix)
                                                          wind$wind_dir<-round(wind$wind_dir, 0)





                                                          share|improve this answer


























                                                            0












                                                            0








                                                            0







                                                            The R packages geosphere will calculate bearingRhumb, which is a constant bearing line given an origin point and easting/northing. The easting and northing must be in a matrix or vector. The origin point for a wind rose is 0,0. The following code seems to readily resolve the issue:



                                                            windE<-wind$uasE
                                                            windN<-wind$vasN
                                                            wind_matrix<-cbind(windE, windN)
                                                            wind$wind_dir<-bearingRhumb(c(0,0), wind_matrix)
                                                            wind$wind_dir<-round(wind$wind_dir, 0)





                                                            share|improve this answer













                                                            The R packages geosphere will calculate bearingRhumb, which is a constant bearing line given an origin point and easting/northing. The easting and northing must be in a matrix or vector. The origin point for a wind rose is 0,0. The following code seems to readily resolve the issue:



                                                            windE<-wind$uasE
                                                            windN<-wind$vasN
                                                            wind_matrix<-cbind(windE, windN)
                                                            wind$wind_dir<-bearingRhumb(c(0,0), wind_matrix)
                                                            wind$wind_dir<-round(wind$wind_dir, 0)






                                                            share|improve this answer












                                                            share|improve this answer



                                                            share|improve this answer










                                                            answered May 19 '16 at 14:32









                                                            ArianeAriane

                                                            1




                                                            1























                                                                0














                                                                theta_rad = Math.Atan2(y,x);
                                                                if(theta_rad < 0)
                                                                theta_rad = theta_rad + 2 * Math.PI; //if neg., add 2 PI to it
                                                                theta_deg = (theta_rad/M_PI*180) ; //convert from radian to degree

                                                                //or
                                                                theta_rad = Math.Atan2(y,x);
                                                                theta_rad = (theta_rad < 0) ? theta_rad + 2 * Math.PI : theta_rad;
                                                                theta_deg = (theta_rad/M_PI*180) ;


                                                                -1 deg becomes (-1 + 360) = 359 deg

                                                                -179 deg becomes (-179 + 360) = 181 deg






                                                                share|improve this answer
























                                                                • What's Math.PI? Is it the same as M_PI?

                                                                  – Pang
                                                                  Feb 3 '17 at 4:13
















                                                                0














                                                                theta_rad = Math.Atan2(y,x);
                                                                if(theta_rad < 0)
                                                                theta_rad = theta_rad + 2 * Math.PI; //if neg., add 2 PI to it
                                                                theta_deg = (theta_rad/M_PI*180) ; //convert from radian to degree

                                                                //or
                                                                theta_rad = Math.Atan2(y,x);
                                                                theta_rad = (theta_rad < 0) ? theta_rad + 2 * Math.PI : theta_rad;
                                                                theta_deg = (theta_rad/M_PI*180) ;


                                                                -1 deg becomes (-1 + 360) = 359 deg

                                                                -179 deg becomes (-179 + 360) = 181 deg






                                                                share|improve this answer
























                                                                • What's Math.PI? Is it the same as M_PI?

                                                                  – Pang
                                                                  Feb 3 '17 at 4:13














                                                                0












                                                                0








                                                                0







                                                                theta_rad = Math.Atan2(y,x);
                                                                if(theta_rad < 0)
                                                                theta_rad = theta_rad + 2 * Math.PI; //if neg., add 2 PI to it
                                                                theta_deg = (theta_rad/M_PI*180) ; //convert from radian to degree

                                                                //or
                                                                theta_rad = Math.Atan2(y,x);
                                                                theta_rad = (theta_rad < 0) ? theta_rad + 2 * Math.PI : theta_rad;
                                                                theta_deg = (theta_rad/M_PI*180) ;


                                                                -1 deg becomes (-1 + 360) = 359 deg

                                                                -179 deg becomes (-179 + 360) = 181 deg






                                                                share|improve this answer













                                                                theta_rad = Math.Atan2(y,x);
                                                                if(theta_rad < 0)
                                                                theta_rad = theta_rad + 2 * Math.PI; //if neg., add 2 PI to it
                                                                theta_deg = (theta_rad/M_PI*180) ; //convert from radian to degree

                                                                //or
                                                                theta_rad = Math.Atan2(y,x);
                                                                theta_rad = (theta_rad < 0) ? theta_rad + 2 * Math.PI : theta_rad;
                                                                theta_deg = (theta_rad/M_PI*180) ;


                                                                -1 deg becomes (-1 + 360) = 359 deg

                                                                -179 deg becomes (-179 + 360) = 181 deg







                                                                share|improve this answer












                                                                share|improve this answer



                                                                share|improve this answer










                                                                answered Feb 3 '17 at 3:56









                                                                PhilCPhilC

                                                                1




                                                                1













                                                                • What's Math.PI? Is it the same as M_PI?

                                                                  – Pang
                                                                  Feb 3 '17 at 4:13



















                                                                • What's Math.PI? Is it the same as M_PI?

                                                                  – Pang
                                                                  Feb 3 '17 at 4:13

















                                                                What's Math.PI? Is it the same as M_PI?

                                                                – Pang
                                                                Feb 3 '17 at 4:13





                                                                What's Math.PI? Is it the same as M_PI?

                                                                – Pang
                                                                Feb 3 '17 at 4:13











                                                                0














                                                                double degree = fmodf((atan2(x, y) * (180.0 / M_PI)) + 360, 360);


                                                                This will return degree from 0°-360° counter-clockwise, 0° is at 3 o'clock.






                                                                share|improve this answer






























                                                                  0














                                                                  double degree = fmodf((atan2(x, y) * (180.0 / M_PI)) + 360, 360);


                                                                  This will return degree from 0°-360° counter-clockwise, 0° is at 3 o'clock.






                                                                  share|improve this answer




























                                                                    0












                                                                    0








                                                                    0







                                                                    double degree = fmodf((atan2(x, y) * (180.0 / M_PI)) + 360, 360);


                                                                    This will return degree from 0°-360° counter-clockwise, 0° is at 3 o'clock.






                                                                    share|improve this answer















                                                                    double degree = fmodf((atan2(x, y) * (180.0 / M_PI)) + 360, 360);


                                                                    This will return degree from 0°-360° counter-clockwise, 0° is at 3 o'clock.







                                                                    share|improve this answer














                                                                    share|improve this answer



                                                                    share|improve this answer








                                                                    edited Feb 7 '17 at 0:18









                                                                    Pang

                                                                    6,9011664101




                                                                    6,9011664101










                                                                    answered Oct 9 '16 at 6:49









                                                                    FinallzFinallz

                                                                    214




                                                                    214























                                                                        0














                                                                        A formula to have the range of values from 0 to 360 degrees.



                                                                        f(x,y)=180-90*(1+sign(x))* (1-sign(y^2))-45*(2+sign(x))*sign(y)



                                                                             -(180/pi())*sign(x*y)*atan((abs(x)-abs(y))/(abs(x)+abs(y)))





                                                                        share|improve this answer
























                                                                        • Can you please explain how this relates to the question?

                                                                          – Klaus Gütter
                                                                          Nov 25 '18 at 13:44
















                                                                        0














                                                                        A formula to have the range of values from 0 to 360 degrees.



                                                                        f(x,y)=180-90*(1+sign(x))* (1-sign(y^2))-45*(2+sign(x))*sign(y)



                                                                             -(180/pi())*sign(x*y)*atan((abs(x)-abs(y))/(abs(x)+abs(y)))





                                                                        share|improve this answer
























                                                                        • Can you please explain how this relates to the question?

                                                                          – Klaus Gütter
                                                                          Nov 25 '18 at 13:44














                                                                        0












                                                                        0








                                                                        0







                                                                        A formula to have the range of values from 0 to 360 degrees.



                                                                        f(x,y)=180-90*(1+sign(x))* (1-sign(y^2))-45*(2+sign(x))*sign(y)



                                                                             -(180/pi())*sign(x*y)*atan((abs(x)-abs(y))/(abs(x)+abs(y)))





                                                                        share|improve this answer













                                                                        A formula to have the range of values from 0 to 360 degrees.



                                                                        f(x,y)=180-90*(1+sign(x))* (1-sign(y^2))-45*(2+sign(x))*sign(y)



                                                                             -(180/pi())*sign(x*y)*atan((abs(x)-abs(y))/(abs(x)+abs(y)))






                                                                        share|improve this answer












                                                                        share|improve this answer



                                                                        share|improve this answer










                                                                        answered Nov 25 '18 at 13:18









                                                                        theodore panagostheodore panagos

                                                                        1




                                                                        1













                                                                        • Can you please explain how this relates to the question?

                                                                          – Klaus Gütter
                                                                          Nov 25 '18 at 13:44



















                                                                        • Can you please explain how this relates to the question?

                                                                          – Klaus Gütter
                                                                          Nov 25 '18 at 13:44

















                                                                        Can you please explain how this relates to the question?

                                                                        – Klaus Gütter
                                                                        Nov 25 '18 at 13:44





                                                                        Can you please explain how this relates to the question?

                                                                        – Klaus Gütter
                                                                        Nov 25 '18 at 13:44


















                                                                        draft saved

                                                                        draft discarded




















































                                                                        Thanks for contributing an answer to Stack Overflow!


                                                                        • Please be sure to answer the question. Provide details and share your research!

                                                                        But avoid



                                                                        • Asking for help, clarification, or responding to other answers.

                                                                        • Making statements based on opinion; back them up with references or personal experience.


                                                                        To learn more, see our tips on writing great answers.




                                                                        draft saved


                                                                        draft discarded














                                                                        StackExchange.ready(
                                                                        function () {
                                                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1311049%2fhow-to-map-atan2-to-degrees-0-360%23new-answer', 'question_page');
                                                                        }
                                                                        );

                                                                        Post as a guest















                                                                        Required, but never shown





















































                                                                        Required, but never shown














                                                                        Required, but never shown












                                                                        Required, but never shown







                                                                        Required, but never shown

































                                                                        Required, but never shown














                                                                        Required, but never shown












                                                                        Required, but never shown







                                                                        Required, but never shown







                                                                        Popular posts from this blog

                                                                        A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                                                                        Calculate evaluation metrics using cross_val_predict sklearn

                                                                        Insert data from modal to MySQL (multiple modal on website)