Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rotate a Rect object in Android

Tags:

java

android

Ok so i have created a Rectangle on a canvas using this code

Rect r =new Rect((point.x - rectWidth / 2), (point.y - rectHeight / 2),(point.x-rectWidth / 2),(point.y + rectHeight / 2));

point being the center of the rectangle determined by a touch event. But now I want to be able to rotate this rectangle. Is there any code or method that will allow the user to rotate a shape about its center. I am also interested in getting the corner points of such a rectangle. Any help is appreciated. Thanks.

like image 956
user1998160 Avatar asked Dec 20 '22 08:12

user1998160


1 Answers

To rotate a rectangle around its own center (as opposed to the origin):

Matrix m = new Matrix();
// point is the point about which to rotate.
m.setRotate(degrees, point.x, point.y);
m.mapRect(r);

You can also use Matrix.mapPoints() to find how the corners are transformed.

like image 94
Alex North Avatar answered Jan 06 '23 04:01

Alex North