Hardik Kubavat
Hardik Kubavat

Reputation: 301

difference between Rotate(angle) and Rotate(angle, px, py) in matrix transformation android

According to official documents of Android on Matrix.

XXRotate(float degrees) -- process the matrix to rotate about (0,0) by the specified number of degrees.

XXRotate(float degrees, float px, float py) -- process the matrix to rotate by the specified number of degrees, with a pivot point at (px, py).

for example : XXRotate(90, center_point_x_of_view, center_point_y_of_view)

Upvotes: 0

Views: 618

Answers (1)

kris larson
kris larson

Reputation: 30985

As an example, let's take a square with a width and height of 10. The upper left corner is at the origin of (0,0) which puts the lower right corner at (10,10).

If we transform that square with matrix.setRotate(180F) we would expect that the origin point — being the pivot point — won't move, while the lower right corner is moved to (-10, -10).

Now let's say we transform the square with matrix.setRotate(180F, 5F, 5F). We have put the pivot point at the center of the square, so we expect that the origin moves to (10, 10) and the lower right corner moves to (0, 0).

So after you look at all the math, it turns out that

matrix.setRotate(theta, pivotX, pivotY);

is really just a shorter version of

matrix.setRotate(theta);
matrix.preTranslate(-pivotX, -pivotY);
matrix.postTranslate(pivotX, pivotY);

If you want to know the changes of the points, for the rotation the X changes by the cosine of the angle and the y changes by the sine of the angle.

So simple rotation:

float thetaR = theta * Math.PI / 180F; // convert degrees to radians
int x2 = Math.round(x * (float) Math.cos(thetaR));
int y2 = Math.round(y * (float) Math.sin(thetaR));

And putting it all together, you have

float thetaR = theta * Math.PI / 180F; // convert degrees to radians
int x2 = Math.round((x - pivotX) * (float) Math.cos(thetaR) + pivotX);
int y2 = Math.round((y - pivotY) * (float) Math.sin(thetaR) + pivotY);

Upvotes: 1

Related Questions