Matrix¶
class Matrix()¶
Matrix is used throughout cairo to convert between different coordinate spaces. A Matrix holds an affine transformation, such as a scale, rotation, shear, or a combination of these. The transformation of a point (x,y) is given by:
x_new = xx * x + xy * y + x0
y_new = yx * x + yy * y + y0
The current transformation matrix of a Context
, represented as a
Matrix, defines the transformation from userspace coordinates to devicespace
coordinates.
Some standard Python operators can be used with matrices:
To read the values from a Matrix:
xx, yx, xy, yy, x0, y0 = matrix
To multiply two matrices:
matrix3 = matrix1.multiply(matrix2)
# or equivalently
matrix3 = matrix1 * matrix2
To compare two matrices:
matrix1 == matrix2
matrix1 != matrix2
For more information on matrix transformation see https://www.cairographics.org/cookbook/matrix_transform/

class
cairo.
Matrix
(xx = 1.0, yx = 0.0, xy = 0.0, yy = 1.0, x0 = 0.0, y0 = 0.0)¶ Parameters:  xx (float) – xx component of the affine transformation
 yx (float) – yx component of the affine transformation
 xy (float) – xy component of the affine transformation
 yy (float) – yy component of the affine transformation
 x0 (float) – X translation component of the affine transformation
 y0 (float) – Y translation component of the affine transformation
Create a new Matrix with the affine transformation given by xx, yx, xy, yy, x0, y0. The transformation is given by:
x_new = xx * x + xy * y + x0 y_new = yx * x + yy * y + y0
To create a new identity matrix:
matrix = cairo.Matrix()
To create a matrix with a transformation which translates by tx and ty in the X and Y dimensions, respectively:
matrix = cairo.Matrix(x0=tx, y0=ty)
To create a matrix with a transformation that scales by sx and sy in the X and Y dimensions, respectively:
matrix = cairo.Matrix(xx=sy, yy=sy)

classmethod
init_rotate
(radians)¶ Parameters: radians (float) – angle of rotation, in radians. The direction of rotation is defined such that positive angles rotate in the direction from the positive X axis toward the positive Y axis. With the default axis orientation of cairo, positive angles rotate in a clockwise direction. Returns: a new Matrix set to a transformation that rotates by radians.

invert
()¶ Returns: If Matrix has an inverse, modifies Matrix to be the inverse matrix and returns None Raises: cairo.Error
if the Matrix as no inverseChanges Matrix to be the inverse of it’s original value. Not all transformation matrices have inverses; if the matrix collapses points together (it is degenerate), then it has no inverse and this function will fail.

multiply
(matrix2)¶ Parameters: matrix2 (cairo.Matrix) – a second matrix Returns: a new Matrix Multiplies the affine transformations in Matrix and matrix2 together. The effect of the resulting transformation is to first apply the transformation in Matrix to the coordinates and then apply the transformation in matrix2 to the coordinates.
It is allowable for result to be identical to either Matrix or matrix2.

rotate
(radians)¶ Parameters: radians (float) – angle of rotation, in radians. The direction of rotation is defined such that positive angles rotate in the direction from the positive X axis toward the positive Y axis. With the default axis orientation of cairo, positive angles rotate in a clockwise direction. Initialize Matrix to a transformation that rotates by radians.

scale
(sx, sy)¶ Parameters: Applies scaling by sx, sy to the transformation in Matrix. The effect of the new transformation is to first scale the coordinates by sx and sy, then apply the original transformation to the coordinates.

transform_distance
(dx, dy)¶ Parameters: Returns: the transformed distance vector (dx,dy), both float
Return type: Transforms the distance vector (dx,dy) by Matrix. This is similar to
transform_point()
except that the translation components of the transformation are ignored. The calculation of the returned vector is as follows:dx2 = dx1 * a + dy1 * c dy2 = dx1 * b + dy1 * d
Affine transformations are position invariant, so the same vector always transforms to the same vector. If (x1,y1) transforms to (x2,y2) then (x1+dx1,y1+dy1) will transform to (x1+dx2,y1+dy2) for all values of x1 and x2.

transform_point
(x, y)¶ Parameters: Returns: the transformed point (x,y), both float
Return type: Transforms the point (x, y) by Matrix.

translate
(tx, ty)¶ Parameters: Applies a transformation by tx, ty to the transformation in Matrix. The effect of the new transformation is to first translate the coordinates by tx and ty, then apply the original transformation to the coordinates.