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












0















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



























    0















    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

























      0












      0








      0








      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 '18 at 14:09









      ToonWKToonWK

      2818




      2818
























          2 Answers
          2






          active

          oldest

          votes


















          1














          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 '18 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 '18 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 '18 at 18:51





















          1














          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',
            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%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









            1














            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 '18 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 '18 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 '18 at 18:51


















            1














            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 '18 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 '18 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 '18 at 18:51
















            1












            1








            1







            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 '18 at 14:35

























            answered Nov 21 '18 at 14:14









            spenderspender

            86.7k21159280




            86.7k21159280













            • 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 18:51















            1














            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




























              1














              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


























                1












                1








                1







                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 '18 at 14:15









                T. LinnellT. Linnell

                22424




                22424






























                    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%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

                    MongoDB - Not Authorized To Execute Command

                    How to fix TextFormField cause rebuild widget in Flutter

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith