[Math] Calculate new positon of rectangle corners based on angle.

geometrytrigonometry

I am trying to make a re-sizable touch view with rotation in android. I re-size rectangle successfully. You can find code here

It has 4 corners. You can re-size that rectangle by dragging one of corner. But now I want to enhance that logic and want to put rotation in that code. I successfully find angle when user touch center of one of the edge of rectangle. But now problem is I can't get new position of corners so that I can redraw that rectangle and rotation is possible.

I am not very much familiar with trigonometry and mathematics.

Question is : How can I calculate 4 corners' new position based on Angle?.

Edit:

I am doing something like this.

Calculate position of rectangle lower left corner on center rotatiton

But after getting angle, four corner's coordinates I am not able to calculate. I search google last 2 days, but not succeed.

Like this..

enter image description here

Best Answer

To rotate a point $\mathbf{p}$ where $$ \mathbf{p}=\left[ \begin{array} [c]{cc} x \\ y \end{array} \right] $$ about a point $\mathbf{p}_0$ $$ \mathbf{p}_0=\left[ \begin{array} [c]{cc} x_0 \\ y_0 \end{array} \right] $$ by an angle $\phi$, you need to apply a rotation matrix $\mathbf{R}$ to $\mathbf{p}-\mathbf{p}_0$ where $\mathbf{R}$ is given by

$$ \mathbf{R} = \left[ \begin{array} [c]{cc} \cos\phi & \sin\phi \\ -\sin\phi & \cos\phi \end{array} \right]\text{ .} $$

So the new point $\mathbf{p}^{\prime}$ is given by

$$ \mathbf{p}^{\prime}=\mathbf{p}_0+\mathbf{R}\left(\mathbf{p} - \mathbf{p}_0\right)=\left[ \begin{array} [c]{c}% x_0 + (x-x_0)\cos\phi+(y-y_0)\sin\phi\\ y_0 - (x-x_0)\sin\phi+(y-y_0)\cos\phi \end{array} \right] $$

If you use that formula on the $x$ and $y$ coordinates of all four corners of your rectangle then that should work. To rotate about the centre of the rectangle, you need to set $x_0$ and $y_0$ to be the centre of the rectangle.