In-depth understanding of the Android matrix theory and use of the detailed _android

Source: Internet
Author: User
Tags cos reflection sin

In the linear algebra before learning the matrix, there are some understanding of the basic operation of the Matrix, some time ago in the use of GDI + to learn how to use the matrix to change the image, after reading here to summarize the description.
Let's take a look at the 3 x 3 matrix below, which is divided into 4 parts. Why split into 4 parts, described in detail later.

First of all, give you a simple example: now point P0 (x0, y0) for translation, moved to P (x,y), where the translation of the x direction is the x,y direction of the translation is Y, then, the coordinates of the P (x,y) are:
x = x0 + x
y = y0 + y
The use of matrices to express the above is as follows:

Above also similar to the translation of the image, through the above matrix we found that only need to modify the matrix in the upper right corner of the 2 elements on it.
We look back at the partitioning of the above matrices:

To verify the function partition above, we give a concrete example: now point P0 (x0, y0) to translate, move to P (x,y), where x magnified a times, y amplification B times,

The matrix is: The verification is based on a method similar to the preceding "panning".

The rotation of the image is slightly more complex: the corresponding point after the P0 (x0, y0) rotation theta angle is P (x, y). By using vectors, we get the following:
x0 = R Cosα
Y0 = r sine
x = R cos (α+θ) = x0 cosθ-y0 sinθ
y = r sin (α+θ) = x0 sinθ+ y0 cosθ

So we get the matrix:

What if the image rotates around a point (a, b)? Then you have to translate the coordinates to that point, rotate it, and then translate the rotated image back to the original coordinate origin, which we'll explain in more detail later.

Matrix Learning--How to use the matrix

In this space we will combine the Android Android.graphics.Matrix to specify, remember the matrix of image rotation we said earlier:

From the simplest 90 degrees of rotation are:

There are functions that correspond to rotations in the Android.graphics.Matrix:
Matrix matrix = new Matrix ();
Matrix.setrotate (90);
Test.Log (Maxtrix_tag, "Setrotate ():%s", matrix.tostring ());

View the values of the running matrix (through log output):

Basically exactly the same as the formula above (Android.graphics.Matrix uses floating-point numbers and the integers we use).
With the above example, I believe you can try it yourself. We also found from the example above that we can also initialize the matrix directly, for example, to rotate 30 degrees:

Before we introduced so much, below we begin to introduce image mirroring , divided into 2 kinds: horizontal mirror, vertical mirror. First describes how to implement a vertical mirror, what is a vertical mirror is not detailed. The vertical image changes can also be represented by the matrix change, the corresponding points of the P0 (x0, y0) are the P (x, y), the height of the image is Fheight, the width is fwidth, and the P0 (x0, y0) in the original image is changed by the coordinates of the vertical mirror (x0, FHEIGHT-Y0);
x = x0
y = fheight–y0
The corresponding matrix is deduced:

Final float f[] = {1.0f,0.0f,0.0f,0.0f,-1.0f,120.0f,0.0f,0.0f,1.0f};
Matrix matrix = new matrix ();
Matrix.setvalues (f);

Following the results of the above method running:

As for the horizontal mirror using a similar method, you can try it yourself.
In fact, you can also achieve vertical mirroring by using the following methods:
Matrix matrix = new matrix ();
Matrix.setscale (1.0,-1.0);
Matrix.posttraslate (0, fheight);
This is what we will explain in more detail in the following space.

Matrix Learning--The compound change of image

Matrix Learning--basic knowledge space, we leave a topic: if the image revolves around a point P (a,b), first translate the coordinate system to that point, rotate it, and then translate the rotated image back to the original coordinate origin.

We need 3 steps:
1. Translate --translate the coordinate system to point P (a,b);
2. rotate --rotate the image with the original point as the center;
3. translation -translation of the rotated image back to the original coordinate origin;
Compared with the geometric changes of the previous image (basic image geometry), translation-rotation-translation is required, which requires a variety of images of the geometric changes called image composite changes.
Set to the given image in turn, the basic changes F1, F2, F3 ..., Fn, their change matrix is T1, T2, T3 ...., Tn, the matrix T of the image compound change can be expressed as: T = TnTn-1 ... T1.
In accordance with the above principles, the sequence of changes around a point (a,b) that rotates θ is:

According to the formula above, we enumerate a simple example: rotate around (100,100) 30 degrees (sin = 0.5, cos = 0.866)
float f[]= {0.866F, -0.5f, 63.4f,0.5f, 0.866f,-36.6f,0.0f, 0.0F, 1.0F};
Matrix = new matrix ();
Matrix.setvalues (f);
The rotated image is as follows:

Android provides us with a much simpler approach, as follows:
Matrix matrix = new Matrix ();
Matrix.setrotate (30,100,100);
The actual results after the matrix is run:



is exactly the same as the matrix we obtained in the previous formula.
Here we provide another way to achieve the same effect:
float a = 100.0f,b = 100.0F;
Matrix = new Matrix ();
Matrix.settranslate (A,B);
Matrix.prerotate (30);
Matrix.pretranslate (-a,-b);
Will be in the later space for you to explain in detail
In a similar way, we can also get: the proportional [sx,sy] variation matrix of the relative point P (a,b)

