How can I transform a Point2f with a matrix on Android?

| | August 10, 2015

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?

One Response to “How can I transform a Point2f with a matrix on Android?”

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:

``````┌      ┐
| a  b |
| c  d |
└      ┘
``````

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:

``````┌      ┐┌   ┐    ┌         ┐
| a  b || x | =  | ax + by |
| c  d || y |    | cx + dy |
└      ┘└   ┘    └         ┘
``````

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:

``````┌         ┐┌   ┐    ┌             ┐
| a  b  h || x | =  | ax + by + h |
| c  d  v || y |    | cx + dy + v |
└         ┘| 1 |    └             ┘
└   ┘
``````

or equivalently:

``````┌         ┐┌   ┐    ┌             ┐
| a  b  h || x | =  | ax + by + h |
| c  d  v || y |    | cx + dy + v |
| 0  0  1 || 1 |    |      1      |
└         ┘└   ┘    └             ┘
``````

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.

• How To Submit a iPhone App to the Appstore

by on July 5, 2012 - 0 Comments

The process to develop an iPhone app is not as hard as one might think. The entry into the app store has been opened up to many new types of develope...

• Develop iphone Apps on Windows

by on March 7, 2011 - 13 Comments

Top 10 Ways to Develop an iphone/ipad app without a Mac #1. Code in Java For Java developers, there is a workaround: XMLVM. XMLVM cross-compiles byte...

• Iphone Android App Ideas: Getting Started

by on March 19, 2011 - 3 Comments

Smartphone Application Ideas: Getting Started Smartphone application developers are constantly on the move. And since overzealous developers stick ...