Package codedraw
Class Matrix2D
java.lang.Object
codedraw.Matrix2D
This class is used to transform the input coordinates.
You can pass matrices with different transformations to
Image.setTransformation(Matrix2D)
and
Image.setTransformation(Matrix2D)
.
All methods except for the multiply(Matrix2D)
method apply their operations left-to-right.
multiply(Matrix2D)
applies its operation right-to-left like in normal matrix multiplication.-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Compares this matrix to the matrix given as a parameter.boolean
static Matrix2D
fromColumnMajor
(double[][] matrix) Creates a new matrix from a 3x3 column major matrix.static Matrix2D
fromColumnMajor
(double r0c0, double r1c0, double r2c0, double r0c1, double r1c1, double r2c1, double r0c2, double r1c2, double r2c2) Creates a new matrix from a 3x3 column major matrix.static Matrix2D
fromRowMajor
(double[][] matrix) Creates a new matrix from a 3x3 row major matrix.static Matrix2D
fromRowMajor
(double r0c0, double r0c1, double r0c2, double r1c0, double r1c1, double r1c2, double r2c0, double r2c1, double r2c2) Creates a new matrix from a 3x3 row major matrix.double
get
(int row, int column) Gets a value from this matrix.int
hashCode()
inverse()
Calculates the inverse of this matrix.mirror
(double angleRadians) Mirrors the coordinate system at the (0, 0) coordinate.mirrorAt
(double x, double y, double angleRadians) Mirrors the coordinate system at the specified coordinate.multiply
(double x, double y) Transforms the point given as a coordinate according to this matrix.Multiplies two matrices with each other.Transforms the point given as a coordinate according to this matrix.rotate
(double angleRadians) Rotates the coordinate system at the (0, 0) coordinate.rotateAt
(double x, double y, double radians) Rotates the coordinate system at the specified coordinate.scale
(double xScale, double yScale) Scales the coordinate system at the (0, 0) coordinate.scaleAt
(double x, double y, double scaleX, double scaleY) Scales the coordinate system from the specified coordinates.set
(int row, int column, double value) Creates a new matrix and changes the value in the matrix at the specified location.shear
(double shearX, double shearY) Shears the coordinate system from the (0, 0) coordinate.shearAt
(double x, double y, double shearX, double shearY) Shears the coordinate system at the specified coordinate.toString()
translate
(double tx, double ty) Moves the coordinate system in the x and y direction.
-
Field Details
-
ZERO
The zero matrix where all values are 0. -
IDENTITY
The identity matrix. This matrix would not modify a coordinate system.
-
-
Method Details
-
fromRowMajor
Creates a new matrix from a 3x3 row major matrix. If in doubt you probably want to use row-major as it visually aligns with most examples.- Parameters:
matrix
- 3x3 matrix- Returns:
- the matrix.
-
fromColumnMajor
Creates a new matrix from a 3x3 column major matrix. If in doubt you probably want to use row-major as it visually aligns with most examples.- Parameters:
matrix
- 3x3 matrix- Returns:
- the matrix.
-
fromRowMajor
public static Matrix2D fromRowMajor(double r0c0, double r0c1, double r0c2, double r1c0, double r1c1, double r1c2, double r2c0, double r2c1, double r2c2) Creates a new matrix from a 3x3 row major matrix. If in doubt you probably want to use row-major as it visually aligns with most examples.- Returns:
- the matrix.
-
fromColumnMajor
public static Matrix2D fromColumnMajor(double r0c0, double r1c0, double r2c0, double r0c1, double r1c1, double r2c1, double r0c2, double r1c2, double r2c2) Creates a new matrix from a 3x3 column major matrix. If in doubt you probably want to use row-major as it visually aligns with most examples.- Returns:
- the matrix.
-
get
public double get(int row, int column) Gets a value from this matrix.- Parameters:
row
- Can be either 0, 1 or 2.column
- Can be either 0, 1 or 2.- Returns:
- The value.
-
set
Creates a new matrix and changes the value in the matrix at the specified location.- Parameters:
row
- Can be either 0, 1 or 2.column
- Can be either 0, 1 or 2.value
- The value to be set at the specified location.- Returns:
- The new matrix with the changed value.
-
inverse
-
translate
Moves the coordinate system in the x and y direction.- Parameters:
tx
- The change in the x direction.ty
- The change in the y direction.- Returns:
- A new matrix with the translated coordinate system.
-
rotate
Rotates the coordinate system at the (0, 0) coordinate.- Parameters:
angleRadians
- Angle in radians. The angle rotates objects clockwise.- Returns:
- The rotated matrix.
-
rotateAt
Rotates the coordinate system at the specified coordinate.- Parameters:
x
- Coordinate to rotate around.y
- Coordinate to rotate around.radians
- Angle in radians. The angle rotates objects clockwise.- Returns:
- The rotated matrix.
-
scale
Scales the coordinate system at the (0, 0) coordinate.- Parameters:
xScale
- The scale in the x direction.yScale
- The scale in the y direction.- Returns:
- A new matrix with the scaled coordinate system.
-
scaleAt
Scales the coordinate system from the specified coordinates.- Parameters:
x
- The coordinate to scale from.y
- The coordinate to scale from.scaleX
- The scale in the x direction.scaleY
- The scale in the y direction.- Returns:
- A new matrix with the scaled coordinate system.
-
shear
Shears the coordinate system from the (0, 0) coordinate.- Parameters:
shearX
- The shear in the x direction.shearY
- The shear in the y direction.- Returns:
- A new matrix with the sheared coordinate system.
-
shearAt
Shears the coordinate system at the specified coordinate.- Parameters:
x
- The coordinate to shear from.y
- The coordinate to shear from.shearX
- The shear in the x direction.shearY
- The shear in the y direction.- Returns:
- A new matrix with the sheared coordinate system.
-
mirror
Mirrors the coordinate system at the (0, 0) coordinate. An angle with the value 0 would mirror the coordinate system along the x-axis. An angle with the value Math.PI / 2 would mirror the coordinate system along the y-axis.- Parameters:
angleRadians
- The angle which represents an infinite line that passes through the (0, 0) coordinate. The mirror line- Returns:
- A new matrix with the mirrored coordinate system.
-
mirrorAt
Mirrors the coordinate system at the specified coordinate. An angle with the value 0 would mirror the coordinate system along the x-axis. An angle with the value Math.PI / 2 would mirror the coordinate system along the y-axis.- Parameters:
x
- The x coordinate where the mirror line goes through.y
- The y coordinate where the mirror line goes through.angleRadians
- The angle which represents an infinite line that passes through the specified coordinate.- Returns:
- A new matrix with the mirrored coordinate system.
-
multiply
Multiplies two matrices with each other. This multiply method works the same as normal matrix multiplication. This means that the operational order would be right-to-left.- Parameters:
other
- The other matrix to multiply this matrix with.- Returns:
- The multiplied matrix as the result.
-
multiply
-
multiply
Transforms the point given as a coordinate according to this matrix.- Parameters:
x
- the x coordinate of the pointy
- the y coordinate of the point- Returns:
- The transformed point.
-
equals
-
equals
Compares this matrix to the matrix given as a parameter. The error allows for some difference between their values. For example if the first value of this matrix is 1 and the first values of the other matrix is 0.999998 and an error of 0.0001 is given then this function will return true.equals(Object)
would return false. However, if a value of this matrix is 1 and the same value is 2 in the other matrix and the error is 0.0001 this function andequals(Object)
would both return false.- Parameters:
other
- The matrix to compare with.error
- The maximum difference allowed by the values in the matrix.- Returns:
- Whether these two matrices are the same within some error.
-
hashCode
-
toString
-