Skip to content

Matrix2D

Canonical path: NemAll_Python_Geometry.Matrix2D

Representation of a 3x3 geometry transformation matrix.

This matrix is used to transform (translate, rotate and scale) 2D geometries and looks like:

\[ \begin{bmatrix} a_{11} & a_{12} & 0 \\ a_{21} & a_{22} & 0 \\ a_{31} & a_{32} & 1 \end{bmatrix} \]
  • The components \(a_{11}...a_{22}\) are responsible for the rotation and scaling.
  • The components \(a_{31}, a_{32}\) are responsible for the translation.

Examples:

Assuming, translation_2 is a Matrix2D that translates by 100 units in each X and Y direction.

Use Transform() function to apply the matrix to a geometry:

>>> geometry = Point2D(1, 1)
>>> Transform(Point2D(1, 1), translation_2)
Point2D(101, 101)

Some geometry objects allow to use multiplication operator *, like:

>>> geometry = Point2D(1, 1)
>>> geometry * translation_2
Point2D(101, 101)

AddDimension

AddDimension() -> Matrix3D

Create a 3D matrix from this 2D matrix

Returns:

  • Matrix3D

    3D Matrix from this matrix

Determinant

Determinant() -> float

Calculate determinant

Returns:

  • float

    Determinant.

IsIdentity

IsIdentity() -> bool

Check to identity matrix

Returns:

  • bool

    Return true when is matrix identity, otherwise false.

Multiply

Multiply(matrix: Matrix2D) -> Matrix2D

Multiply this matrix with another one, according to the formula:

\[ A = A \cdot B \]

Where \(A\) is this matrix and \(B\) is the other matrix.

NOTE: This method is a mutator AND returns the result!

Parameters:

  • matrix (Matrix2D) –

    Matrix to be multiple with

Returns:

Reflection

Reflection(axis: Axis2D)

Adds a reflection to the current matrix

Parameters:

  • axis (Axis2D) –

    Reflection axis

Examples:

A transformation consisting of translation and reflection can be defined like:

>>> translation_reflection = Matrix2D()
>>> translation_reflection.Translate(Vector2D(100, 100))
>>> translation_reflection.Reflection(Axis2D(Point2D(0, 0), Vector2D(0, 1)))
>>> Point2D(2, 2) * translation_reflection
Point2D(-102, 102)

Reverse

Reverse() -> bool

Reverse the matrix

This method provide geometrical inverse matrix and can not be used with regular inverse 3x3 matrix calculations.

Geometrical representation:

\[ (\vec{P} \cdot A) \cdot A^{-1} = \vec{P} \]

Where \(\vec{P}\) is a point and \(A\) is this matrix.

Returns:

  • bool

    True when operation is successful, otherwise false.

Rotation

Rotation(point: Point2D, angle: Angle)

Adds a rotation to the current matrix

Parameters:

  • point (Point2D) –

    Point of rotation

  • angle (Angle) –

    Angle of rotation.

Examples:

A transformation consisting of translation and rotation can be defined like:

>>> translation_rotation = Matrix2D()
>>> translation_rotation.Translate(Vector2D(100, 100))
>>> translation_rotation.Rotation(Point2D(0, 0), Angle(pi/2))
>>> Point2D(2, 2) * translation_rotation
Point2D(-102, 102)

Scaling

Scaling(scaleX: float, scaleY: float)

Adds a scaling to the current matrix

Parameters:

  • scaleX (float) –

    Scale in X axis.

  • scaleY (float) –

    Scale in Y axis.

Examples:

A transformation consisting of translation and scaling can be defined like:

>>> translation_scaling = Matrix2D()
>>> translation_scaling.Translate(Vector2D(100, 100))
>>> translation_scaling.Scaling(2, 3)
>>> Point2D(2, 2) * translation_scaling
Point2D(204, 306)

SetIdentity

SetIdentity()

Set the matrix to identity

SetReflection

SetReflection(axis: Axis2D)

Set the matrix to a reflection matrix

Parameters:

  • axis (Axis2D) –

    Reflection axis

Examples:

A matrix mirroring geometries relative to Y axis can be defined like:

>>> reflection = Matrix2D()
>>> reflection.SetReflection(Axis2D(Point2D(0, 0), Vector2D(0, 1)))
>>> Point2D(2, 2) * reflection
Point2D(-2, 2)

