-1

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

Community
  • 1
  • 1

3 Answers3

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;
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
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.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
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.

YSC
  • 38,212
  • 9
  • 96
  • 149