How to rotate a line around one of its vertexes





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am making my first raycasting engine, and would like to rotate a line over an angle θ



How does one do this? Would it be possible to show me some basic C++ code or some pseudocode?



This image describes my problem:
enter image description here





Optional question



I decided to make all of this in graphics.h, because it is the simplest graphics header for C/C++.










share|improve this question

























  • Also interested, because cross product works only in 3 dimensions

    – ishidex2
    Jan 3 at 10:43











  • Link doesn't work, I get the image cannot be displayed.

    – SPlatten
    Jan 3 at 10:43











  • It works for me :/

    – ishidex2
    Jan 3 at 10:45











  • If you are doing linear algebra, pick a linear algebra library and express it using values of appropriate types. Then you can write code that looks like the maths B = P + M * (A - P); can be valid C++

    – Caleth
    Jan 3 at 12:08











  • This website can be of help to you: geomalgorithms.com

    – kvantour
    Jan 3 at 12:08


















0















I am making my first raycasting engine, and would like to rotate a line over an angle θ



How does one do this? Would it be possible to show me some basic C++ code or some pseudocode?



This image describes my problem:
enter image description here





Optional question



I decided to make all of this in graphics.h, because it is the simplest graphics header for C/C++.










share|improve this question

























  • Also interested, because cross product works only in 3 dimensions

    – ishidex2
    Jan 3 at 10:43











  • Link doesn't work, I get the image cannot be displayed.

    – SPlatten
    Jan 3 at 10:43











  • It works for me :/

    – ishidex2
    Jan 3 at 10:45











  • If you are doing linear algebra, pick a linear algebra library and express it using values of appropriate types. Then you can write code that looks like the maths B = P + M * (A - P); can be valid C++

    – Caleth
    Jan 3 at 12:08











  • This website can be of help to you: geomalgorithms.com

    – kvantour
    Jan 3 at 12:08














0












0








0


2






I am making my first raycasting engine, and would like to rotate a line over an angle θ



How does one do this? Would it be possible to show me some basic C++ code or some pseudocode?



This image describes my problem:
enter image description here





Optional question



I decided to make all of this in graphics.h, because it is the simplest graphics header for C/C++.










share|improve this question
















I am making my first raycasting engine, and would like to rotate a line over an angle θ



How does one do this? Would it be possible to show me some basic C++ code or some pseudocode?



This image describes my problem:
enter image description here





Optional question



I decided to make all of this in graphics.h, because it is the simplest graphics header for C/C++.







c++ algorithm geometry raycasting cartesian-coordinates






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 12:08









kvantour

10.8k41734




10.8k41734










asked Jan 3 at 10:40









Sir.RosticcianoSir.Rosticciano

106




106













  • Also interested, because cross product works only in 3 dimensions

    – ishidex2
    Jan 3 at 10:43











  • Link doesn't work, I get the image cannot be displayed.

    – SPlatten
    Jan 3 at 10:43











  • It works for me :/

    – ishidex2
    Jan 3 at 10:45











  • If you are doing linear algebra, pick a linear algebra library and express it using values of appropriate types. Then you can write code that looks like the maths B = P + M * (A - P); can be valid C++

    – Caleth
    Jan 3 at 12:08











  • This website can be of help to you: geomalgorithms.com

    – kvantour
    Jan 3 at 12:08



















  • Also interested, because cross product works only in 3 dimensions

    – ishidex2
    Jan 3 at 10:43











  • Link doesn't work, I get the image cannot be displayed.

    – SPlatten
    Jan 3 at 10:43











  • It works for me :/

    – ishidex2
    Jan 3 at 10:45











  • If you are doing linear algebra, pick a linear algebra library and express it using values of appropriate types. Then you can write code that looks like the maths B = P + M * (A - P); can be valid C++

    – Caleth
    Jan 3 at 12:08











  • This website can be of help to you: geomalgorithms.com

    – kvantour
    Jan 3 at 12:08

















Also interested, because cross product works only in 3 dimensions

– ishidex2
Jan 3 at 10:43





Also interested, because cross product works only in 3 dimensions

– ishidex2
Jan 3 at 10:43













Link doesn't work, I get the image cannot be displayed.

– SPlatten
Jan 3 at 10:43





Link doesn't work, I get the image cannot be displayed.

