Partager via


Transformations globales et locales

Une transformation globale est une transformation qui s’applique à chaque élément dessiné par un objet Graphics donné. 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 objet Graphics, puis manipulez sa propriété Transform. La propriété Transform est un objet Matrix, de sorte qu’elle peut contenir n’importe quelle séquence de transformations affine. La transformation stockée dans la propriété Transform est appelée transformation mondiale. La classe Graphics fournit plusieurs méthodes pour la création d’une transformation mondiale composite : MultiplyTransform, RotateTransform, ScaleTransformet 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 effectue d’abord une mise à l’échelle d’un facteur de 0,5 dans la direction y, puis une translation de 50 unités dans la direction x, et enfin une rotation de 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 des matrices Échelle, Translation et Rotation se combinant pour former la transformation globale.

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 objet GraphicsPath a une méthode Transform qui vous permet de transformer les points de données de ce chemin d’accès. L’exemple suivant dessine un rectangle sans transformation et une trajectoire 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 d’une matrice qui reflète sur l’axe horizontal.

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 décrit simplement en définissant la transformation mondiale d’un objet Graphics.

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 du nouveau système de coordonnées et des deux rectangles.

Voir aussi