SetRotation

SetRotation(point: Point2D, angle: Angle)

Set the matrix to a rotation matrix

Parameters:

  • point (Point2D) –

    Point of rotation.

  • angle (Angle) –

    Angle of rotation.

Examples:

A matrix rotating geometries by 90° around the origin can be defined like:

>>> rotation = Matrix2D()
>>> rotation.SetRotation(Point2D(0, 0), Angle(pi/2))
>>> Point2D(2, 2) * rotation
Point2D(-2, 2)

SetScaling

SetScaling(scaleX: float, scaleY: float)

Set the matrix to a scaling matrix

Parameters:

  • scaleX (float) –

    Scale in X axis.

  • scaleY (float) –

    Scale in Y axis.

Examples:

A matrix scaling geometries by 2 in X and 3 in Y direction can be defined like:

>>> scaling = Matrix2D()
>>> scaling.SetScaling(2, 3)
>>> Point2D(2, 2) * scaling
Point2D(4, 6)

SetTranslation

SetTranslation(vec: Vector2D)

Set the matrix to a translation matrix

Parameters:

  • vec (Vector2D) –

    Vector of translation.

Examples:

A matrix translating geometries by 100 units in X and Y direction can be defined like:

>>> translation = Matrix2D()
>>> translation.SetTranslation(Vector2D(100, 100))
>>> Point2D(2, 2) * translation
Point2D(102, 102)

SetValue

SetValue(index: int, value: float) -> bool

Set a specific matrix component to a desired value

Parameters:

  • index (int) –

    index from 0 to 8

  • value (float) –

    value to set

Returns:

  • bool

    True when operation successful (index is not out of range), otherwise false.

Translate

Translate(vec: Vector2D)

Adds a translation to the current matrix

Parameters:

  • vec (Vector2D) –

    Vector of translation.

Examples:

A transformation consisting of rotation and translation can be defined like:

>>> rotation_translation = Matrix2D()
>>> rotation_translation.Rotation(Point2D(0, 0), Angle(pi/2))
>>> rotation_translation.Translate(Vector2D(100, 100))
>>> Point2D(2, 2) * rotation_translation
Point2D(98, 102)

__add__

__add__(matrix: Matrix2D) -> Matrix2D

Add two matrices, according to the formula:

\[ C = A + B \]

Where \(A\) is this matrix

__getitem__

__getitem__(index: int) -> float

Get the matrix element at a specified position

This method is checked and throwing Geometry::Exception when index is out of range.

Parameters:

  • index (int) –

    Position index <0..9>

Returns:

  • float

    Returns a matrix element at a specified position.

__iadd__

__iadd__(matrix: Matrix2D) -> Matrix2D

Add two matrices in place, according to the formula:

\[ A = A + B \]

Where \(A\) is this matrix

Parameters:

Returns:

__imul__

__imul__(matrix: Matrix2D) -> Matrix2D

Multiply two matrices in place, according to the formula:

\[ A = A \cdot B \]

Where \(A\) is this matrix

Parameters:

Returns:

__init__ overloaded

__init__()

Initialize

__init__(matrix: Matrix2D)

Copy constructor

Parameters:

  • matrix (Matrix2D) –

    Matrix which will be copied.

__isub__

__isub__(matrix: Matrix2D) -> Matrix2D

Subtract two matrices in place, according to the formula:

\[ A = A - B \]

Where \(A\) is this matrix

Parameters:

Returns:

__mul__

__mul__(matrix: Matrix2D) -> Matrix2D

Multiply two matrices, according to the formula:

\[ C = A \cdot B \]

Where \(A\) is this matrix

Parameters:

Returns:

Examples:

When:

  • rotation_3 is a matrix that rotates by 90° around the origin.
  • translation_2 is a matrix that translates by 100 units in each X and Y direction.

A matrix that first rotates, then translates can be defined like:

>>> rotation_3 * translation_2
Matrix2D(
   0 1 0
   -1 0 0
   100 100 1)

__repr__

__repr__() -> str

Convert to string

__sub__

__sub__(matrix: Matrix2D) -> Matrix2D

Subtract two matrices, according to the formula:

\[ C = A - B \]

Where \(A\) is this matrix

Parameters:

Returns:

Placeholder