– SPlatten
Jan 3 at 10:43













It works for me :/

– ishidex2
Jan 3 at 10:45





It works for me :/

– ishidex2
Jan 3 at 10:45













If you are doing linear algebra, pick a linear algebra library and express it using values of appropriate types. Then you can write code that looks like the maths B = P + M * (A - P); can be valid C++

– Caleth
Jan 3 at 12:08





If you are doing linear algebra, pick a linear algebra library and express it using values of appropriate types. Then you can write code that looks like the maths B = P + M * (A - P); can be valid C++

– Caleth
Jan 3 at 12:08













This website can be of help to you: geomalgorithms.com

– kvantour
Jan 3 at 12:08





This website can be of help to you: geomalgorithms.com

– kvantour
Jan 3 at 12:08












3 Answers
3






active

oldest

votes


















4














You want:



B = P + M * (A - P)


Where M is a 2D rotation matrix:



M = |  cos(ϴ)  -sin(ϴ) |
| sin(ϴ) cos(ϴ) |


In C++ it could be written as:



float c = cos(theta), s = sin(theta);
float dx = ax - px, dy = ay - py;
float bx = px + c * dx - s * dy;
float by = py + s * dx + c * dy;





share|improve this answer































    2














    One simple algorithm:




    1. Move the circle -P, so that P is at (0, 0).

    2. Rotate A by the angle by multiplying it by the rotation matrix.

    3. Move the circle P to restore its original position.


    All these three steps can be done using one 3x3 matrix multiplication.






    share|improve this answer































      2














      The scalar product of two vectors have the following property:



      vec(PA) . vec(PB) = rho cos theta


      Taking the definition of our two vectors:



      vec(PA) = (x_a-x_p, y_a-y_p)
      vec(PB) = (x_b-x_p, y_b-y_p)


      We can get:



      (x_a-x_p)(x_b-x_p) + (y_a-y_p)(y_b-y_p) = rho cos theta (1)


      Since PA=PB, we also have:



      (x_a-x_p)^2 + (y_a-y_p)^2 = (x_b-x_p)^2 + (y_b-y_p)^2 (2)


      From (1) and (2) you can derive x_band y_b with some arithmetic autopilot.






      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%2f54020650%2fhow-to-rotate-a-line-around-one-of-its-vertexes%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        4














        You want:



        B = P + M * (A - P)


        Where M is a 2D rotation matrix:



        M = |  cos(ϴ)  -sin(ϴ) |
        | sin(ϴ) cos(ϴ) |


        In C++ it could be written as:



        float c = cos(theta), s = sin(theta);
        float dx = ax - px, dy = ay - py;
        float bx = px + c * dx - s * dy;
        float by = py + s * dx + c * dy;





        share|improve this answer




























          4














          You want:



          B = P + M * (A - P)


          Where M is a 2D rotation matrix:



          M = |  cos(ϴ)  -sin(ϴ) |
          | sin(ϴ) cos(ϴ) |


          In C++ it could be written as:



          float c = cos(theta), s = sin(theta);
          float dx = ax - px, dy = ay - py;
          float bx = px + c * dx - s * dy;
          float by = py + s * dx + c * dy;





          share|improve this answer


























            4












            4








            4







            You want:



            B = P + M * (A - P)


            Where M is a 2D rotation matrix:



            M = |  cos(ϴ)  -sin(ϴ) |
            | sin(ϴ) cos(ϴ) |


            In C++ it could be written as:



            float c = cos(theta), s = sin(theta);
            float dx = ax - px, dy = ay - py;
            float bx = px + c * dx - s * dy;
            float by = py + s * dx + c * dy;





            share|improve this answer













            You want:



            B = P + M * (A - P)


            Where M is a 2D rotation matrix:



            M = |  cos(ϴ)  -sin(ϴ) |
            | sin(ϴ) cos(ϴ) |


            In C++ it could be written as:



            float c = cos(theta), s = sin(theta);
            float dx = ax - px, dy = ay - py;
            float bx = px + c * dx - s * dy;
            float by = py + s * dx + c * dy;






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 3 at 10:53









            HolyBlackCatHolyBlackCat

            17.2k33568




            17.2k33568

























                2














                One simple algorithm:




                1. Move the circle -P, so that P is at (0, 0).

                2. Rotate A by the angle by multiplying it by the rotation matrix.

                3. Move the circle P to restore its original position.


                All these three steps can be done using one 3x3 matrix multiplication.






                share|improve this answer




























                  2














                  One simple algorithm:




                  1. Move the circle -P, so that P is at (0, 0).

                  2. Rotate A by the angle by multiplying it by the rotation matrix.

                  3. Move the circle P to restore its original position.


                  All these three steps can be done using one 3x3 matrix multiplication.






                  share|improve this answer


























                    2












                    2








                    2







                    One simple algorithm:




                    1. Move the circle -P, so that P is at (0, 0).

                    2. Rotate A by the angle by multiplying it by the rotation matrix.

                    3. Move the circle P to restore its original position.


                    All these three steps can be done using one 3x3 matrix multiplication.






                    share|improve this answer













                    One simple algorithm:




                    1. Move the circle -P, so that P is at (0, 0).

                    2. Rotate A by the angle by multiplying it by the rotation matrix.

                    3. Move the circle P to restore its original position.


                    All these three steps can be done using one 3x3 matrix multiplication.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 3 at 10:51









                    Maxim EgorushkinMaxim Egorushkin

                    90.1k11104192




                    90.1k11104192























                        2














                        The scalar product of two vectors have the following property:



                        vec(PA) . vec(PB) = rho cos theta


                        Taking the definition of our two vectors:



                        vec(PA) = (x_a-x_p, y_a-y_p)
                        vec(PB) = (x_b-x_p, y_b-y_p)


                        We can get:



                        (x_a-x_p)(x_b-x_p) + (y_a-y_p)(y_b-y_p) = rho cos theta (1)


                        Since PA=PB, we also have:



                        (x_a-x_p)^2 + (y_a-y_p)^2 = (x_b-x_p)^2 + (y_b-y_p)^2 (2)


                        From (1) and (2) you can derive x_band y_b with some arithmetic autopilot.






                        share|improve this answer






























                          2














                          The scalar product of two vectors have the following property:



                          vec(PA) . vec(PB) = rho cos theta


                          Taking the definition of our two vectors:



                          vec(PA) = (x_a-x_p, y_a-y_p)
                          vec(PB) = (x_b-x_p, y_b-y_p)


                          We can get:



                          (x_a-x_p)(x_b-x_p) + (y_a-y_p)(y_b-y_p) = rho cos theta (1)


                          Since PA=PB, we also have:



                          (x_a-x_p)^2 + (y_a-y_p)^2 = (x_b-x_p)^2 + (y_b-y_p)^2 (2)


                          From (1) and (2) you can derive x_band y_b with some arithmetic autopilot.






                          share|improve this answer




























                            2












                            2








                            2







                            The scalar product of two vectors have the following property:



                            vec(PA) . vec(PB) = rho cos theta


                            Taking the definition of our two vectors:



                            vec(PA) = (x_a-x_p, y_a-y_p)
                            vec(PB) = (x_b-x_p, y_b-y_p)


                            We can get:



                            (x_a-x_p)(x_b-x_p) + (y_a-y_p)(y_b-y_p) = rho cos theta (1)


                            Since PA=PB, we also have:



                            (x_a-x_p)^2 + (y_a-y_p)^2 = (x_b-x_p)^2 + (y_b-y_p)^2 (2)


                            From (1) and (2) you can derive x_band y_b with some arithmetic autopilot.






                            share|improve this answer















                            The scalar product of two vectors have the following property:



                            vec(PA) . vec(PB) = rho cos theta


                            Taking the definition of our two vectors:



                            vec(PA) = (x_a-x_p, y_a-y_p)
                            vec(PB) = (x_b-x_p, y_b-y_p)


                            We can get:



                            (x_a-x_p)(x_b-x_p) + (y_a-y_p)(y_b-y_p) = rho cos theta (1)


                            Since PA=PB, we also have:



                            (x_a-x_p)^2 + (y_a-y_p)^2 = (x_b-x_p)^2 + (y_b-y_p)^2 (2)


                            From (1) and (2) you can derive x_band y_b with some arithmetic autopilot.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jan 3 at 13:42

























                            answered Jan 3 at 10:57









                            YSCYSC

                            25.7k657112




                            25.7k657112






























                                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%2f54020650%2fhow-to-rotate-a-line-around-one-of-its-vertexes%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