グローバル変換とローカル変換

グローバル変換は、すべての Graphics オブジェクトによって描画されるすべての項目に適用される変換です。 一方、ローカル変換は、描画される特定の項目に適用される変換です。

グローバル変換

グローバル変換を作成するには、Graphics オブジェクトを作成し、その Transform プロパティを操作します。 Transformプロパティは Matrix オブジェクトであるため、アフィン変換のすべてのシーケンスを保持できます。 Transform プロパティに格納されている変換は、ワールド変換と呼ばれます。 Graphics クラスには、複合ワールド変換を構築するためのメソッドがいくつか用意されています (MultiplyTransformRotateTransformScaleTransformTranslateTransform)。 次の例では、楕円を 2 回描画します。1 回はワールド変換を作成する前、もう 1 回はその後です。 変換によって、y 方向に係数 0.5 で拡大縮小してから、x 方向に 50 単位移動し、さらに 30 度回転します。

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)

次の図は、この変換に関連するマトリックスを示しています。

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

Note

前の例では、座標系の原点 (クライアント領域の左上隅) を中心として楕円を回転させています。 このため、自らの中心点に対して楕円を回転させる場合とは異なる結果になります。

ローカル変換

ローカル変換は、描画される特定の項目に適用されます。 たとえば、GraphicsPath オブジェクトの Transform メソッドを使用すると、そのパスのデータ ポイントを変換できます。 次の例では、変換なしの四角形と、回転変換を適用したパスを描画します。 (ワールド変換がないと仮定します。)

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)

ワールド変換とローカル変換を組み合わせて、さまざまな結果を得ることができます。 たとえば、ワールド変換を使用して座標系を変更し、ローカル変換を使用して、新しい座標系上に描画されたオブジェクトの回転と拡大縮小を行うことができます。

クライアント領域の左端から 200 ピクセル、クライアント領域の上端から 150 ピクセルの位置を原点とする座標系が必要だとします。 さらに、測定単位がピクセルで、x 軸が右向き、y 軸が上向きと仮定します。 既定の座標系では y 軸は下向きであるため、横軸に対して反射を実行する必要があります。 次の図は、そのような反射のマトリックスを示しています。

Illustration of a matrix that reflects across the horizontal axis.

次に、右へ 200 単位、下へ 150 単位移動する必要があるとします。

次の例では、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)

次のコード (前の例の最後) で作成されるパスは、左下隅が新しい座標系の原点である 1 つの四角形で構成されます。 この四角形は、1 回はローカル変換なしで塗りつぶされ、もう 1 回はローカル変換が適用されて塗りつぶされます。 ローカル変換では、係数 2 の水平方向への拡大縮小の後で 30 度回転させます。

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

次の図は、新しい座標系と 2 つの四角形を示しています。

Illustration of the new coordinate system and the two rectangles.

関連項目