Struct Matrix
- Namespace
- Avalonia
- Assembly
- Avalonia.Base.dll
A 3x3 matrix.
public readonly struct Matrix : IEquatable<Matrix>
- Implements
- Inherited Members
Remarks
Matrix layout: | 1st col | 2nd col | 3r col | 1st row | scaleX | skewY | perspX | 2nd row | skewX | scaleY | perspY | 3rd row | transX | transY | perspZ |
Note: Skia.SkMatrix uses a transposed layout (where for example skewX/skewY and persp0/transX are swapped).
Constructors
Matrix(double, double, double, double, double, double)
Initializes a new instance of the Matrix struct (equivalent to a 2x3 Matrix without perspective).
public Matrix(double scaleX, double skewY, double skewX, double scaleY, double offsetX, double offsetY)
Parameters
scaleX
doubleThe first element of the first row.
skewY
doubleThe second element of the first row.
skewX
doubleThe first element of the second row.
scaleY
doubleThe second element of the second row.
offsetX
doubleThe first element of the third row.
offsetY
doubleThe second element of the third row.
Matrix(double, double, double, double, double, double, double, double, double)
Initializes a new instance of the Matrix struct.
public Matrix(double scaleX, double skewY, double perspX, double skewX, double scaleY, double perspY, double offsetX, double offsetY, double perspZ)
Parameters
scaleX
doubleThe first element of the first row.
skewY
doubleThe second element of the first row.
perspX
doubleThe third element of the first row.
skewX
doubleThe first element of the second row.
scaleY
doubleThe second element of the second row.
perspY
doubleThe third element of the second row.
offsetX
doubleThe first element of the third row.
offsetY
doubleThe second element of the third row.
perspZ
doubleThe third element of the third row.
Properties
HasInverse
HasInverse Property - returns true if this matrix is invertible, false otherwise.
public bool HasInverse { get; }
Property Value
Identity
Returns the multiplicative identity matrix.
public static Matrix Identity { get; }
Property Value
IsIdentity
Returns whether the matrix is the identity matrix.
public bool IsIdentity { get; }
Property Value
M11
The first element of the first row (scaleX).
public double M11 { get; }
Property Value
M12
The second element of the first row (skewY).
public double M12 { get; }
Property Value
M13
The third element of the first row (perspX: input x-axis perspective factor).
public double M13 { get; }
Property Value
M21
The first element of the second row (skewX).
public double M21 { get; }
Property Value
M22
The second element of the second row (scaleY).
public double M22 { get; }
Property Value
M23
The third element of the second row (perspY: input y-axis perspective factor).
public double M23 { get; }
Property Value
M31
The first element of the third row (offsetX/translateX).
public double M31 { get; }
Property Value
M32
The second element of the third row (offsetY/translateY).
public double M32 { get; }
Property Value
M33
The third element of the third row (perspZ: perspective scale factor).
public double M33 { get; }
Property Value
Methods
Append(Matrix)
Appends another matrix as post-multiplication operation. Equivalent to this * value;
public Matrix Append(Matrix value)
Parameters
value
MatrixA matrix.
Returns
- Matrix
Post-multiplied matrix.
ContainsPerspective()
Determines if the current matrix contains perspective (non-affine) transforms (true) or only (affine) transforms that could be mapped into an 2x3 matrix (false).
public bool ContainsPerspective()
Returns
CreateRotation(double)
Creates a rotation matrix using the given rotation in radians.
public static Matrix CreateRotation(double radians)
Parameters
radians
doubleThe amount of rotation, in radians.
Returns
- Matrix
A rotation matrix.
CreateRotation(double, Point)
Creates a rotation matrix using the given rotation in radians around center point.
public static Matrix CreateRotation(double radians, Point center)
Parameters
Returns
CreateScale(Vector)
Creates a scale matrix from the given vector scale.
public static Matrix CreateScale(Vector scales)
Parameters
scales
VectorThe scale to use.
Returns
- Matrix
A scaling matrix.
CreateScale(double, double)
Creates a scale matrix from the given X and Y components.
public static Matrix CreateScale(double xScale, double yScale)
Parameters
Returns
- Matrix
A scaling matrix.
CreateSkew(double, double)
Creates a skew matrix from the given axis skew angles in radians.
public static Matrix CreateSkew(double xAngle, double yAngle)
Parameters
xAngle
doubleThe amount of skew along the X-axis, in radians.
yAngle
doubleThe amount of skew along the Y-axis, in radians.
Returns
- Matrix
A rotation matrix.
CreateTranslation(Vector)
Creates a translation matrix from the given vector.
public static Matrix CreateTranslation(Vector position)
Parameters
position
VectorThe translation position.
Returns
- Matrix
A translation matrix.
CreateTranslation(double, double)
Creates a translation matrix from the given X and Y components.
public static Matrix CreateTranslation(double xPosition, double yPosition)
Parameters
Returns
- Matrix
A translation matrix.
Equals(Matrix)
Returns a boolean indicating whether the matrix is equal to the other given matrix.
public bool Equals(Matrix other)
Parameters
other
MatrixThe other matrix to test equality against.
Returns
- bool
True if this matrix is equal to other; False otherwise.
Equals(object?)
Returns a boolean indicating whether the given Object is equal to this matrix instance.
public override bool Equals(object? obj)
Parameters
obj
objectThe Object to compare against.
Returns
- bool
True if the Object is equal to this matrix; False otherwise.
GetDeterminant()
Calculates the determinant for this matrix.
public double GetDeterminant()
Returns
- double
The determinant.
Remarks
The determinant is calculated by expanding the matrix with a third column whose values are (0,0,1).
GetHashCode()
Returns the hash code for this instance.
public override int GetHashCode()
Returns
- int
The hash code.
Invert()
Inverts the Matrix.
public Matrix Invert()
Returns
- Matrix
The inverted matrix.
Exceptions
- InvalidOperationException
Matrix is not invertible.
Parse(string)
Parses a Matrix string.
public static Matrix Parse(string s)
Parameters
s
stringSix or nine comma-delimited double values (m11, m12, m21, m22, offsetX, offsetY[, perspX, perspY, perspZ]) that describe the new Matrix
Returns
Prepend(Matrix)
Prepends another matrix as pre-multiplication operation. Equivalent to value * this;
public Matrix Prepend(Matrix value)
Parameters
value
MatrixA matrix.
Returns
- Matrix
Pre-multiplied matrix.
ToRadians(double)
Converts an angle in degrees to radians.
public static double ToRadians(double angle)
Parameters
angle
doubleThe angle in degrees.
Returns
- double
The angle in radians.
ToString()
Returns a String representing this matrix instance.
public override string ToString()
Returns
- string
The string representation.
Transform(Point)
Transforms the point with the matrix
public Point Transform(Point p)
Parameters
p
PointThe point to be transformed
Returns
- Point
The transformed point
TryDecomposeTransform(Matrix, out Decomposed)
Decomposes given matrix into transform operations.
public static bool TryDecomposeTransform(Matrix matrix, out Matrix.Decomposed decomposed)
Parameters
matrix
MatrixMatrix to decompose.
decomposed
Matrix.DecomposedDecomposed matrix.
Returns
- bool
The status of the operation.
TryInvert(out Matrix)
Attempts to invert the Matrix.
public bool TryInvert(out Matrix inverted)
Parameters
inverted
Matrix
Returns
Operators
operator ==(Matrix, Matrix)
Returns a boolean indicating whether the given matrices are equal.
public static bool operator ==(Matrix value1, Matrix value2)
Parameters
Returns
- bool
True if the matrices are equal; False otherwise.
operator !=(Matrix, Matrix)
Returns a boolean indicating whether the given matrices are not equal.
public static bool operator !=(Matrix value1, Matrix value2)
Parameters
Returns
- bool
True if the matrices are not equal; False if they are equal.
operator *(Matrix, Matrix)
Multiplies two matrices together and returns the resulting matrix.
public static Matrix operator *(Matrix value1, Matrix value2)
Parameters
Returns
- Matrix
The product matrix.
operator -(Matrix)
Negates the given matrix by multiplying all values by -1.
public static Matrix operator -(Matrix value)
Parameters
value
MatrixThe source matrix.
Returns
- Matrix
The negated matrix.