Rotating a triangle around $(x, y)$

118 Views Asked by At

I have two coordinates $(x, y)$ lets call this point $A$. The triangle is drawn between point $B$, $C$ and $D$ which have the following values

$B = (A_x - 5, A_y)$

$C = (A_x + 5, A_y)$

$D = (A_x, A_y + 25)$

I also have a variable $R$ which stands for rotation, it will be from $0-180$ or $0-360$ whichever you prefer to work with.

How do I rotate the triangle $\triangle BCD$ a degree of $R$ ?

Edit:

My current attempt to create something like this, looks like this

sinr = sin(R*3.14/180.0)
cosr = cos(R*3.14/180.0)

B = int((x + 5)*cosr - y*sinr), int((x + 5)*sinr + y*cosr)
C = int((x - 5)*cosr - y*sinr), int((x - 5)*sinr + y*cosr)
D = int(x*cosr - (y - 25)*sinr), int(x*sinr + (y - 25)*cosr)

However it leads to the following: link

Note: the $X$ and $ Y $ value does not change in this example

1

There are 1 best solutions below

6
On BEST ANSWER

Hint. You can use rotation matrix: $$ \begin{bmatrix} \cos R & -\sin R\\ \sin R & \cos R\\ \end{bmatrix} \begin{bmatrix} \ X\\ \ Y\\ \end{bmatrix} $$ Calculate it for every point. P.S. Note, that here we use radians, so you should use transformation formula.

UPD: I think, this will help you: $$X = x_0 + (x - x_0) * \cos(R) - (y - y_0) * \sin(R)\\ Y = y_0 + (y - y_0) * \cos(R) + (x - x_0) * \sin(R)$$ Where $(x_0, y_0)$ are coordinates of "origin" around which you want to rotate.