Skip to content

Matrix2D

Class full path: NemAll_Python_Geometry.Matrix2D

Representation class for 2D matrix

Matrix2D describes transformation (translation, rotation and scaling) of a two dimensional element

Matrix data organization in memory

|0, 1, 2|
|3, 4, 5|
|6, 7, 8|
  • [0..8] indexes of array values
  • 0,1, 3,4 - rotation, scaling and shrinking
  • 6,7 - translation

All operations are correct only in geometry calculation context and cannot be used for calculating with regular 3x3 matrix.

Functions

AddDimension()

Create a 3D matrix from this 2D matrix

Returns:

Type Description
Matrix3D

3D Matrix from this matrix

Determinant()

Calculate determinant

Returns:

Type Description
float

Determinant.

IsIdentity()

Check to identity matrix

Returns:

Type Description
bool

Return true when is matrix identity, otherwise false.

Multiply(matrix)

Multiple matrix with given matrix

calling " A.Multiply(B) " is identical with " A *= B "

Parameters:

Name Type Description Default
matrix Matrix2D

Matrix to be multiple with

required

Returns:

Type Description
Matrix2D

Product of matrices

Reflection(axis)

Reflection across a axis of given angle

Parameters:

Name Type Description Default
axis Axis2D

Reflection axis

required

Examples:

Reflection around the y axis

>>> matrix = Matrix2D()
>>> matrix.Reflection(Axis2D(Point2D(0, 0), Vector2D(0.0, 1.0)))
    [-1, 0,  0
     0,  1,  0
     0,  0,  1]

Reverse()

Reverse matrix

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

Returns:

Type Description
bool

True when operation is successful, otherwise false.

Examples:

when reversed_matrix is the inverted matrix, this expression will return True

>>> geometry == geometry * matrix * reversed_matrix
    True

Rotation(point, angle)

Rotate the matrix

Args:s point: Point of rotation

angle:  Angle of rotation.

Examples:

>>> matrix = Matrix2D()
>>> matrix.SetRotation(Point2D(0, 0), Angle(math.pi/2))
    [0,  1,  0
    -1,  0,  0
     0,  0,  1]
>>> matrix.Rotation(Point2D(0, 0), Angle(math.pi/2))
    [-1, 0,  0
     0, -1,  0
     0,  0,  1]

Scaling(scaleX, scaleY)

Scale the matrix

Parameters:

Name Type Description Default
scaleX float

Scale in X axis.

required
scaleY float

Scale in Y axis.

required

Examples:

>>> matrix = Matrix2D()
>>> matrix.Scaling(2,4)
    [2,  0,  0
     0,  4,  0
     0,  0,  1]

SetIdentity()

Initialize identity matrix

SetReflection(axis)

Initialize matrix only with reflection

Parameters:

Name Type Description Default
axis Axis2D

Reflection axis

required

Examples:

Reflection around the x axis

>>> matrix = Matrix2D()
>>> matrix.SetReflection(Axis2D(Point2D(0, 0), AllplanGeo.Vector2D(1.0, 0.0)))
    [1,  0,  0
     0, -1,  0
     0,  0,  1]

SetRotation(point, angle)

Initialize matrix only with rotation

Parameters:

Name Type Description Default
point Point2D

Point of rotation.

required
angle Angle

Angle of rotation.

required

Examples:

>>> matrix = Matrix2D()
>>> matrix.SetRotation(Point2D(0, 0), Angle(math.pi/2))
    [0,  1,  0
    -1,  0,  0
     0,  0,  1]

SetScaling(scaleX, scaleY)

Initialize matrix only with scaling factors

Parameters:

Name Type Description Default
scaleX float

Scale in X axis.

required
scaleY float

Scale in Y axis.

required

Examples:

>>> matrix = Matrix2D()
>>> matrix.SetScaling(2,4)
    [2,  0,  0
     0,  4,  0
     0,  0,  1]

SetTranslation(vec)

Initialize matrix only with translation

Parameters:

Name Type Description Default
vec Vector2D

Vector of translation.

required

Examples:

>>> matrix = Matrix2D()
>>> matrix.SetTranslation(Vector2D(100, 100))
    [1,  0,  0
     0,  1,  0
     100,100,1]

SetValue(index, value)

Set the matrix element at a specified position

Use this method when you don't want to catch exception by operator[].

Parameters:

Name Type Description Default
index int

Position index <0..9>

required
value float

Value for set

required

Returns:

Type Description
bool

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

Translate(vec)

Translate the matrix

Parameters:

Name Type Description Default
vec Vector2D

Vector of translation.

required

Examples:

>>> matrix = Matrix3D()
>>> matrix.SetTranslation(Vector2D(100, 100))
    [1,  0,  0
     0,  1,  0
     100,100,1]
>>> matrix.Translate(Vector2D(100, 100))
    [1,  0,  0
     0,  1,  0
     200,200,1]

__add__(matrix)

Matrix addition

Formula: Result(new matrix) = A+B A is this matrix.

Parameters:

Name Type Description Default
matrix Matrix2D

B matrix

required

Returns:

Type Description
Matrix2D

Addition of matrices

__getitem__(index)

Get the matrix element at a specified position

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

Parameters:

Name Type Description Default
index int

Position index <0..9>

required

Returns:

Type Description
float

Returns an element at a specified position.

__iadd__(matrix)

Matrix addition

Formula: A += B or A = A+B A is this matrix.

Parameters:

Name Type Description Default
matrix Matrix2D

B matrix

required

Returns:

Type Description
Matrix2D

Addition of matrices

__imul__(matrix)

Matrix multiplication

Formula: A = B or A = AB A is this matrix.

Parameters:

Name Type Description Default
matrix Matrix2D

B matrix

required

Returns:

Type Description
Matrix2D

Product of matrices

__init__ overload

__init__()

initialize

Examples:

Initializing the matrix will result in an identity 3x3 matrix

>>> Matrix2D()
    [1,0,0
     0,1,0
     0,0,1]
__init__(matrix)

Copy constructor

Parameters:

Name Type Description Default
matrix Matrix2D

Matrix which will be copied.

required

__isub__(matrix)

Matrix addition

Formula: A -= B or A = A-B A is this matrix.

Parameters:

Name Type Description Default
matrix Matrix2D

B matrix

required

Returns:

Type Description
Matrix2D

Addition of matrices

__mul__(matrix)

Matrix multiplication

Formula: Result(new matrix) = A*B A is this matrix.

Parameters:

Name Type Description Default
matrix Matrix2D

B matrix

required

Returns:

Type Description
Matrix2D

Product of matrices

__repr__()

Convert to string

__sub__(matrix)

Matrix addition

Formula: Result(new matrix) = A-B A is this matrix.

Parameters:

Name Type Description Default
matrix Matrix2D

B matrix

required

Returns:

Type Description
Matrix2D

Addition of matrices