1. ## Coordinate rotation

Hi all,

I have a hexagon which looks like this:

The red and blue parts signify doors, which are defined by a point in the middle of the door. The center of the hexagon is at (0, 0), and the blue door is at (2207, -3751). What I need to calculate is what happens to all door coordinates when I rotate the hexagon 30 degrees clockwise around its center.

From my manual calculations, the blue door should wind up at (3751, -2207), but is there a formula I can use which holds true even if the shape is different, for example square or octagon?

(This is for a programming task, by the way, not part of any math class. I hope I've put it in the right place, otherwise please move it where it belongs.)

Best regards,
Lizzan

2. ...but is there a formula I can use which holds true even if the shape is different, for example square or octagon?
There sure is. You can use rotation matrices to solve this problem. If you have a vector

$\vec{r}=\begin{bmatrix}x\\y\end{bmatrix},$ (which is exactly the same sort of idea as your (x,y) coordinates) from the origin to point $P$, then if you rotate all the points (e.g., rotating the hexagaon) counterclockwise through an angle of $\theta$ radians, this is equivalent to multiplying the vector $\vec{r}$ with the rotation matrix

$R_{\theta}=\begin{bmatrix}\cos(\theta) &-\sin(\theta)\\ \sin(\theta) &\cos(\theta)\end{bmatrix}.$

If you're doing clockwise rotations, your angle will be negative.

That is, the vector $\vec{r}$, after being rotated through the angle $\theta$ radians, will look like $\vec{r}_{\text{rot}}$, where

$\vec{r}_{\text{rot}}=\begin{bmatrix}x'\\y'\end{bma trix}=\begin{bmatrix}\cos(\theta) &-\sin(\theta)\\ \sin(\theta) &\cos(\theta)\end{bmatrix}\begin{bmatrix}x\\y\end{ bmatrix}=\begin{bmatrix}x\cos(\theta)-y\sin(\theta)\\ x\sin(\theta)+y\cos(\theta)\end{bmatrix}.$

You can read more about these matrices here (also the 3-dimensional equivalents). I should mention that in 2 dimensions, if you rotate more than once, it doesn't matter what order you do the rotations in, you'll end up with the same result. In 3 dimensions, the order in which you do the rotations matters.

Incidentally, I'm not sure I buy your calculations. Can you show me how you got them?

3. Thanks Ackbeet, I'll try to show you my calculations. It's been a while since I've done any maths (and then only in Swedish), so please forgive me it's not represented the way you're used to. For the same reason, it's entirely possible I messed up some of the calculations - thanks for checking them for me!

The hexagon is 8830 units from side to side, 10050 from point to point. (According to the tent manufacturer - this is a tent seen from above. The measurements could be wrong for all I know.)

By using the following (from Wikipedia) I calculated the length of the sides of the gray triangles in the picture:
In a right triangle with acute angles measuring 30 and 60 degrees, the hypotenuse is twice the length of the shorter side, and the longer side is equal to the length of the shorter side times √3.
The long side of the gray triangle is half of the side-to-side measurement: 8830/2 = 4415.
To get the short side, I used 4415 / √3 = 2549.
The hypotenuse is twice the short side, 5098.

Since every coordinate is exactly in the middle of a side, I make a smaller triangle with the hypotenuse 2549.
Using the same formulas, I get a new short side of 1274 (2549/2) and a new long side of 2207 (1274 * √3).
The long side gives the x value, 2207.
The length from point to point, halved, minus the short side gives the negative y value: 10050/2 - 1274 = 3751

I then made the same calculations with the hexagon with a side up, as it would be when rotated 30 degrees clockwise to get the target coordinates.

I just realized that I've been using the computer's coordinate system, where (0, 0) is in the top left corner (hence negative y upwards and positive y downwards). It's generally the other way around in maths, isn't it? Could this be the source of my problems with the formula you posted? I've tried but I don't seem to get the correct results. For example, I get -3786 as the x value for the next door counter-clockwise from the blue door when I rotate the hexagon 30 degrees clockwise, when I'd expect it to be 0.
If I invert the y value before making the calculation, and then invert it back, I'm still off by 35. Any suggestions?

4. Hmm. I think some more careful definition would be in order. I'm going to do things the math way, not the computer science way, because I'm more familiar with that.

First: the origin is the center of the hexagon (the entire tent with 6 doors). Define $x$ positive to the right, and $y$ positive up. We assume that two vertices of the hexagon are on the $y$-axis. The side-to-opposite-side distance is 8830 units, and the vertex-to-vertex distance is 10050 units.

First goal: find the coordinates of the middle of the blue door with respect to this coordinate system.

Well, if we draw a line segment from the origin to the middle of the blue door, it will form an angle of 60 degrees with the positive $x$-axis. (Here, positive angles are defined as going counter-clockwise from the positive $x$-axis; so 0 degrees is the positive $x$-axis, 90 degrees is the positive $y$-axis, etc.) The distance from the origin to the middle of the blue door is half of the side-to-opposite-side distance of 8830 units: 4415 units. We therefore have the polar coordinates of the middle of the blue door:

$(r,\theta)=(4415,60^{\circ}).$

We want to find the corresponding cartesian coordinates. That transformation goes like this:

$(x,y)=(r\cos(\theta),r\sin(\theta)).$

Mathematically, this is no different from what you were doing (I think you might have gotten confused, or maybe I was confused in reading what you did.) We get the following:

$\displaystyle{(x,y)=(4415\cos(60^{\circ}),4415\sin (60^{\circ}))=\left(4415\cdot\frac{1}{2},4415\cdot \frac{\sqrt{3}}{2}\right)\approx(2207.5,3823.5)}.$