6

I would like to know about the function of mapRect api available under Matrix class in Android. If I have a sample Matrix A and Rectangle R, then for

RectF R = new RectF(t1,t2,t3,t4);

A.mapRect(R);

what kind of transformation is likely to happen to R. It would be more helpful if someone can illustrate the mapRect() api with some suitable examples.

San
  • 71
  • 1
  • 5
  • it maps all the corners, and computes the bounding rect around mapped points – pskink Oct 09 '15 at 17:18
  • I would like to know about the resultant Rectangle dimensions. In the above case, Can u provide me an example with a sample 3x3 matrix ? – San Oct 09 '15 at 17:21
  • it depends on Matrix `A`, if its rotated, scaled etc – pskink Oct 09 '15 at 17:24
  • Sorry for more trouble pskink. I am currently working in an Android Project where I come across this function while performing Image related operations. To be precise, I couldn understand the change done by this transformation function. If Matrix A is [(a1,a2,a3),(b1,b2,b3),(c1,c2,c3)] , I would like to know about arriving at resultant Rectangle dimensions mathematically. – San Oct 09 '15 at 17:31
  • like [this](https://en.wikipedia.org/wiki/Transformation_matrix#Affine_transformations) ? – pskink Oct 09 '15 at 17:36

1 Answers1

18

Here's a very simple example:

Let's take a matrix:

Matrix matrix = new Matrix();

Set that matrix to scale everything twice as large:

matrix.setScale(2.0F, 2.0F);

Create a rectangle that is 10x10 with origin in upper left corner:

RectF rect = new RectF(0F, 0F, 10F, 10F);

So when we call

matrix.mapRect(rect);

the input rectangle we created is replaced with the output rectangle, which is the result of transforming the input:

rect.left = 0F;
rect.top = 0F;
rect.right = 20F;
rect.bottom = 20F;

There is another version of the method

matrix.mapRect(RectF dst, RectF src);

that does the same transform without affecting the input rectangle.


What is a matrix?

Consider a mirror. The mirror takes your image and creates a horizontally flipped version of your image.

Consider a microphone and an amplifier. They take your voice and create a louder version of your voice.

That's what a matrix is. It's a transformer. It takes an input and creates an output that is based on the input. So a matrix can transform a point, a rectangle, a circle, a polygon...

For more info, see my answer How does Matrix.postScale( sx, sy, px, py) work?

Also check out Affine transformations | Wikipedia. There is an awesome graphic that shows the different affine transforms and their effects.

Community
  • 1
  • 1
kris larson
  • 30,387
  • 5
  • 62
  • 74
  • Thats a fine example Kris. May I know how the rectangle co-ordinates (top,left,bottom and right)are mapped onto a matrix. Can they be represented in a matrix? – San Oct 10 '15 at 10:33
  • More specifically: The matrix maps an input rectangle onto an output rectangle. The same matrix can also map an input circle onto an output circle, or an input polygon onto an output polygon. Please see my updated answer. I added a link to another question I answered where I go into gory detail about how the matrix operations actually work. Perhaps that will clear up some confusion. – kris larson Oct 10 '15 at 13:11