A global transformation is a transformation that is applied to each item that is drawn by a given Graphics object. In contrast, a local transformation is a transformation that is applied to a particular item to be drawn.
Global transformations
To create a global transformation, construct a Graphics object , and then manipulate its Transform property. The Transform property is a Matrix object, so it can hold any sequence of affine transformations. The transformations stored in the Transform attribute are called world transformations. The Graphics class provides several ways to build a composite world transformation: MultiplyTransform, RotateTransform, ScaleTransform, and TranslateTransform. The following example draws an ellipse two times: once before the world transformation is created, once after the world transformation is created. The transformation first scales 0.5 times times in the y direction, then translates 50 units in the x direction, then rotates 30 degrees.
Mygraphics.drawellipse (mypen, 0, 0, 100, 50);
Mygraphics.scaletransform (1, 0.5f);
Mygraphics.translatetransform (0, MatrixOrder.Append);
Mygraphics.rotatetransform (MatrixOrder.Append);
Mygraphics.drawellipse (mypen, 0, 0, 100, 50);
Shows the matrix 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 than the ellipse revolves around its own center rotation.
Local transformations
Local transformations are applied to specific items to be drawn. For example, theGraphicsPath object has a Transform method that can be used to transform the data points of the path. The following example draws a rectangle with no transformations and a path with a rotation transform. (assuming there is no world transformation).
New Matrix ();
Mymatrix.rotate (45);
Mygraphicspath.transform (Mymatrix);
Mygraphics.drawrectangle (Mypen, 10, 10, 100, 50);
Mygraphics.drawpath (Mypen, Mygraphicspath);
Local transformations and global transformations
The world transforms can be combined with local transformations to obtain multiple results. For example, a world transformation can be used to correct a coordinate system, while a local transformation can be used to rotate and scale objects drawn on the new coordinate system .
Assume that you need a coordinate system that is 200 pixels from the left edge of the workspace, and 150 pixels from the top of the work area. Also, suppose you want the unit of measure to be a pixel, and the x-axis points to the right and the y-axis to the top. The default coordinate system is the y-axis pointing downwards, so you need to perform reflections around the horizontal axis. Shows the reflection of such matrices.
Next, assume that you need to perform a pan to the right 200 units, down to 150 units.
The following example establishes the coordinate system that was previously described by setting the world transformation of the Graphics object.
New Matrix (1, 0, 0,-1, 0, 0);
Mygraphics.transform = Mymatrix;
Mygraphics.translatetransform (MatrixOrder.Append);
The following code (placed at the end of the preceding example) creates a path of a single rectangle with the lower-left corner of the rectangle at the origin of the new coordinate system. The rectangle is filled two times: The local transformation is not used at one time, and the local transformation is used once. Local transformations include scaling twice times in the horizontal direction and then rotating 30 degrees.
//Create the path.GraphicsPath Mygraphicspath =NewGraphicsPath (); Rectangle MyRectangle=NewRectangle (0,0, -, -); 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 =NewMatrix (); Mypathmatrix.scale (2,1); Mypathmatrix.rotate ( -, 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:
[C # Drawing] Global transformations and local transformations