Transformations globales et locales

Une transformation globale est une transformation qui s’applique à chaque élément dessiné par un objet donné Graphics . En revanche, une transformation locale est une transformation qui s’applique à un élément spécifique à dessiner.

Transformations globales

Pour créer une transformation globale, construisez un Graphics objet, puis manipulez sa Transform propriété. La Transform propriété est un Matrix objet, de sorte qu’il peut contenir n’importe quelle séquence de transformations affine. La transformation stockée dans la Transform propriété est appelée transformation mondiale. La Graphics classe fournit plusieurs méthodes pour la création d’une transformation du monde composite : MultiplyTransform, , RotateTransformScaleTransform, et TranslateTransform. L’exemple suivant dessine deux fois un ellipse : une fois avant de créer une transformation mondiale et une fois après. La transformation est d’abord mise à l’échelle par un facteur de 0,5 dans la direction y, puis traduit 50 unités dans le sens x, puis fait pivoter 30 degrés.

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

L’illustration suivante montre les matrices impliquées dans la transformation.

Illustration of the Scale, Translate, and Rotate matrices combining to form the global transformation.

Remarque

Dans l’exemple précédent, l’ellipse est pivotée sur l’origine du système de coordonnées, qui se trouve en haut à gauche de la zone cliente. Cela produit un résultat différent de la rotation de l’ellipse sur son propre centre.

Transformations locales

Une transformation locale s’applique à un élément spécifique à dessiner. Par exemple, un GraphicsPath objet a une Transform méthode qui vous permet de transformer les points de données de ce chemin. L’exemple suivant dessine un rectangle sans transformation et un chemin d’accès avec une transformation de rotation. (Supposons qu’il n’y a pas de transformation mondiale.)

Matrix myMatrix = new Matrix();
myMatrix.Rotate(45);
myGraphicsPath.Transform(myMatrix);
myGraphics.DrawRectangle(myPen, 10, 10, 100, 50);
myGraphics.DrawPath(myPen, myGraphicsPath);
Dim myMatrix As New Matrix()
myMatrix.Rotate(45)
myGraphicsPath.Transform(myMatrix)
myGraphics.DrawRectangle(myPen, 10, 10, 100, 50)
myGraphics.DrawPath(myPen, myGraphicsPath)

Vous pouvez combiner la transformation mondiale avec les transformations locales pour obtenir un large éventail de résultats. Par exemple, vous pouvez utiliser la transformation mondiale pour réviser le système de coordonnées et utiliser des transformations locales pour faire pivoter et mettre à l’échelle des objets dessinés sur le nouveau système de coordonnées.

Supposons que vous souhaitez un système de coordonnées dont l’origine est de 200 pixels à partir du bord gauche de la zone cliente et de 150 pixels en haut de la zone cliente. En outre, supposons que vous souhaitez que l’unité de mesure soit le pixel, avec l’axe x pointant vers la droite et l’axe y pointant vers le haut. Le système de coordonnées par défaut comporte l’axe y pointant vers le bas. Vous devez donc effectuer une réflexion sur l’axe horizontal. L’illustration suivante montre la matrice d’une telle réflexion.

Illustration of a matrix that reflects across the horizontal axis.

Ensuite, supposons que vous devez effectuer une traduction de 200 unités à droite et 150 unités vers le bas.

L’exemple suivant établit le système de coordonnées juste décrit en définissant la transformation mondiale d’un Graphics objet.

Matrix myMatrix = new Matrix(1, 0, 0, -1, 0, 0);
myGraphics.Transform = myMatrix;
myGraphics.TranslateTransform(200, 150, MatrixOrder.Append);
Dim myMatrix As New Matrix(1, 0, 0, -1, 0, 0)
myGraphics.Transform = myMatrix
myGraphics.TranslateTransform(200, 150, MatrixOrder.Append)

Le code suivant (placé à la fin de l’exemple précédent) crée un chemin qui se compose d’un rectangle unique avec son coin inférieur gauche à l’origine du nouveau système de coordonnées. Le rectangle est rempli une fois sans transformation locale et une fois avec une transformation locale. La transformation locale se compose d’une mise à l’échelle horizontale par un facteur de 2 suivi d’une rotation de 30 degrés.

// 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 transformation
myGraphics.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);
' 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 transformation
myGraphics.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)

L’illustration suivante montre le nouveau système de coordonnées et les deux rectangles.

Illustration of the new coordinate system and the two rectangles.

Voir aussi