Search code examples
c++algorithmgeometryraycastingcartesian-coordinates

How to rotate a line around one of its vertexes


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++.


Solution

  • 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;