1
$\begingroup$

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

  • 0
    Two points? You mean coordinates for the point A?2017-02-26
  • 0
    Yes, i only have two points to begin with, the points for A, the points B, C and D are made from A2017-02-26
  • 0
    mvmath gives a great hint, but you may need to learn matrix multiplication if you haven't already: https://en.wikipedia.org/wiki/Matrix_multiplication don't worry it's not too difficult and the things you will be able to do with it are quite awesome.2017-02-26
  • 0
    @mathreadler I've updated the question to show what I've been doing, and it looks like what mvmath suggested, however it doesn't work as one would imagine, not sure as to why though.2017-02-26
  • 0
    Yes, the Ax,Ay needs to be removed before and put back to x and y before and after the rotation or the rotation will be around origo (0,0). mvmath has extended his hint below to account for this.2017-02-26

1 Answers 1

1

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.

  • 1
    The column vector XY is from the right. And important to note whether R is degrees or radians.2017-02-26
  • 0
    @mathreadler Yes, thank you for the correction.2017-02-26
  • 0
    I'm currently trying to use this method, however it doesn't behave like i except it to. This is a gif of how it behaves [link](https://gyazo.com/30899a78b4a4b7ed7c619796005cd600) I do not change the variables for x or y, which makes this wierd. Here is the code I'm using [link](https://gyazo.com/97d4a75f9b5baa05138e8a4da52dfb94) boid.rotate increases by 0.1 every time this is run, starting at 0. d1 is B d2 is C and d3 is D2017-02-26
  • 0
    Which kind of behaviour do you want? Did you mean rotation around centre?2017-02-26
  • 0
    @mvmath yeah, i want it to rotate with the (x, y) $A$ acting as the "origo" in a sense. The way it is now, it seems to rotate around the actual origo2017-02-26
  • 0
    @user3672215 By the way, can you show a gif of this rotation?:)2017-02-26