Matrix learning-Basic Knowledge

Source: Internet
Author: User

Previously, I learned about matrices in linear algebra and had some knowledge about the basic operations of matrices. I learned how to use matrices to change images when I used GDI + some time ago, after reading this article, I will summarize it here.

First, let's take a look at the 3x3 matrix, which is divided into four parts. The reason for splitting the data into four parts is described in detail later.


First, we will give you a simple example: after we have set the point P0 (x0, y0) for translation, we will move it to p (x, y), where the translation in the X direction is △x, if the translation in the Y direction is △y, then the coordinates of P (x, y) are:

X = x0 + △x

Y = y0 + △y

The matrix expression is as follows:

The above is similar to the image translation. Through the above matrix, we find that you only need to modify the two elements in the upper right corner of the matrix.

Let's look back at the division of the above matrix:

In order to verify the functional division above, we will give a specific example: now we have set the point P0 (x0, y0) to pan and move it to p (x, y), where X is multiplied by, Y to B,

The matrix is: Verify according to the method similar to the previous "Translation.

The image rotation is slightly complicated: The corresponding point after the θ angle is rotated by P0 (x0, y0) is p (x, y ). By using vectors, we can get the following:

X0 = r cos α

Y0 = r sin α

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 certain point (a, B? First, you need to translate the coordinates to the point, then rotate, and then translate the rotated image back to the original coordinate origin. We will introduce it in detail in the subsequent sections.

Matrix learning-how to use Matrix

In the previous article, we introduced matrix from the perspective of advanced mathematics. In this article, we will combine android in Android. graphics. for details about the Matrix, remember the image rotation matrix we mentioned earlier:


Which of the following is the simplest rotation of 90 degrees:


There is a corresponding rotation function in Android. Graphics. Matrix:

Matrix matrix = new matrix ();

Matrix. setrotate (90 );

Test. Log (maxtrix_tag, "setrotate (90): % s", matrix. tostring ());


View the value of the matrix after running (output through log ):


It is basically the same as the above formula (Android. Graphics. matrix uses floating-point numbers while we use integers ).

With the above example, you can try it yourself. Through the above example, we also found that we can also initialize the Matrix directly, for example, to rotate 30 degrees:


We have introduced so many images. Next we will introduce two types of images: horizontal images and vertical images. First, we will introduce how to implement a vertical image. What is a vertical image is not described in detail. The vertical image changes of the image can also be represented by matrix changes. Set the point P0 (x0, y0) to p (x, y), and the Image Height to fheight, the width is fwidth, and the coordinates of P0 (x0, y0) in the original image after the vertical image are changed to (x0
, Fheight-y0 );

X = x0

Y = fheight-y0

Export the corresponding matrix:


Finalfloat
F [] = {1.0f, 0.0f, 0.0f, 0.0f,-1.0f, 1200000f, 0.0f, 0.0f, 1.0f };

Matrix matrix = newmatrix ();


Matrix. setvalues (f );

The result is as follows:

You can try horizontal images in a similar way.

In fact, you can use the following method to implement a vertical image:

Matrix matrix = newmatrix ();


Matrix. setscale (1.0,-1.0 );

Matrix. posttraslate (0, fheight );

This is what we will describe in detail later.

Matrix learning-compound changes of images

Matrix learning-in the space of basic knowledge, let's leave a topic: if an image rotates around a certain point P (A, B), first you need to translate the coordinate system to this point and then rotate it, then, the rotated image is moved back to the original coordinate origin.

We need three steps:

1. Translation -- translate the coordinate system to point P (A, B );

2. Rotation-rotate the image with the origin as the center;

3. Translation-translate the rotated image back to the original coordinate origin;

Compared with the geometric changes of the image (the Basic geometric changes of the image), here we need to translate, rotate, and translate. This kind of geometric changes that require multiple types of images is called the compound changes of the image.

For a given image, F1, F2, F3 ..... , FN, their change matrices are T1, T2, T3 ..... , TN, image composite change matrix t can be expressed as: t = TnTn-1... T1.

According to the above principle, the change matrix sequence of θ rotating around a certain point (a, B) is:


Based on the above formula, we will give a simple example: rotate around (100,100) 30 degrees (SIN 30 = 0.5, cos 30 = 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 simpler method, as follows:


Matrix matrix = new matrix ();

Matrix. setrotate (30,100,100 );

Actual results after matrix operation:


This is exactly the same as the matrix obtained through the formula.

Here we provide another method to achieve the same effect:


Float a = 1000000f, B = 1000000f;

Matrix = new matrix ();

Matrix. settranslate (A, B );

Matrix. prerotate (30 );

Matrix. pretranslate (-a,-B );

I will explain it in detail later.

Using a similar method, we can also obtain the ratio of the relative P (A, B) to the [SX, sy] change matrix.


Matrix Learning -- preconcats or postconcats?

Starting from the most basic advanced mathematics, the basic operations of matrix include: + and ,*. The multiplication of matrix does not satisfy the exchange law, that is, a * B = B *.

There are two common matrices:


With the above foundation, we will start to enter the topic. Because the matrix does not meet the exchange law, if matrix B is used to multiply matrix A, you need to consider whether it is left multiplication (B * A) or right multiplication (A * B ). In Android. Graphics. Matrix of Android, a similar method is provided for us, that is, the preconcats matrix and postconcats matrix to be described in this article. The following is an example:


Through the output information, we analyze the running process as follows:


Read the output above. We conclude that the preconcats matrix is equivalent to the right multiplication matrix, and the postconcats matrix is equivalent to the left multiplication matrix.

In the previous article, we said:


The detailed analysis of the dizzy process is not described here.

Matrix learning-error tangent Transformation


What is shear transformation )? We can directly see the effect after the incorrect image switch:



Make a summary of the miscut transformation of the image:


X = x0 + B * y0;

Y = D * x0 + y0;


Here we will introduce you to the following points:


Through the above, we found that the setxxxx () function of matrix calls a reset () during the call, which requires attention during composite transformation.

Matrix learning-symmetric transformation (reflection)

What is symmetric transformation? The specific theory is not detailed. The image is a type of symmetric transformation.


Take the above summary as an example to generate a reflection image that is symmetric with the straight line y =-X. The code snippet is as follows:


The current matrix output is:


The effect of image transformation is as follows:



Appendix: trigonometric function formula

Two corners and Formula

Sin (a + B) = sinaco Sb + cosasinb

Sin (a-B) = sinaco Sb-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)

