3

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

  • What with respect to what point is the rotation being done? – Milind Hegde May 07 '13 at 07:32
  • 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. – Chintan Rathod May 07 '13 at 07:35
  • are you trying to do exactly what is here http://math.stackexchange.com/questions/192345/calculate-position-of-rectangle-lower-left-corner-on-center-rotatiton –  May 07 '13 at 07:45
  • 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. – Chintan Rathod May 07 '13 at 07:48
  • is the rectangle you have parallel or inclined with the horizontal ground? –  May 07 '13 at 07:57

2 Answers2

8

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.

  • let me catch someone who can explain this formula.. how to implement in programming language. :-) – Chintan Rathod May 07 '13 at 08:11
  • rectangle is not rotating on its position. Its coordinates are changed very high. – Chintan Rathod May 07 '13 at 09:23
  • 1
    @ChintanRathod OK I've improved the answer, does that work? – user2354018 May 07 '13 at 09:54
  • it is rotating rectangle, but position of rectangle is changed. – Chintan Rathod May 07 '13 at 10:18
  • 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. – user2354018 May 07 '13 at 10:28
  • I want to rotate rectangle from center of it. I am finding center x and y by width/2 and height/2. – Chintan Rathod May 07 '13 at 10:31
  • My first point is (80,100), second (180,100), third (180,200) and fourth is (80,200), my center point is (130,150). Am I taking wrong center points? – Chintan Rathod May 07 '13 at 10:40
  • That is the right centre point. So I'm not sure why it's not working. Perhaps there's another implementation issue. Sorry I can't be more help. – user2354018 May 07 '13 at 10:49
  • In case anyone else followed this answer and got incorrect solutions- close answer but the signs of the sin in the rotation matrix are wrong. Couple sources: http://faculty.salina.k-state.edu/tim/robotics_sg/Pose/pointTrans2d.html and a YT vid https://youtu.be/VcnU7cu6j5Y?t=36 – MDragon Aug 10 '20 at 20:48
  • Submitted an edit with correct signs for the sin in the rotation matrix – MDragon Aug 10 '20 at 20:59
0

Here is a python implementation if anyone is interested:

def rotate_about_a_point(target_point,center_point,angle_rs):

    cp=np.subtract(target_point,center_point)

    px=cp[0]*math.cos(math.radians(angle_rs))+cp[1]*-math.sin(math.radians(angle_rs))


    py=cp[0]*math.sin(math.radians(angle_rs))+cp[1]*math.cos(math.radians(angle_rs))

    return(np.add([px,py],center_point))

target_point=[x,y],
center_point=[x0,y0],
angle_rs=angle in degrees

Note: this for a clockwise rotation

BigZee
  • 103