Need help to create an equation

58 Views Asked by At

I am doing some programming, and I hit a bump. I got a square that I can rotate at will. I got an angle from $0$ to $360$ The square is a fixed size, let's say 3.

Now I need to find the position of the 4 corners. This is "relatively" easy to do when the square is at a fixed angle, as I just need to plus on the $X$ & $Y$ axis, half the width & half the height, to get to a corner. The difficult part for me is to do the same thing while the square is rotated.

Say at $45$degrees I can't figure out the position of the "top left corner" anymore. Is there a simple equation I could use/create that would use say -1.5, 1.5 (on either or both XY axis) that would give me the position $(XY)$ of one of the corners?

Thanks in advance Concerned mathdoer

(Note: I am using this in a script, to spawn objects in a square with a fixed rotation and size, from 1 to 9, from top left to bottom right in orderly fashion)

1

There are 1 best solutions below

0
On BEST ANSWER

If the center of the square is at $(0,0)$, and one of the corners is originally at $(x,y)$, then after rotating through an angle of $\theta$ counterclockwise, that angle's position is given by:

$$\left[\begin{matrix}\cos \theta & -\sin\theta \\ \sin\theta & \cos\theta \end{matrix}\right]\left[\begin{matrix} x \\ y \end{matrix}\right],$$

Unpacking that, since you haven't worked with matrices, you get:

$$(x\cos\theta - y\sin\theta, x\sin\theta+y\cos\theta),$$

as your new coordinates.

If the center of your square is originally at $(h,k)$, and your corner is originally at $(x,y)$, then your new location is:

$$(h+ (x-h)\cos\theta - (y-k)\sin\theta, k+(x-h)\sin\theta + (y-k)\cos\theta).$$