Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the rotated points of a Shape in Android

enter image description here

I have a shape e-g, Rectanle and i want to rotate it at an angle X and want to get the updated rotated points of shape.

Currently for rotation of object i am using canvas.rotate, but the original points remains the same; not the rotated one. I am using this code.

    canvas.save();
    canvas.rotate(angle, Pivate.x, Pivate.y);
    canvas.drawRect(left, top, right, bottom, redPaint);
    canvas.restore();

Any help will be appreciated...

like image 933
Fahad Nasrullah Avatar asked Sep 19 '13 10:09

Fahad Nasrullah


People also ask

How do you rotate a shape in Javascript?

The rotate() method allows you to rotate a drawing object on the canvas. The rotate() method accepts a rotation angle in radians. If the angle is positive, the rotation is clockwise. In case the angle is negative, the rotation is counterclockwise.

How to find the vertices angle after rotation?

When a point (x,y) is rotated about the origin (0,0) counterclockwise by an angle θ, the coordinates of the new point (x′,y′) are x′=xcos(θ)−ysin(θ),y′=xsin(θ)+ycos(θ).

How to find rotated coordinates?

Coordinates of Rotation: The point (x,y) rotated an angle of θ counter-clockwise about the origin will land at point (x′,y′) where x′=xcos(θ)−ysin(θ) x ′ = x cos ⁡ ( θ ) − y sin ⁡ and y′=ycos(θ)+xsin(θ) y ′ = y cos ⁡ ( θ ) + x sin ⁡ .


1 Answers

You need the following way to get update array of points...

  float[] ptArr = new float[] { 20, 30, 60, 30, 60, 45, 20, 45};


  Matrix m = new Matrix();
  m.preRotate(angle, px, py); // where px and py is pivot point.

  m.mapPoints(ptArr);
  //at this point your array will be updated.
 canvas.drawPoints(ptArr, mPaint);
like image 81
Ali Imran Avatar answered Sep 23 '22 09:09

Ali Imran