Matrix.Multiply 方法

定義

藉由預先規劃指定的 Matrix,將這個 Matrix 乘以指定的 Matrix

多載

Multiply(Matrix)

藉由預先規劃指定的 Matrix,將這個 Matrix 乘以 matrix 參數中所指定的矩陣。

Multiply(Matrix, MatrixOrder)

將這個 Matrix 乘以 matrix 參數中所指定的矩陣,並且依據 order 參數中所指定的順序。

Multiply(Matrix)

來源:
Matrix.cs
來源:
Matrix.cs
來源:
Matrix.cs

藉由預先規劃指定的 Matrix,將這個 Matrix 乘以 matrix 參數中所指定的矩陣。

public:
 void Multiply(System::Drawing::Drawing2D::Matrix ^ matrix);
public void Multiply (System.Drawing.Drawing2D.Matrix matrix);
member this.Multiply : System.Drawing.Drawing2D.Matrix -> unit
Public Sub Multiply (matrix As Matrix)

參數

matrix
Matrix

這個 Matrix 要乘以的 Matrix

範例

如需範例,請參閱 Multiply

適用於

Multiply(Matrix, MatrixOrder)

來源:
Matrix.cs
來源:
Matrix.cs
來源:
Matrix.cs

將這個 Matrix 乘以 matrix 參數中所指定的矩陣,並且依據 order 參數中所指定的順序。

public:
 void Multiply(System::Drawing::Drawing2D::Matrix ^ matrix, System::Drawing::Drawing2D::MatrixOrder order);
public void Multiply (System.Drawing.Drawing2D.Matrix matrix, System.Drawing.Drawing2D.MatrixOrder order);
member this.Multiply : System.Drawing.Drawing2D.Matrix * System.Drawing.Drawing2D.MatrixOrder -> unit
Public Sub Multiply (matrix As Matrix, order As MatrixOrder)

參數

matrix
Matrix

這個 Matrix 要乘以的 Matrix

order
MatrixOrder

表示乘法順序的 MatrixOrder

範例

下列程式碼範例是設計來搭配Windows Forms使用,而且需要 PaintEventArgse 事件 Paint 物件。 此程式碼會執行下列動作:

  • 建立三個矩陣。

  • 列出矩陣 1 到畫面的內容。

  • 將矩陣 1 乘以矩陣 2,並將結果儲存在矩陣 1 中。

  • 列出矩陣 1 到畫面的內容。

  • 將矩陣 1 中儲存的結果乘以矩陣 3,然後再將結果儲存在矩陣 1 中。

  • 列出矩陣 1 到畫面的內容。

  • 在套用矩陣 1 轉換之前,將矩形繪製到畫面, (藍色矩形) 。

  • 將轉換套用至矩形。

  • 使用與上一個矩形相同的座標,將轉換的矩形繪製到畫面 (紅色矩形) 。

請注意,紅色矩形已依水準方向的兩個因數縮放,然後旋轉 90 度,然後移動 (轉譯) x 方向 250 點,而 Y 方向為 50 點。

public:
   void MultiplyExample( PaintEventArgs^ e )
   {
      Pen^ myPen = gcnew Pen( Color::Blue,1.0f );
      Pen^ myPen2 = gcnew Pen( Color::Red,1.0f );

      // Set up the matrices.
      Matrix^ myMatrix1 = gcnew Matrix( 2.0f,0.0f,0.0f,1.0f,0.0f,0.0f );
      Matrix^ myMatrix2 = gcnew Matrix( 0.0f,1.0f,-1.0f,0.0f,0.0f,0.0f );
      Matrix^ myMatrix3 = gcnew Matrix( 1.0f,0.0f,0.0f,1.0f,250.0f,50.0f );

      // Display the elements of the starting matrix.
      ListMatrixElements( e, myMatrix1, "Beginning Matrix", 6, 40 );

      // Multiply Matrix1 by Matrix 2.
      myMatrix1->Multiply( myMatrix2, MatrixOrder::Append );

      // Display the result of the multiplication of Matrix1 and
      // Matrix2.
      ListMatrixElements( e, myMatrix1, "Matrix After 1st Multiplication", 6, 60 );

      // Multiply the result from the previous multiplication by
      // Matrix3.
      myMatrix1->Multiply( myMatrix3, MatrixOrder::Append );

      // Display the result of the previous multiplication
      // multiplied by Matrix3.
      ListMatrixElements1( e, myMatrix1, "Matrix After 2nd Multiplication", 6, 80 );

      // Draw the rectangle prior to transformation.
      e->Graphics->DrawRectangle( myPen, 0, 0, 100, 100 );

      // Make the transformation.
      e->Graphics->Transform = myMatrix1;

      // Draw the rectangle after transformation.
      e->Graphics->DrawRectangle( myPen2, 0, 0, 100, 100 );
   }

   //-------------------------------------------------------
   // The following function is a helper function to
   // list the contents of a matrix.
   //-------------------------------------------------------
   void ListMatrixElements1( 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;
      System::Drawing::Font^ myFont = gcnew System::Drawing::Font( "Arial",8 );
      SolidBrush^ myBrush = gcnew SolidBrush( Color::Black );

      // Draw the matrix name to the screen.
      e->Graphics->DrawString( String::Concat( matrixName, ":  " ), myFont, myBrush, (float)x, (float)y );

      // Draw the set of path points and types to the screen.
      for ( i = 0; i < numElements; i++ )
      {
         e->Graphics->DrawString( String::Concat( matrix->Elements[ i ], ", " ), myFont, myBrush, (float)X, (float)y );
         X += 30;
      }
   }
