std/linear/matrix2
std/linear/src/matrix2.trb
Matrix2, the 2x2 matrix: the linear part of a transformation in the plane - rotation, scale, shear - without a
translation.
The fields are the columns, and a column is where a basis vector lands. That is the whole of what a matrix is, so
reading one is reading two vectors, and xAxis is matrix * unitX.
type Matrix2
type Matrix2<Scalar: Numeric = Float> with Add, Subtract, Multiply
A linear transformation of the plane, held as the two vectors its basis lands on.
Vectors are columns and a transformation is applied on the left (matrix * vector), which is the convention of
mathematics and of every graphics API that says "column-major": composing a after b is a * b.
Examples
const turn = Matrix2.rotation by: Angle.degrees(90.0)
print turn.applied(to: Vector2(1.0, 0.0)).isCloseTo(Vector2(0.0, 1.0), tolerance: 0.0001)
Related
Matrix3- the same with a translation, for the plane.Vector2.rotated- a rotation of one vector, without building a matrix for it.
field xAxis
xAxis: Vector2<Scalar>
Where the first basis vector lands.
field yAxis
yAxis: Vector2<Scalar>
Where the second basis vector lands.
const identity
static identity: Matrix2<Scalar>
The transformation that changes nothing, over whichever scalar is asked for: Matrix2<Int>.identity.
fn scaling
static fn scaling(by: Vector2<Scalar>): Matrix2<Scalar>
A diagonal matrix: each axis scaled on its own, and nothing mixed.
fn transposed
fn transposed(): Matrix2<Scalar>
The two rows read as columns: the inverse of a rotation, and half of the inverse of anything else.
fn determinant
fn determinant(): Scalar
The factor the transformation multiplies an area by. Zero exactly where it collapses the plane onto a line.
fn at
fn at(row: Int, column: Int): Scalar
The cell in that row and that column. Panics outside 0..2.
fn add
fn add(other: Matrix2<Scalar>): Matrix2<Scalar>
Cell by cell. Adding two transformations is not composing them; multiply is.
fn subtract
fn subtract(other: Matrix2<Scalar>): Matrix2<Scalar>
Cell by cell.
fn multiply
fn multiply(other: Matrix2<Scalar>): Matrix2<Scalar>
The composition: self after other.
fn applied
fn applied(to: Vector2<Scalar>): Vector2<Scalar>
The vector transformed: the column combination x * xAxis + y * yAxis.
It is a method and not matrix * vector, because a type has one namespace of members and multiply is already the
composition of two matrices.
extend Matrix2<Scalar> with Negate
extend<Scalar: Signed> Matrix2<Scalar> with Negate
Every cell turned around.
fn negate
fn negate(): Matrix2<Scalar>
Every cell with its sign flipped.
extend Matrix2<Scalar>
extend<Scalar: Real> Matrix2<Scalar>
What an angle and a division buy: rotations and the way back.
fn rotation
static fn rotation(by: Angle<Scalar>): Matrix2<Scalar>
The rotation by that angle, from the first axis towards the second.
fn inverse
fn inverse(): Matrix2<Scalar>?
The transformation that undoes this one, or None where there is none - which is exactly where the determinant is
zero and the plane has been collapsed onto a line.
fn isCloseTo
fn isCloseTo(other: Matrix2<Scalar>, tolerance: Scalar): Bool
Whether every cell is within tolerance of the other matrix's.
extend Matrix2<Scalar> with Power<Int64>
extend<Scalar: Numeric> Matrix2<Scalar> with Power<Int64>
A whole power: the transformation applied that many times over.
fn power
fn power(exponent: Int64): Matrix2<Scalar>
turn ** 3 is turn * turn * turn, by squaring, and matrix ** 0 is the identity.
Panics
On a negative exponent, which is a power of the inverse: a matrix has one only over a Real scalar and only where
its determinant is not zero, so the caller asks for inverse() and raises that.