Get the coordinates of a point as it rotates around the center of a rectangle

80 Views Asked by At

I am trying to get the coordinates of a point as it rotates around the center of a rectangle (the point will always be on the perimeter).

So if the rectangle dimensions are 400 x 200 and we declare that due east from center is zero degrees, the starting point (x,y) at zero degrees is (400, 100). Given the center of the rectangle (a,b) is at (200,100), I see from this post that rotating 180 degrees can be solved with the formula (2a-x, 2b-y)

Can you help me with a solution for any degree of rotation?

UPDATE In case you were curious, I am programming in javascript trying to calculate the starting and ending points on a canvas rectangle when working with gradients and knowing the gradient degree.

1

There are 1 best solutions below

6
On BEST ANSWER

Let us place the origin of coordinates in the center of your rectangle. Let $a=400$, $b=200$ be the side lengths of the rectangle.

For "small" angles $\alpha$, namely, when $|\alpha|<\arctan{b\over a}$, we have $$ (x,y) = ({a\over2},{a\over2}\tan\alpha). $$ Can you continue for larger angles? The whole solution may look like this: $$ (x,y) = ({a\over2},{a\over2}\tan\alpha) \quad\mbox{ for }|\alpha|<\arctan{b\over a} \ \ \mbox{ or } \ \ |\alpha-2\pi|<\arctan{b\over a}, \quad\mbox{(right)} $$ $$ (x,y) = (-{b\over2}\tan(\alpha-{\pi\over2}),{b\over2}) \ \quad\qquad\mbox{ for }|\alpha-{\pi\over2}|<\arctan{a\over b}, \quad\mbox{(top)} $$ $$ (x,y) = (-{a\over2},-{a\over2}\tan(\alpha-\pi)) \qquad\mbox{ for }|\alpha-\pi|<\arctan{b\over a}, \qquad\mbox{(left)} $$ $$ (x,y) = ({b\over2}\tan(\alpha-{3\pi\over2}),-{b\over2}) \qquad\mbox{ for }|\alpha-{3\pi\over2}|<\arctan{a\over b} \qquad\mbox{(bottom).} $$ (The formulas can still be simplified a little.)

JavaScript has the function $\,\tt Math.atan2(a,b)$ (arc tangent of a ratio) that is very suitable for checking conditions like the above.