# Need help to create an equation

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?

(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)

• Are you familiar at all with matrices? – G Tony Jacobs Aug 18 '17 at 15:52
• Nope, not at all. – Blueduckraider50 Aug 18 '17 at 15:54
• I can think of possibilities using pythagorean theorem to some extent possibly if you need that ( assuming you know the pythagorean theorem). okay I guess my idea is more of a changing center thing. – user451844 Aug 18 '17 at 16:08

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),$$

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).$$