# How to rotate a square by an angle? [Image]

This topic is 3407 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I know a point can be rotated if its position is (0, 0), with this method: x = cos(angle)*x - sin(angle)*y y = cos(angle)*x + sin(angle)*y But what if the point I want to rotate is not at (0, 0), and how do I apply the rotation on a whole square that starts at a certain point? (For example: (3, 0), (3, 1), (2, 1), (2, 0)) Thanks for any helpers, Portishead.

##### Share on other sites
First translate the points such that the point of rotation is at (0, 0). For example, if you want to rotate around the point (2, 3) you would subtract 2 from each of the x-coordinates and 3 from each of the y coordinates. Then apply the rotation to each of the points you want to rotate and then add back in the coordinates of the center point.

##### Share on other sites
When you said each of the x\y cordinates, you meant each one of the square?

If this is the square: (3, 0) , (3, 1) , (2, 1) , (2, 0)
That would be after the subtraction: (0, 0) , (0, 1) , (-1, 1) , (-1, 0) ? Or you meant somthing different?

[Sorry for being noob, haven't learned that in math yet]

##### Share on other sites
Correct, and the proper terms would be: move the object pivot point to the origin, rotate about the origin, move the object pivot point back to where it was.
In your case the pivot point of the object is likely to be the center. So you'll move the center to the object, rotate, and move it back.

##### Share on other sites
Heya portishead(awesome band btw)

But-t-t,I think the correct equation for rotation in 2-D is

X = (cos(angle) * Radius) + XOffset
Y = (sin(angle) * Radius) + YOffset

Be careful with the angle though in most languages they use "Radians" so you may have to convert the angle from "Degrees" to "Radians".

I'm sure you actually knew this I just wanted to compliment you on your user-name.

##### Share on other sites
Yeah, it's an awesome band, wish they would come to my country. :)

I found the equation on google, so I think it's the right one..

bzroom:
So I need to do the rotation equation on the center of the square?

That's the square after the subtraction: (Subtract X: 3, Subtract Y: 0)
(0, 0) , (0, 1) , (-1, 1) , (-1, 0)
So the center is:
(-0.5, 0.5)
After the equation: (Rotating by 90 degrees)
(0 - 1*(0.5), 1*(0.5)) => (-0.5, 0.5) //The same? Oo
What's the next step now?

##### Share on other sites
You have to 'bring' the point about which you want the rotation to happen to the origin.
Then Apply the rotation and finally 'undo' your first translation.

In homogenous coordinates we would have:

$\begin{pmatrix} R_x\\ R_y\\ 1 \end{pmatrix} = \begin{pmatrix} 1 & 0 & P_x\\ 0 & 1 & P_y\\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} \cos\theta & -sin\theta & 0\\ \sin\theta & \cos\theta & 0\\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} 1 & 0 & -P_x\\ 0 & 1 & -P_y\\ 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x\\ y\\ 1 \end{pmatrix}$

Where:

(Px, Py) is the point to rotate about
(x, y) is the point you want transformed
theta is the angle of rotation
(Rx, Ry) is the point rotated

If you do the Matrix multiplications you will find a formula for Rx and Ry.

##### Share on other sites
Don't know matrices math :\
How would I rotate without it? (Using the normal formula)

##### Share on other sites
x=x*cos(angle)-y*sin(angle)
y=x*sin(angle)+x*cos(angle)

im pretty sure thats right... so thats
float newX=(x-originX)*cos(angle)-(y-originY)*sin(angle)+x;float newY=(x-originX)*sin(angle)+(y-originY)*cos(angle)+y;

just off the top of my head...

##### Share on other sites
I use complex numbers to remember the formula for 2D rotation. Multiplying by exp(angle*i) does the rotation around the origin.

(x+y*i)*exp(angle*i) = (x+yi)*(cos(angle)+sin(angle)*i) = (x*cos(angle)-y*sin(angle) + (y*cos(angle)+x*sin(angle))*i = new_x+new_y*i

So your transformation rule is:

new_x = x*cos(angle)-y*sin(angle)
new_y = y*cos(angle)+x*sin(angle)

1. 1
2. 2
3. 3
frob
12
4. 4
5. 5
Rutin
10

• 13
• 14
• 65
• 14
• 15
• ### Forum Statistics

• Total Topics
632130
• Total Posts
3004285

×