How can I find a circle angle from x and y axis











up vote
0
down vote

favorite












Feel free to mark my question duplicated. Because I know absolute nothing about COS, SIN, and TAN and someone else probably already ask this question.



So, I was try to set the circular progress bar based on x and y axis that can get from gamepad input. The progress bar put it simple is just a Minimum of 0 and maximum of 360.



I did try to search a bit, but my best understanding is that it work with only 180 degree and positive x and y. But the input I get from the controller is and y from -1 to 1 (where x -1 is left and 1 is right, y -1 is bottom and 1 is top)



Here is my code so far.



var controller = Windows.Gaming.Input.Gamepad.Gamepads[0].GetCurrentReading();
x = controller.LeftThumbstickX
y = controller.LeftThumbstickY
//what do I have to do from here?
progress.Value = angle; //?









share|improve this question


























    up vote
    0
    down vote

    favorite












    Feel free to mark my question duplicated. Because I know absolute nothing about COS, SIN, and TAN and someone else probably already ask this question.



    So, I was try to set the circular progress bar based on x and y axis that can get from gamepad input. The progress bar put it simple is just a Minimum of 0 and maximum of 360.



    I did try to search a bit, but my best understanding is that it work with only 180 degree and positive x and y. But the input I get from the controller is and y from -1 to 1 (where x -1 is left and 1 is right, y -1 is bottom and 1 is top)



    Here is my code so far.



    var controller = Windows.Gaming.Input.Gamepad.Gamepads[0].GetCurrentReading();
    x = controller.LeftThumbstickX
    y = controller.LeftThumbstickY
    //what do I have to do from here?
    progress.Value = angle; //?









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Feel free to mark my question duplicated. Because I know absolute nothing about COS, SIN, and TAN and someone else probably already ask this question.



      So, I was try to set the circular progress bar based on x and y axis that can get from gamepad input. The progress bar put it simple is just a Minimum of 0 and maximum of 360.



      I did try to search a bit, but my best understanding is that it work with only 180 degree and positive x and y. But the input I get from the controller is and y from -1 to 1 (where x -1 is left and 1 is right, y -1 is bottom and 1 is top)



      Here is my code so far.



      var controller = Windows.Gaming.Input.Gamepad.Gamepads[0].GetCurrentReading();
      x = controller.LeftThumbstickX
      y = controller.LeftThumbstickY
      //what do I have to do from here?
      progress.Value = angle; //?









      share|improve this question













      Feel free to mark my question duplicated. Because I know absolute nothing about COS, SIN, and TAN and someone else probably already ask this question.



      So, I was try to set the circular progress bar based on x and y axis that can get from gamepad input. The progress bar put it simple is just a Minimum of 0 and maximum of 360.



      I did try to search a bit, but my best understanding is that it work with only 180 degree and positive x and y. But the input I get from the controller is and y from -1 to 1 (where x -1 is left and 1 is right, y -1 is bottom and 1 is top)



      Here is my code so far.



      var controller = Windows.Gaming.Input.Gamepad.Gamepads[0].GetCurrentReading();
      x = controller.LeftThumbstickX
      y = controller.LeftThumbstickY
      //what do I have to do from here?
      progress.Value = angle; //?






      c# windows math uwp trigonometry






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 at 14:09









      ToonWK

      268




      268
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          The trigonometric function atan2 is the tool for this job. In C#, this is implemented by Math.Atan2 :



          double angleInRadians = Math.Atan2(y, x);
          double angleInDegrees = (180 / Math.PI) * angleInRadians;


          Using this formula with (for instance) parameters (1,1), you'll get a result of 45.



          However, in terms of polar alignment, this angle measures anti-clockwise from "east". To convert this to an angle that measures clockwise from "north":



          double compassRadians = Math.PI / 2 - angleInRadians;
          double compassDegrees = (180 / Math.PI) * compassRadians;


          but now we may encounter negative values, so we can normalize them with the following method:



          double normalizeDegrees(double a) => ((a % 360) + 360) % 360; //convert to 0-360


          then



          var compassAngle = normalizeDegrees(compassDegrees);





          share|improve this answer























          • Well that was very insightful answer. Thank you. But I was tested this right now, and it default (0,0) to 90 degree. Even though, I wanted it to be 0. Should I just add if (x == 0 && y == 0) return 0; or is there something in that formula that can change?
            – ToonWK
            Nov 21 at 15:52










          • @ToonWK Yes, I think you'll have to make a special case for 0,0 as the maths probably breaks down at 0.
            – spender
            Nov 21 at 18:44










          • @ToonWK Btw, you can avoid some of the maths here by flipping the parameters to atan2 with double compassRadians = Math.Atan2(x,y);
            – spender
            Nov 21 at 18:51




















          up vote
          1
          down vote













          The method you want is Math.Atan2. This takes two arguments - the y-value first, then the x-value - and it gives you an angle in radians.



          Since you want an angle in degrees, you'll need to convert - the conversion factor is 180 / Math.PI. So you'll be using something like:



          var radiansToDegrees = 180 / Math.PI;
          progress.Value = Math.Atan2(y,x) * radiansToDegrees;


          Depending exactly what combination of x and y needs to correspond to 0 you might need to add a number of degrees on afterwards. This as-is will give you 0 degrees for x = 1, y = 0, and 90 degrees for x = 0, y = 1, etc.






          share|improve this answer





















            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',
            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%2f53413924%2fhow-can-i-find-a-circle-angle-from-x-and-y-axis%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            The trigonometric function atan2 is the tool for this job. In C#, this is implemented by Math.Atan2 :



            double angleInRadians = Math.Atan2(y, x);
            double angleInDegrees = (180 / Math.PI) * angleInRadians;


            Using this formula with (for instance) parameters (1,1), you'll get a result of 45.



            However, in terms of polar alignment, this angle measures anti-clockwise from "east". To convert this to an angle that measures clockwise from "north":



            double compassRadians = Math.PI / 2 - angleInRadians;
            double compassDegrees = (180 / Math.PI) * compassRadians;


            but now we may encounter negative values, so we can normalize them with the following method:



            double normalizeDegrees(double a) => ((a % 360) + 360) % 360; //convert to 0-360


            then



            var compassAngle = normalizeDegrees(compassDegrees);





            share|improve this answer























            • Well that was very insightful answer. Thank you. But I was tested this right now, and it default (0,0) to 90 degree. Even though, I wanted it to be 0. Should I just add if (x == 0 && y == 0) return 0; or is there something in that formula that can change?
              – ToonWK
              Nov 21 at 15:52










            • @ToonWK Yes, I think you'll have to make a special case for 0,0 as the maths probably breaks down at 0.
              – spender
              Nov 21 at 18:44










            • @ToonWK Btw, you can avoid some of the maths here by flipping the parameters to atan2 with double compassRadians = Math.Atan2(x,y);
              – spender
              Nov 21 at 18:51

















            up vote
            1
            down vote



            accepted










            The trigonometric function atan2 is the tool for this job. In C#, this is implemented by Math.Atan2 :



            double angleInRadians = Math.Atan2(y, x);
            double angleInDegrees = (180 / Math.PI) * angleInRadians;


            Using this formula with (for instance) parameters (1,1), you'll get a result of 45.



            However, in terms of polar alignment, this angle measures anti-clockwise from "east". To convert this to an angle that measures clockwise from "north":



            double compassRadians = Math.PI / 2 - angleInRadians;
            double compassDegrees = (180 / Math.PI) * compassRadians;


            but now we may encounter negative values, so we can normalize them with the following method:



            double normalizeDegrees(double a) => ((a % 360) + 360) % 360; //convert to 0-360


            then



            var compassAngle = normalizeDegrees(compassDegrees);





            share|improve this answer























            • Well that was very insightful answer. Thank you. But I was tested this right now, and it default (0,0) to 90 degree. Even though, I wanted it to be 0. Should I just add if (x == 0 && y == 0) return 0; or is there something in that formula that can change?
              – ToonWK
              Nov 21 at 15:52










            • @ToonWK Yes, I think you'll have to make a special case for 0,0 as the maths probably breaks down at 0.
              – spender
              Nov 21 at 18:44










            • @ToonWK Btw, you can avoid some of the maths here by flipping the parameters to atan2 with double compassRadians = Math.Atan2(x,y);
              – spender
              Nov 21 at 18:51















            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            The trigonometric function atan2 is the tool for this job. In C#, this is implemented by Math.Atan2 :



            double angleInRadians = Math.Atan2(y, x);
            double angleInDegrees = (180 / Math.PI) * angleInRadians;


            Using this formula with (for instance) parameters (1,1), you'll get a result of 45.



            However, in terms of polar alignment, this angle measures anti-clockwise from "east". To convert this to an angle that measures clockwise from "north":



            double compassRadians = Math.PI / 2 - angleInRadians;
            double compassDegrees = (180 / Math.PI) * compassRadians;


            but now we may encounter negative values, so we can normalize them with the following method:



            double normalizeDegrees(double a) => ((a % 360) + 360) % 360; //convert to 0-360


            then



            var compassAngle = normalizeDegrees(compassDegrees);





            share|improve this answer














            The trigonometric function atan2 is the tool for this job. In C#, this is implemented by Math.Atan2 :



            double angleInRadians = Math.Atan2(y, x);
            double angleInDegrees = (180 / Math.PI) * angleInRadians;


            Using this formula with (for instance) parameters (1,1), you'll get a result of 45.



            However, in terms of polar alignment, this angle measures anti-clockwise from "east". To convert this to an angle that measures clockwise from "north":



            double compassRadians = Math.PI / 2 - angleInRadians;
            double compassDegrees = (180 / Math.PI) * compassRadians;


            but now we may encounter negative values, so we can normalize them with the following method:



            double normalizeDegrees(double a) => ((a % 360) + 360) % 360; //convert to 0-360


            then



            var compassAngle = normalizeDegrees(compassDegrees);






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 21 at 14:35

























            answered Nov 21 at 14:14









            spender

            85.8k21157276




            85.8k21157276












            • Well that was very insightful answer. Thank you. But I was tested this right now, and it default (0,0) to 90 degree. Even though, I wanted it to be 0. Should I just add if (x == 0 && y == 0) return 0; or is there something in that formula that can change?
              – ToonWK
              Nov 21 at 15:52










            • @ToonWK Yes, I think you'll have to make a special case for 0,0 as the maths probably breaks down at 0.
              – spender
              Nov 21 at 18:44










            • @ToonWK Btw, you can avoid some of the maths here by flipping the parameters to atan2 with double compassRadians = Math.Atan2(x,y);
              – spender
              Nov 21 at 18:51




















            • Well that was very insightful answer. Thank you. But I was tested this right now, and it default (0,0) to 90 degree. Even though, I wanted it to be 0. Should I just add if (x == 0 && y == 0) return 0; or is there something in that formula that can change?
              – ToonWK
              Nov 21 at 15:52










            • @ToonWK Yes, I think you'll have to make a special case for 0,0 as the maths probably breaks down at 0.
              – spender
              Nov 21 at 18:44










            • @ToonWK Btw, you can avoid some of the maths here by flipping the parameters to atan2 with double compassRadians = Math.Atan2(x,y);
              – spender
              Nov 21 at 18:51


















            Well that was very insightful answer. Thank you. But I was tested this right now, and it default (0,0) to 90 degree. Even though, I wanted it to be 0. Should I just add if (x == 0 && y == 0) return 0; or is there something in that formula that can change?
            – ToonWK
            Nov 21 at 15:52




            Well that was very insightful answer. Thank you. But I was tested this right now, and it default (0,0) to 90 degree. Even though, I wanted it to be 0. Should I just add if (x == 0 && y == 0) return 0; or is there something in that formula that can change?
            – ToonWK
            Nov 21 at 15:52












            @ToonWK Yes, I think you'll have to make a special case for 0,0 as the maths probably breaks down at 0.
            – spender
            Nov 21 at 18:44




            @ToonWK Yes, I think you'll have to make a special case for 0,0 as the maths probably breaks down at 0.
            – spender
            Nov 21 at 18:44












            @ToonWK Btw, you can avoid some of the maths here by flipping the parameters to atan2 with double compassRadians = Math.Atan2(x,y);
            – spender
            Nov 21 at 18:51






            @ToonWK Btw, you can avoid some of the maths here by flipping the parameters to atan2 with double compassRadians = Math.Atan2(x,y);
            – spender
            Nov 21 at 18:51














            up vote
            1
            down vote













            The method you want is Math.Atan2. This takes two arguments - the y-value first, then the x-value - and it gives you an angle in radians.



            Since you want an angle in degrees, you'll need to convert - the conversion factor is 180 / Math.PI. So you'll be using something like:



            var radiansToDegrees = 180 / Math.PI;
            progress.Value = Math.Atan2(y,x) * radiansToDegrees;


            Depending exactly what combination of x and y needs to correspond to 0 you might need to add a number of degrees on afterwards. This as-is will give you 0 degrees for x = 1, y = 0, and 90 degrees for x = 0, y = 1, etc.






            share|improve this answer

























              up vote
              1
              down vote













              The method you want is Math.Atan2. This takes two arguments - the y-value first, then the x-value - and it gives you an angle in radians.



              Since you want an angle in degrees, you'll need to convert - the conversion factor is 180 / Math.PI. So you'll be using something like:



              var radiansToDegrees = 180 / Math.PI;
              progress.Value = Math.Atan2(y,x) * radiansToDegrees;


              Depending exactly what combination of x and y needs to correspond to 0 you might need to add a number of degrees on afterwards. This as-is will give you 0 degrees for x = 1, y = 0, and 90 degrees for x = 0, y = 1, etc.






              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                The method you want is Math.Atan2. This takes two arguments - the y-value first, then the x-value - and it gives you an angle in radians.



                Since you want an angle in degrees, you'll need to convert - the conversion factor is 180 / Math.PI. So you'll be using something like:



                var radiansToDegrees = 180 / Math.PI;
                progress.Value = Math.Atan2(y,x) * radiansToDegrees;


                Depending exactly what combination of x and y needs to correspond to 0 you might need to add a number of degrees on afterwards. This as-is will give you 0 degrees for x = 1, y = 0, and 90 degrees for x = 0, y = 1, etc.






                share|improve this answer












                The method you want is Math.Atan2. This takes two arguments - the y-value first, then the x-value - and it gives you an angle in radians.



                Since you want an angle in degrees, you'll need to convert - the conversion factor is 180 / Math.PI. So you'll be using something like:



                var radiansToDegrees = 180 / Math.PI;
                progress.Value = Math.Atan2(y,x) * radiansToDegrees;


                Depending exactly what combination of x and y needs to correspond to 0 you might need to add a number of degrees on afterwards. This as-is will give you 0 degrees for x = 1, y = 0, and 90 degrees for x = 0, y = 1, etc.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 at 14:15









                T. Linnell

                22514




                22514






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f53413924%2fhow-can-i-find-a-circle-angle-from-x-and-y-axis%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

                    Berounka

                    Different font size/position of beamer's navigation symbols template's content depending on regular/plain...

                    Sphinx de Gizeh