I'm developing for Android and I'm using the android.renderscript.Matrix3f class to do some calculations.

What I need to do now is to now is to do something like mat.tranform(pointIn, pointOut);

So I need to transform a matrix by a given Point class. In awt I would simply do this:

AffineTransform t = new AffineTransform();
Point2D.Float p = new Point2D.Float();
t.transform( p, p );

But in Android I now have this:

Matrix3f t = new Matrix3f();
PointF p = new PointF();
// Now I need to tranform it somehow..

But the Matrix3f class in Android doesn't have a Matrix.transform(Point2D ptSrc, Point2D ptDst) method.

So I guess I have to do the transformation manually. But I'm not really sure how that works. From what I've seen it's something like a translate and then a rotate?

Could anyone please tell me how to do this in code?


1 Answer 1


Matrix transform functions are just implementing matrix multiplication, which is pretty straightforward. For the simple (non-homogeneous) 2D case, you have a matrix of four values:

$$ \begin{bmatrix} a & b \\ c & d \end{bmatrix} $$

multiplied by a 2-vector (x, y).

I'm most used to the convention that vectors are columns, and so are multiplied on the right, like so: $$ \begin{bmatrix} a & b \\ c & d \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} ax + by \\ cx + dy \end{bmatrix} $$

Here, each entry of the result vector is the dot product of the input vector with the corresponding row of the matrix. (The opposite convention is also valid, where vectors are columns and are multiplied on the left - just know which one your libraries expect)

If you want homogeneous coordinates (necessary for translating with a matrix), then you're "really" working with a 3-vector, (x, y, 1), something like:

$$ \begin{bmatrix} a & b & h\\ c & d & v \end{bmatrix} \begin{bmatrix} x \\ y \\ 1 \end{bmatrix} = \begin{bmatrix} ax + by + h \\ cx + dy + v \end{bmatrix} $$

or equivalently: $$ \begin{bmatrix} a & b & h\\ c & d & v\\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \\ 1 \end{bmatrix} = \begin{bmatrix} ax + by + h \\ cx + dy + v\\ 1 \end{bmatrix} $$

So you might be able to use the 3-vector support that already exists in your library, or implement it yourself using the formula above.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .