This topic has not yet been rated - Rate this topic

Matrix.TransformVectors Method (Point[])

Applies only the scale and rotate components of this Matrix to the specified array of points.

Namespace:  System.Drawing.Drawing2D
Assembly:  System.Drawing (in System.Drawing.dll)
public void TransformVectors(
	Point[] pts
)

Parameters

pts
Type: System.Drawing.Point[]
An array of Point structures that represents the points to transform.

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, an Paint event object. The code performs the following actions:

  • Creates an array of points that form a rectangle.

  • Draws this array of points to the screen prior to applying a scaling transform (the blue rectangle).

  • Creates a matrix and scales it by 3 in the x-axis and 2 in the y-axis, and translates it by 100 in both axes.

  • Lists the matrix elements to the screen.

  • Applies this matrix transform to the array of points.

  • Draws the transformed array to the screen (the red rectangle).

Notice that the red rectangle has been scaled by a factor of 3 in the x-axis and by 2 in the y-axis, including the upper left-hand corner of the rectangle (the beginning point of the rectangle) but the translation vector (the last two elements of the matrix) is ignored.


public void TransformVectorsExample(PaintEventArgs e)
{
    Pen myPen = new Pen(Color.Blue, 1);
    Pen myPen2 = new Pen(Color.Red, 1);

    // Create an array of points.
    Point[] myArray =
             {
                 new Point(20, 20),
                 new Point(120, 20),
                 new Point(120, 120),
                 new Point(20, 120),
                 new Point(20,20)
             };

    // Draw the Points to the screen before applying the
    // transform.
    e.Graphics.DrawLines(myPen, myArray);

    // Create a matrix, scale it, and translate it.
    Matrix myMatrix = new Matrix();
    myMatrix.Scale(3, 2, MatrixOrder.Append);
    myMatrix.Translate(100, 100, MatrixOrder.Append);

    // List the matrix elements to the screen.
    ListMatrixElements(e,
        myMatrix,
        "Scaled and Translated Matrix",
        6,
        20);

    // Apply the transform to the array.
    myMatrix.TransformVectors(myArray);

    // Draw the Points to the screen again after applying the
    // transform.
    e.Graphics.DrawLines(myPen2, myArray);
}

//-------------------------------------------------------
// This function is a helper function to
// list the contents of a matrix.
//-------------------------------------------------------
public void ListMatrixElements(
    PaintEventArgs e,
    Matrix matrix,
    string matrixName,
    int numElements,
    int y)
{

    // Set up variables for drawing the array
    // of points to the screen.
    int i;
    float x = 20, X = 200;
    Font myFont = new Font("Arial", 8);
    SolidBrush myBrush = new SolidBrush(Color.Black);

    // Draw the matrix name to the screen.
    e.Graphics.DrawString(
        matrixName + ":  ",
        myFont,
        myBrush,
        x,
        y);

    // Draw the set of path points and types to the screen.
    for(i=0; i<numElements; i++)
    {
        e.Graphics.DrawString(
            matrix.Elements[i].ToString() + ", ",
            myFont,
            myBrush,
            X,
            y);
        X += 30;
    }
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