public void MultiplyExample(PaintEventArgs e)
{
    Pen myPen = new Pen(Color.Blue, 1);
    Pen myPen2 = new Pen(Color.Red, 1);
             
    // Set up the matrices.
    Matrix myMatrix1 = new Matrix(
        2.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);  
    
    Matrix myMatrix2 = new Matrix(
        0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f); 
  
    Matrix myMatrix3 = new Matrix(
        1.0f, 0.0f, 0.0f, 1.0f, 250.0f, 50.0f);  

    // Display the elements of the starting matrix.
    ListMatrixElements(e, myMatrix1, "Beginning Matrix", 6, 40);
             
    // Multiply Matrix1 by Matrix 2.
    myMatrix1.Multiply(myMatrix2, MatrixOrder.Append);
             
    // Display the result of the multiplication of Matrix1 and
             
    // Matrix2.
    ListMatrixElements(e,
        myMatrix1,
        "Matrix After 1st Multiplication",
        6,
        60);
             
    // Multiply the result from the pervious multiplication by
    // Matrix3.
    myMatrix1.Multiply(myMatrix3, MatrixOrder.Append);
             
    // Display the result of the previous multiplication
    // multiplied by Matrix3.
    ListMatrixElements1(e,
        myMatrix1,
        "Matrix After 2nd Multiplication",
        6,
        80);
             
    // Draw the rectangle prior to transformation.
    e.Graphics.DrawRectangle(myPen, 0, 0, 100, 100);
             
    // Make the transformation.
    e.Graphics.Transform = myMatrix1;
             
    // Draw the rectangle after transformation.
    e.Graphics.DrawRectangle(myPen2, 0, 0, 100, 100);
}
             
//-------------------------------------------------------
// The following function is a helper function to
// list the contents of a matrix.
//-------------------------------------------------------
public void ListMatrixElements1(
    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;
    }
}
Public Sub MultiplyExample(ByVal e As PaintEventArgs)
    Dim myPen As New Pen(Color.Blue, 1)
    Dim myPen2 As New Pen(Color.Red, 1)

    ' Set up the matrices.
    Dim myMatrix1 As New Matrix(2.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F)

    ' Scale.
    Dim myMatrix2 As New Matrix(0.0F, 1.0F, -1.0F, 0.0F, 0.0F, 0.0F)

    ' Rotate 90.
    Dim myMatrix3 As New Matrix(1.0F, 0.0F, 0.0F, 1.0F, 250.0F, 50.0F)

    ' Display the elements of the starting matrix.
    ListMatrixElementsHelper(e, myMatrix1, "Beginning Matrix", 6, 40)

    ' Multiply Matrix1 by Matrix 2.
    myMatrix1.Multiply(myMatrix2, MatrixOrder.Append)

    ' Display the result of the multiplication of Matrix1 and
    ' Matrix2.
    ListMatrixElementsHelper(e, myMatrix1, _
    "Matrix After 1st Multiplication", 6, 60)

    ' Multiply the result from the pervious multiplication by
    ' Matrix3.
    myMatrix1.Multiply(myMatrix3, MatrixOrder.Append)

    ' Display the result of the previous multiplication
    ' multiplied by Matrix3.
    ListMatrixElementsHelper1(e, myMatrix1, _
    "Matrix After 2nd Multiplication", 6, 80)

    ' Draw the rectangle prior to transformation.
    e.Graphics.DrawRectangle(myPen, 0, 0, 100, 100)
    e.Graphics.Transform = myMatrix1

    ' Draw the rectangle after transformation.
    e.Graphics.DrawRectangle(myPen2, 0, 0, 100, 100)
End Sub

' A helper function to list the contents of a matrix.
Public Sub ListMatrixElementsHelper1(ByVal e As PaintEventArgs, _
ByVal matrix As Matrix, ByVal matrixName As String, ByVal numElements As Integer, _
ByVal y As Integer)

    ' Set up variables for drawing the array

    ' of points to the screen.
    Dim i As Integer
    Dim x As Single = 20
    Dim j As Single = 200
    Dim myFont As New Font("Arial", 8)
    Dim myBrush As 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 To numElements - 1
        e.Graphics.DrawString(matrix.Elements(i).ToString() + ", ", _
        myFont, myBrush, j, y)
        j += 30
    Next i
End Sub

備註

如果指定的順序為 Prepend ,這會 Matrix 以前面加上的順序乘以指定的矩陣。 如果指定的順序為 Append ,則會 Matrix 以附加順序乘以指定的矩陣。

適用於