Implementation of coordinate transformation

Source: Internet
Author: User

Http://blog.csdn.net/bailang71/article/details/1966158

Windows form programming coordinate system type

 

GDI + uses three coordinate spaces: World, page, and device. World coordinates are the coordinate systems used to establish special world models in graphics. They are also the coordinate systems passed in. NET Framework. The page coordinate system refers to the coordinate system used by a drawing (such as a form or control. The device coordinate system is the coordinate system used by a physical device (such as a screen or paper) that is drawn on it. WhenMygraphics. drawline (mypen, 0, 0,160, 80)Point passed to the drawline method ((0,
0)And(160, 80)In the world coordinate space. Before GDI + can draw lines on the screen, the coordinates must undergo a series of transformations. A transformation called "World transformation" can convert the world into page coordinates, while a transformation called "Page transformation" can convert the page coordinates into device coordinates.

Transformation and Coordinate System

Assume that you want to use the coordinate system where the origin is located in the workspace rather than the upper left corner. For example, you need to place the origin point at 100 pixels from the left edge of the work area and 50 pixels from the top. Displays such a coordinate system.

WhenMygraphics. drawline (mypen, 0, 0,160, 80)To obtain the lines shown in the following illustration.

The following table shows the coordinates of the line endpoints in three coordinate spaces:

World

(0, 0) to (160, 80)

Page

(100, 50) to (260,130)

Device

(100, 50) to (260,130)

Note that the origin of the page coordinate space is always in the upper left corner of the work area. Note that the device and page coordinates are the same because the measurement unit is pixel. If you set the measurement unit to a unit other than pixel (for example, inch), the device coordinate is different from the page coordinate.

The world transform used to map world coordinates to page coordinates is stored in the transform attribute of the graphics class. In the previous example
Translate 100 units in the x direction and 50 units in the Y direction. The following example setsGraphicsThe world of the object, and then useGraphicsThe line shown in the figure before the object is drawn:

Copy code in Visual Basic
myGraphics.TranslateTransform(100, 50)myGraphics.DrawLine(myPen, 0, 0, 160, 80)
C # copy code
myGraphics.TranslateTransform(100, 50);myGraphics.DrawLine(myPen, 0, 0, 160, 80);

Page transformation maps page coordinates to device coordinates.GraphicsClass provides pageunit and pagescale attributes for page transformation.GraphicsClass also provides two read-only attributes: dpix and dpiy, used to check the horizontal and vertical points of the display device per inch.

AvailableGraphicsClassPageunitAttribute specifies measurement units other than pixels.

Note:

You cannot setPageunitThe attribute is set to world, because this is not a physical unit and will cause exceptions.

The following example draws a line from (0, 0) to (2, 1), where the point (2, 1) is located at 2 inch on the right of the point (0, 0) and at 1 inch below:

Copy code in Visual Basic
myGraphics.PageUnit = GraphicsUnit.InchmyGraphics.DrawLine(myPen, 0, 0, 2, 1)
C # copy code
myGraphics.PageUnit = GraphicsUnit.Inch;myGraphics.DrawLine(myPen, 0, 0, 2, 1);
Note:

If the width of the pen is not specified when constructing the pen, the previous example draws a 1 inch-width line. You can specify the pen width in the second parameter of the PEN constructor:

Copy code in Visual Basic
Dim myPen As New Pen(Color.Black, 1 / myGraphics.DpiX)
C # copy code
Pen myPen = new Pen(Color.Black, 1 / myGraphics.DpiX);

If we assume that the display device has 96 points in both the horizontal and vertical directions per inch, the final point of the line in the above example has the following coordinates in the three coordinate spaces:

World

(0, 0) to (2, 1)

Page

(0, 0) to (2, 1)

Device

(0, 0) to (192, 96)

Note that because the origin of the world coordinate space is in the upper left corner of the work area, the page coordinate is the same as the world coordinate.

You can merge world transformations and page transformations to achieve multiple effects. For example, assume that you want to use an inch as the measurement unit and make the original point of the coordinate system 2 inch to the left edge of the work area and 1/2 inch to the top of the work area. The following sample settingsGraphicsThe world transformation and page transformation of the object, and then draw a line from (0, 0) to (2, 1:

Copy code in Visual Basic
myGraphics.TranslateTransform(2, 0.5F)myGraphics.PageUnit = GraphicsUnit.InchmyGraphics.DrawLine(myPen, 0, 0, 2, 1)
C # copy code
myGraphics.TranslateTransform(2, 0.5f);myGraphics.PageUnit = GraphicsUnit.Inch;myGraphics.DrawLine(myPen, 0, 0, 2, 1);

Lines and coordinate systems are displayed.

If we assume that the display device has 96 points in both the horizontal and vertical directions per inch, the final point of the line in the above example has the following coordinates in the three coordinate spaces:

World

(0, 0) to (2, 1)

Page

(2, 0.5) to (4, 1.5)

Device

(192, 48) to (384,144)

 

Transformed Matrix Representation

 

M × nThe matrices are arranged inMRows andNNumber of columns. Displays several matrices.

You can add two matrices of the same size by adding a single element. An example of adding two matrices is displayed.

M × nA matrix can be associated withN × PMultiply the matrix and the result isM × PMatrix. The number of columns in the first Matrix must be the same as that in the second matrix. For example, a 4x2 matrix is multiplied by a 2x3 matrix to generate a 4x3 matrix.

The point in the row and column of the Matrix can be considered as a vector. For example, (2, 5) is a vector with two components, (3, 7, 1) is a vector with three components. The dot product of the two vectors is defined as follows:

(A, B) • (c, d) = AC + BD

(A, B, C) • (d, e, f) = AD + be + CF

For example, the point product of (2, 3) and (5, 4) is (2) (5) + (3) (4) = 22. The Point product of (2, 5, 1) and (4, 3, 1) is (2) (4) + (5) (3) + (1) (1) = 24. Note that the dot product of two vectors is a number, not another vector. Note that point product can be calculated only when the component numbers of two vectors are the same.

Use a (I, j) as the entry of row I and column J in matrix. For example, a (3, 2) is an entry of 3rd rows and 2nd columns in matrix. Assuming that A, B, and C are matrices and AB = C, the calculation of items c is as follows:

C (I, j) = (row I of A) • (column J of B)

Several examples of matrix multiplication are displayed.

If the point in the plane is regarded as a 1X2 matrix, the point can be transformed by multiplying the point by a 2x2 matrix. Displays several transformations applied to points (2, 1.

All transformations shown in the preceding figure are linear transformations. Some other transformations (such as translation) are not linear and cannot be expressed in the form of multiplying the 2x2 matrix. Assume that you want to rotate a vertex 90 degrees from (2, 1), translate it to 3 units in the x direction, and translate it to 4 units in the Y direction. You can perform this operation by first using matrix multiplication and then using matrix addition.

A linear transformation (multiplied by a 2x2 matrix) following a one-level moving (addition to a 1X2 matrix) is called an affine transformation. An alternative solution for storing an affined transformation in a matrix (one for the linear part and one for translation) is to store the entire transformation in a 3 × 3 matrix. To make it take effect, points on the plane must be stored in the 1 × 3 matrix with the virtual coordinate. The common method is to make all coordinate coordinates equal to 1. For example, a matrix [2 1 1] represents a vertex (2, 1 ). This example demonstrates an affine transformation that represents multiplying a single 3x3 matrix (Rotating 90 degrees; translating three units in the x direction, and translating four units in the Y direction ).

In the previous example, a vertex (2, 1) is mapped to a vertex (2, 6 ). Note that the third column of the 3x3 matrix contains numbers 0, 0, and 1. This will always be the case for a 3 × 3 matrix of an affinic transformation. An important number is the six numbers in column 1 and column 2. 2 × 2 in the upper left corner of the matrix indicates the linear part of the transformation, and the first two items in the second row represent the translation.

In GDI +, you can store the affine transform in the matrix object. Because the third column of the Matrix that represents the affine transformation is always (0, 0, 1 ),MatrixYou only need to specify
6.Matrix mymatrix = new matrix (0, 1,-1, 0, 3, 4)Statement to construct the matrix shown in the preceding figure.

Compound Transformation

A composite transformation is a transformation sequence. Consider the matrix and transformation in the following list:

Matrix

Rotate 90 degrees

Matrix B

Scale twice in the X direction

Matrix C

Translate three units in the Y direction

If the vertex (2, 1) represented by [2 1] and multiplied by A, B, and C, then the vertex (2, 1) there are three Transformations in the order listed.

[2 1 1] abc = [-2 5 1]

Instead of storing the three parts of the composite transformation in three independent matrices, We can multiply them together by A, B, and C to obtain a single 3 × 3 matrix that stores the entire composite transformation. Assume abc = D. Then, the result of multiplying a vertex by D is the same as that of multiplying A, B, and C by a vertex.

[2 1 1] d = [-2 5 1]

Displays matrices A, B, C, and D.

The matrix of composite transformations can be obtained by multiplying several individual transformation matrices, which means that any sequence of affine transformations can be stored in a singleMatrixObject.

Warning

The order of compound transformations is very important. Generally, first rotation, then scaling, and then translation are different from first scaling, then rotating, and then translating. Similarly, the order of matrix multiplication is also important. Generally, ABC is different from BAC.

MatrixClass provides several methods to construct composite transformations: multiply, rotate, rotateat, scale, shear, and translate. The following example creates a composite transform (first rotating
30 degrees, scale twice in the Y direction, and then translate 5 units in the x direction) of the matrix.

Copy code in Visual Basic
Dim myMatrix As New Matrix()myMatrix.Rotate(30)myMatrix.Scale(1, 2, MatrixOrder.Append)myMatrix.Translate(5, 0, MatrixOrder.Append)
C # copy code
Matrix myMatrix = new Matrix();myMatrix.Rotate(30);myMatrix.Scale(1, 2, MatrixOrder.Append);myMatrix.Translate(5, 0, MatrixOrder.Append);

This matrix is displayed.

Global and local Transformations

 

Global transformation is applied to the transformation of each project drawn by a given graphics object. In contrast, local transformations apply to transformations of specific projects to be drawn.

Global Transformation

To create a global transformation, constructGraphicsObject, and then perform operations on its transform attributes.TransformAn attribute is a matrix object. Therefore, it can store any sequence of the affine transformation. Stored inTransformTransformations in attributes are called world transformations.GraphicsClass provides several methods for building a composite world Transformation: multiplytransform, rotatetransform, scaletransform, and translatetransform. The following example draws two ovans: One before creating a world transformation, and one after creating a world transformation. First
Scale 0.5 times in the Y direction, then translate 50 units in the x direction, and then rotate 30 degrees.

Copy code in Visual Basic
myGraphics.DrawEllipse(myPen, 0, 0, 100, 50)myGraphics.ScaleTransform(1, 0.5F)myGraphics.TranslateTransform(50, 0, MatrixOrder.Append)myGraphics.RotateTransform(30, MatrixOrder.Append)myGraphics.DrawEllipse(myPen, 0, 0, 100, 50)
C # copy code
myGraphics.DrawEllipse(myPen, 0, 0, 100, 50);myGraphics.ScaleTransform(1, 0.5f);myGraphics.TranslateTransform(50, 0, MatrixOrder.Append);myGraphics.RotateTransform(30, MatrixOrder.Append);myGraphics.DrawEllipse(myPen, 0, 0, 100, 50);

Displays the matrices involved in the transformation.

Note:

In the previous example, the ellipse rotates around the origin of the coordinate system. The origin is located in the upper left corner of the workspace. This produces different results compared to the rotation of an ellipse around its own center.

Local Transformation

Local transformations are applied to specific projects to be drawn. For example, a graphicspath object has a transform method, which can be used to transform the data points in the path. The following example draws a rectangle without transformation and a path with a rotation transformation. (Assuming there is no world change ).

Copy code in Visual Basic
Dim myMatrix As New Matrix()myMatrix.Rotate(45)myGraphicsPath.Transform(myMatrix)myGraphics.DrawRectangle(myPen, 10, 10, 100, 50)myGraphics.DrawPath(myPen, myGraphicsPath)
C # copy code
Matrix myMatrix = new Matrix();myMatrix.Rotate(45);myGraphicsPath.Transform(myMatrix);myGraphics.DrawRectangle(myPen, 10, 10, 100, 50);myGraphics.DrawPath(myPen, myGraphicsPath);

World transformations can be combined with local transformations to obtain multiple results. For example, World transformations can be used to modify the coordinate system, while local transformations can be used to rotate and scale objects drawn on the new coordinate system.

Assume that you need a coordinate system with a distance of 200 pixels from the left edge of the work area and 150 pixels from the top of the work area. In addition, assume that you need a measurement unit of pixels, and the X axis points to the right, and the Y axis points to the top. The default coordinate system points to the bottom of the Y axis, so you need to perform reflection around the horizontal axis. This matrix reflection is displayed.

Next, assume that you need to perform a translation of 200 to the right and 150 to the next.

The following example setsGraphicsThe world transformation of objects and the establishment of the previously described coordinate system.

Copy code in Visual Basic
Dim myMatrix As New Matrix(1, 0, 0, -1, 0, 0)myGraphics.Transform = myMatrixmyGraphics.TranslateTransform(200, 150, MatrixOrder.Append)
C # copy code
Matrix myMatrix = new Matrix(1, 0, 0, -1, 0, 0);myGraphics.Transform = myMatrix;myGraphics.TranslateTransform(200, 150, MatrixOrder.Append);

The following code (placed at the end of the preceding example) creates a path composed of a single rectangle. The lower left corner of the rectangle is at the origin of the new coordinate system. The rectangle is filled twice: local transformation is not used at one time, and local transformation is used at one time. Local transformations are scaled to 2 times in the horizontal direction, and then rotated to 30 degrees.

Copy code in Visual Basic
' Create the path.Dim myGraphicsPath As New GraphicsPath()Dim myRectangle As New Rectangle(0, 0, 60, 60)myGraphicsPath.AddRectangle(myRectangle)' Fill the path on the new coordinate system.' No local transformationmyGraphics.FillPath(mySolidBrush1, myGraphicsPath)' Set the local transformation of the GraphicsPath object.Dim myPathMatrix As New Matrix()myPathMatrix.Scale(2, 1)myPathMatrix.Rotate(30, MatrixOrder.Append)myGraphicsPath.Transform(myPathMatrix)' Fill the transformed path on the new coordinate system.myGraphics.FillPath(mySolidBrush2, myGraphicsPath)
C # copy code
// Create the path.GraphicsPath myGraphicsPath = new GraphicsPath();Rectangle myRectangle = new Rectangle(0, 0, 60, 60);myGraphicsPath.AddRectangle(myRectangle);// Fill the path on the new coordinate system.// No local transformationmyGraphics.FillPath(mySolidBrush1, myGraphicsPath);// Set the local transformation of the GraphicsPath object.Matrix myPathMatrix = new Matrix();myPathMatrix.Scale(2, 1);myPathMatrix.Rotate(30, MatrixOrder.Append);myGraphicsPath.Transform(myPathMatrix);// Fill the transformed path on the new coordinate system.myGraphics.FillPath(mySolidBrush2, myGraphicsPath);

The new coordinate system and two rectangles are displayed.

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.