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
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Compares this matrix to the matrix given as a parameter.boolean
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.
-
-
Constructor Details
-
Matrix2D
public Matrix2D(double[][] matrix) Creates a new matrix from a 3x3 double array.- Parameters:
matrix
- A 3x3 double array.
-
Matrix2D
public Matrix2D(double v00, double v01, double v02, double v10, double v11, double v12, double v20, double v21, double v22) Creates a new matrix from the values provided.- Parameters:
v00
- The value at position (0, 0).v01
- The value at position (0, 1).v02
- The value at position (0, 2).v10
- The value at position (1, 0).v11
- The value at position (1, 1).v12
- The value at position (1, 2).v20
- The value at position (2, 0).v21
- The value at position (2, 1).v22
- The value at position (2, 2).
-
-
Method Details
-
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
Calculates the inverse of this matrix. A matrix M times its inverse is theIDENTITY
matrix.- Returns:
- The inverse of this matrix.
-
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 goes counter-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.- 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.- 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
Transforms the point given as a coordinate according to this matrix.- Parameters:
point
- A point.- Returns:
- The transformed point.
-
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
public int hashCode() -
toString
-