Vector rotation, respectively, triangular transformation, matrix transformation Implementation, C # source code.

Source: Internet
Author: User
Tags cos x2 y2

The recent use of graphics rotation, took a lot of time to find materials, coding tests. It also uses the trigonometric functions taught by teachers 20 years ago, as well as the matrix operations that have long been forgotten in universities.

Oh, tidy up a bit, I hope we have some help.

Function: Known vector op, clockwise rotation α degree, to find the coordinates of P2 point.

According to the trigonometric functions, we can naturally write:

P2. X = o.x + (int) (Math.Cos (Alpha) * R);
P2.    Y = o.y + (int) (Math.sin (Alpha) * R); Oh, how does the color of the code change? Oh, because I just started blogging in the blog Park, all will not make, hehe, don't bother about it.

Unfortunately, it is wrong to write this way. The reason, there are four quadrants, ah, when the P1 in different quadrants, the results are different. Demonstration process ... Omit the N word.

Well, let's start with a theorem, and maybe everyone will use:

As above, radius R knows, and the rotation angle knows, what is the length of the p1p2?

Oh, caught dead, hand-painted.

This theorem can help us to get the edge length, or to get the size of the corresponding angle of some side. It's in my project.

So what is the P2 point above? Oh, I'm going to omit the complicated reasoning process, directly out of the code:

 PrivatePoint Getnewpoint (DoubleRate , point Cirpos, point startpos) {            DoubleRage2 = rate;///Math.PI *; //b Point around a point to r degrees to get c point coordinates, flag: Clockwise 1, counter-clockwise -1:b is the point of rotation, a is the center//c.x= (b.x-a.x) *cos (R*flag)-(b.y-a.            Y) *sin (R*flag); //c.y= (b.y-a. Y) *cos (R*flag) + (b.x-a.            X) *sin (R*flag); //Point coordinate of the turn-center coordinate//Center coordinate + Calculate coordinate = coordinates of new position            DoubleT1 = (startpos.x-cirpos.x) *Math.Cos (Rage2); DoubleT2 = (STARTPOS.Y-CIRPOS.Y) *Math.sin (Rage2); intNewx = (int) (T1-T2); intNewy = (int) ((STARTPOS.Y-CIRPOS.Y) * Math.Cos (Rage2) + (startpos.x-cirpos.x) *Math.sin (Rage2)); Point Newpoint=NewPoint (Cirpos.x + newx, Cirpos.y +newy); returnNewpoint; }

The above formula, which is not a personal introduction, is a textbook conclusion:

x0=| R|*cosa y0=| R|*sinax1 =| R|*cos (A +b) y1=| R|*sin (a+b)so the x1,y1 is expanded to include:x1=| r|* (COSACOSB-SINASINB) y1=| r|* (sinacosb+cosasinb) CosA = x0/|   r| SinA = y0/| r| Substituting the above formula to get X1 = | r|* (x0*cosb/| r|-y0*sinb/| r|) Y1 = | r|* (y0*cosb/| r|+x0*sinb/|  r|) Final Result: X1 = x0 * cosb-y0 * sinBy1 = x0 * sinb + y0 * COSB hehe, trigonometric method, code and principle are finished. Here is the matrix. Usually in two dimensions, we are using a third-order matrix, why? Because the second order is not enough, why? Because the second-order can only be achieved: scaling, rotation, symmetry transformation, can not achieve translation ah, why? Because, second order is not the Almighty God Ah, why? The Almighty God does not ask why, ha. No words. The way the matrix looks in the program is this: let's hit a matrix in C # to see:
namespacesystem.drawing.drawing2d{//    //Summary://encapsulates a 3 x 3 affine matrix representing a geometric transformation. This class cannot be inherited.      Public Sealed classMatrix:marshalbyrefobject, IDisposable {//        //Summary://Initializes a new instance of the System.Drawing.Drawing2D.Matrix class to the unit matrix.          PublicMatrix ();          PublicMatrix (RectangleF rect, pointf[] plgpts); //        //Summary://Initializes a new instance of the System.Drawing.Drawing2D.Matrix class with the specified element. //        //Parameters://M11://The values in the first row and the first column of the new System.Drawing.Drawing2D.Matrix. //        //M12://The values in the first row and the second column of the new System.Drawing.Drawing2D.Matrix. //        //M21://The value in the second row and the first column of the new System.Drawing.Drawing2D.Matrix. //        //M22://The value in the second row and second column of the new System.Drawing.Drawing2D.Matrix. //        //DX://The value in the third row and the first column of the new System.Drawing.Drawing2D.Matrix. //        //dy://The values in the third row and second column of the new System.Drawing.Drawing2D.Matrix.          PublicMatrix (floatM11,floatM12,floatM21,floatM22,floatDxfloatDY);

That is, Microsoft's matrix, we can define as X1 y1,x2 y2,x3 y3. and Microsoft will automatically give us the third column: 0,0,1. Check out this MSDN and see.

Well, let's start with the code and see how we can do this with the matrix.

        Private voidMyTest () {Point PA=NewPoint ( -, -); Point PB=NewPoint ( $, -);            Drawpoint (Brushes.Red, PA);            Drawpoint (Brushes.black, PB);                        DrawLine (Pens.red, PA, Pb); DoubleAng = Math.PI * -/ the; Matrix m=NewMatrix (); M.translate (-pa. X,-Pa.            Y); M.rotate ( -, MatrixOrder.Append); M.translate (Pa. X, Pa.            Y, MatrixOrder.Append); Point[] PS=Newpoint[1]; ps[0] =PB;            M.transformpoints (PS); Drawpoint (Brushes.blue, ps[0]); Graphics g= Graphics.fromhwnd ( This.            Handle); Rectangle rect=NewRectangle (PA,NewSize (0,0)); Rect. Inflate ( -, -); G.drawarc (pens.red, rect,0, -); Drawpoint (Brushes.green, ps[0]); DrawLine (Pens.red, PA, ps[0]); }        Private voidDrawpoint (Brush B, point P) {Rectangle rect=NewRectangle (P,NewSize (5,5)); Graphics g= Graphics.fromhwnd ( This.            Handle);                   G.fillrectangle (b, rect); }        Private voidDrawLine (pen pen, point P1, point p2) {Graphics g= Graphics.fromhwnd ( This.            Handle);        G.drawline (pen, p1, p2); }

This code can achieve the effect of the first picture.

We look at the information on Baidu, you can see: scaling matrix, rotation matrix, translation matrix. However, if a point is made to rotate around another point, then this operation is a composite matrix operation, which is a combination transformation.

In the actual application process, we use Word, ppt, Photoshop, often will rotate the picture, it may be in the upper left corner of the center, rotated 20 degrees, or the center of the picture as a central point of rotation. The problem is, the center of the circle, which is different from the center of the upper left corner of the screen oh, the coordinates of the four corners of the picture are based on the center of the top left corner of the screen. What's this all about?

Yes, from O, to O1 (haha, I painted an O-skimming, input method, why not draw a O1 ah, hit their feet), we are panning a displacement. So if we want to spin P1 and get P2 (hehe, you know),

We should do this: the first step: Move the O1 to O. Step two: Rotate the coordinate system. Step three: Move the O1 back to its original location. This P2 the task of transferring from the P1 point to the P2 point in the coordinate system x1o1y1.

First step: Translation transformation:

Second Step: Rotation transformation:

Step three: Translation transformation:

Note that matrix multiplication is a priority, like the ranking of leaders, do not mess oh. Call... Gaocheng, comrades are auspicious.

Vector rotation, respectively, triangular transformation, matrix transformation Implementation, C # source code.

Related Article

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.