Angle X formula

Tan2a = 2 Tana/[1-(Tana) ^ 2]

Cos2a = (COSA) ^ 2-(SINA) ^ 2 = 2 (COSA) ^ 2-1 = 1-2 (SINA) ^ 2

Sin2a = 2sina * cosa

Halfwidth 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 difference Product

2 sinaco sb = sin (a + B) + sin (a-B)

2 cosasinb = sin (a + B)-sin (a-B ))

2 cosacosb = cos (a + B)-sin (a-B)

-2 sinasinb = 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

Product and difference Formulas

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 ()

Cos (-a) = cos ()

Sin (PI/2-A) = cos ()

Cos (PI/2-A) = sin ()

Sin (PI/2 + a) = cos ()

Cos (PI/2 + a) =-sin ()

Sin (Pi-a) = sin ()

Cos (Pi-a) =-cos ()

Sin (PI + a) =-sin ()

Cos (PI + a) =-cos ()

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) [Where Tan (c) = B/A]

A * sin (a)-B * Cos (A) = SQRT (a ^ 2 + B ^ 2) Cos (a-c) [Where 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-key trigonometric Functions


SEC (A) = 1/cos ()

Hyperbolic Functions

Sinh (A) = (E ^ A-E ^ (-A)/2

Cosh (A) = (E ^ A + e ^ (-A)/2

TGH (A) = sinh (a)/cosh ()

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.