Matrix Learning--preconcats or postconcats?

Starting with the most basic advanced mathematics, the matrix's basic operations include: +, *. Matrix multiplication does not satisfy the commutative law, that is to say a*b≠b*a. There are also 2 common matrices:

With the above foundation, let's start with the topic. Since the matrix does not satisfy the commutative law, the matrix B is multiplied by the matrix A to consider whether it is a left (b*a) or a right multiplication (a*b). The Android Android.graphics.Matrix provides us with a similar approach, which is the preconcats matrix and the Postconcats matrix, which we are going to explain in this space. Here are some examples to illustrate:

By outputting the information, we analyze its running process as follows:

Looked at the output information above. We conclude thatthe preconcats matrix is the equivalent of the right multiplier, and the postconcats matrix is the equivalent of the left-multiply matrices .

Matrix Learning--the wrong-cut transformation

What is an image's shear (transformation)? We still look directly at the image after the wrong-cut transformation is the effect:

Make a summary of the error-cut transformation of the image:

x = x0 + b*y0;
y = d*x0 + y0;

Here's another place to pay attention to:

Through the above, we find the Matrix's setxxxx () function, which calls a reset () at the time of the call, which requires attention in the composite transformation.

Matrix Learning--symmetric transformation (reflection)

What is a symmetric transformation? The concrete theory does not elaborate that image mirroring is one of the symmetrical transformations.

Use the above summary to make a concrete example, produces with the line y=–x symmetrical reflection graph, the code fragment is as follows:

The current matrix output is:

The effect of image transformation is as follows:

Appendix: Trigonometric Functions Formula
Two corners and a formula
Sin (a+b) =sinacosb+cosasinb
Sin (a-b) =sinacosb-sinbcosa
cos (a+b) =cosacosb-sinasinb
cos (a-b) =cosacosb+sinasinb
Tan (a+b) = (TANA+TANB)/(1-TANATANB)
Tan (a-b) = (TANA-TANB)/(1+TANATANB)
Cot (a+b) = (cotacotb-1)/(Cotb+cota) 
Cot (a-b) = (cotacotb+1)/(Cotb-cota)
Double Angle formula
Tan2a=2tana/[1-(Tana) ^2]
Cos2a= (cosa) ^2-(Sina) ^2=2 (cosa) ^2-1=1-2 (Sina) ^2
Sin2a=2sina*cosa
Half-angle formula
Sin (A/2) =√ ((1-cosa)/2) sin (A/2) =-√ ((1-cosa)/2)
cos (A/2) =√ ((1+cosa)/2) cos (A/2) =-√ ((1+cosa)/2)
Tan (A/2) =√ ((1-cosa)/((1+cosa)) tan (A/2) =-√ ((1-cosa)/((1+cosa))
Cot (A/2) =√ ((1+cosa)/((1-cosa)) cot (A/2) =-√ ((1+cosa)/((1-cosa)) 
Tan (A/2) = (1-cosa)/sina=sina/(1+cosa)
and differential product
2sinacosb=sin (a+b) +sin (a-b)
2cosasinb=sin (a+b)-sin (a-b))
2cosacosb=cos (a+b)-sin (a-b)
-2sinasinb=cos (a+b)-cos (a-b)
Sina+sinb=2sin ((a+b)/2) cos ((a-b)/2
Cosa+cosb=2cos ((a+b)/2) sin ((a-b)/2)
Tana+tanb=sin (a+b)/COSACOSB
the formula of accumulation and difference
Sin (a) sin (b) =-1/2*[cos (a+b)-cos (a-b)]
cos (a) cos (b) =1/2*[cos (a+b) +cos (a-b)]
Sin (a) cos (b) =1/2*[sin (a+b) +sin (a-b)]
Induction Formula
Sin (a) =-sin (a)
Cos (a) =cos (a)
Sin (pi/2-a) =cos (a)
cos (pi/2-a) =sin (a)
Sin (pi/2+a) =cos (a)
cos (pi/2+a) =-sin (a)
Sin (pi-a) =sin (a)
cos (pi-a) =-cos (a)
Sin (pi+a) =-sin (a)
cos (pi+a) =-cos (a)
Tga=tana=sina/cosa
Universal Formula
Sin (a) = (2tan (A/2))/(1+tan^2 (A/2))
cos (a) = (1-tan^2 (A/2))/(1+tan^2 (A/2))
Tan (a) = (2tan (A/2))/(1-tan^2 (A/2))
Other formulas
A*sin (a) +b*cos (a) =sqrt (a^2+b^2) sin (a+c) [of which, Tan (c) =b/a]
A*sin (a)-b*cos (a) =sqrt (a^2+b^2) cos (a-c) [of which, Tan (c) =a/b]
1+sin (a) = (sin (a/2) +cos (A/2)) ^2
1-sin (a) = (sin (a/2)-cos (A/2)) ^2
Other non-important trigonometric functions
CSC (a) =1/sin (a)
SEC (a) =1/cos (a)
Hyperbolic function
Sinh (a) = (e^a-e^ (a))/2
Cosh (a) = (e^a+e^ (a))/2
Tgh (a) =sinh (a)/cosh (a)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.