2
$\begingroup$

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

$\endgroup$
  • $\begingroup$ What with respect to what point is the rotation being done? $\endgroup$ – Milind May 7 '13 at 7:32
  • $\begingroup$ I am calculating center point of rectangle by width/2 and height/2 and then getting angle by arc tangent of y/x. I am getting 0 to 179 and -179 to 0 degree angle. If angle return 2, I want to calculate new coordinates by applying angle to them. Sorry if I wrote wrong. I am not familiar with math words. $\endgroup$ – Chintan Rathod May 7 '13 at 7:35
  • $\begingroup$ are you trying to do exactly what is here math.stackexchange.com/questions/192345/… $\endgroup$ – user31280 May 7 '13 at 7:45
  • $\begingroup$ I implemented logic what they provide. But its not working in my case. I like to calculate top left, top right, bottom left and bottom right corners coordinates based on angle not only left corner. $\endgroup$ – Chintan Rathod May 7 '13 at 7:48
  • $\begingroup$ is the rectangle you have parallel or inclined with the horizontal ground? $\endgroup$ – user31280 May 7 '13 at 7:57
6
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ let me catch someone who can explain this formula.. how to implement in programming language. :-) $\endgroup$ – Chintan Rathod May 7 '13 at 8:11
  • $\begingroup$ rectangle is not rotating on its position. Its coordinates are changed very high. $\endgroup$ – Chintan Rathod May 7 '13 at 9:23
  • 1
    $\begingroup$ @ChintanRathod OK I've improved the answer, does that work? $\endgroup$ – user2354018 May 7 '13 at 9:54
  • $\begingroup$ it is rotating rectangle, but position of rectangle is changed. $\endgroup$ – Chintan Rathod May 7 '13 at 10:18
  • $\begingroup$ At least it's rotating :-). If you want to rotate about the centre of the rectangle, then I'm pretty sure that what I've posted is the right formula, so perhaps you're not calculating the centre of the rectangle properly. But perhaps that's not what you want? Your question wasn't completely clear as to what point you want to rotate about. $\endgroup$ – user2354018 May 7 '13 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.