jitm
jitm

Reputation: 2597

Android Rotate Matrix

I have matrix. This matrix represents array x and y coordinates. For example

float[] src = {7,1,7,2,7,3,7,4};

I need to rotate this coordinates to 90 degrees. I use android.graphics.Matrix like this:

    float[] src = {7,1,7,2,7,3,7,4};
    float[] dist = new float[8];
    Matrix matrix = new Matrix();
    matrix.preRotate(90.0f);
    matrix.mapPoints(dist,src);

after operation rotate I have array with next values

-1.0    7.0     -2.0    7.0     -3.0    7.0     -4.0    7.0

Its is good for area with 360 degrees. And how do rotate in area from 0 to 90? I need set up center of circle in this area but how ?
Thanks.

Upvotes: 5

Views: 14887

Answers (3)

Ribo
Ribo

Reputation: 3429

Use the Matrix preRotate(float degrees, float px, float py) method (preRotate documenation)

This preRoate(degrees) is equivalent to preRotate(degrees, 0, 0).

Upvotes: 0

Mark
Mark

Reputation: 2952

Use setRotate, not preRotate:

  • setRotate initializes the matrix as a rotation matrix.
  • preRotate multiplies the current matrix by a rotation matrix M' = M x R

Since you called the default constructor your starting with the identity matrix.

Remember matrix multiplication is not commutative.

Upvotes: 6

cobbal
cobbal

Reputation: 70733

I'm not familiar with android, but if you translate after you rotate you can get a rotation around a specific point. Find where your center point would be rotated to, then translate it back to it's original position.

Upvotes: 0

Related Questions