10.8 Using Rotation and Translation Transformations

 <  Day Day Up  >  

You want to render a shape that is rotated or translated from a base coordinate.


Technique

Rotation and translation is accomplished by manipulating the transformation matrix of a Graphics object. To rotate the rendering surface, call the RotateTransform method. This method uses a single float value representing the angle of rotation in the clockwise direction:

 
 // rotate clockwise 45 degrees e.Graphics.RotateTransform( 45.0f ); 

To move or translate the drawing surface, use the TranslateTransform method, passing an X and Y value. The rendering origin moves to the right for positive X values and down for positive Y values:

 
 // moves origin 100 pixels to the right and 50 pixels down e.Graphics.TranslateTransform( 100.0f, 50.0f ); 

Comments

Drawing a rectangle using GDI+ is a trivial task. Simply by knowing the location on the form and the width and the height of the rectangle you want to draw, you can render a rectangle with a single function call. What would happen if you wanted to rotate that rectangle 1 degree to the right? The DrawRectangle method would be utterly useless. Instead, you would have to resort to tricky rotational mathematics combined with four separate DrawLine calls. The trivial problem transforms into a complicated algorithm even though you are still in essence drawing a rectangle. The transformation matrix defined in the Graphics class was designed to handle these situations.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net