Package codedraw

Class Matrix2D

java.lang.Object
codedraw.Matrix2D

public class Matrix2D extends Object
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

    Fields
    Modifier and Type
    Field
    Description
    static final Matrix2D
    The identity matrix.
    static final Matrix2D
    The zero matrix where all values are 0.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Matrix2D(double[][] matrix)
    Creates a new matrix from a 3x3 double array.
    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.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    equals(Matrix2D other, double error)
    Compares this matrix to the matrix given as a parameter.
    boolean
     
    double
    get(int row, int column)
    Gets a value from this matrix.
    int
     
    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.
     
    translate(double tx, double ty)
    Moves the coordinate system in the x and y direction.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • ZERO

      public static final Matrix2D ZERO
      The zero matrix where all values are 0.
    • IDENTITY

      public static final Matrix2D 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

      public Matrix2D set(int row, int column, double value)
      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

      public Matrix2D inverse()
      Calculates the inverse of this matrix. A matrix M times its inverse is the IDENTITY matrix.
      Returns:
      The inverse of this matrix.
    • translate

      public Matrix2D translate(double tx, double ty)
      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

      public Matrix2D rotate(double angleRadians)
      Rotates the coordinate system at the (0, 0) coordinate.
      Parameters:
      angleRadians - Angle in radians. The angle goes counter-clockwise.
      Returns:
      The rotated matrix.
    • rotateAt

      public Matrix2D rotateAt(double x, double y, double radians)
      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

      public Matrix2D scale(double xScale, double yScale)
      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

      public Matrix2D scaleAt(double x, double y, double scaleX, double scaleY)
      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

      public Matrix2D shear(double shearX, double shearY)
      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

      public Matrix2D shearAt(double x, double y, double shearX, double shearY)
      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

      public Matrix2D mirror(double angleRadians)
      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

      public Matrix2D mirrorAt(double x, double y, double angleRadians)
      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

      public Matrix2D multiply(Matrix2D other)
      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

      public Point2D multiply(Point2D point)
      Transforms the point given as a coordinate according to this matrix.
      Parameters:
      point - A point.
      Returns:
      The transformed point.
    • multiply

      public Point2D multiply(double x, double y)
      Transforms the point given as a coordinate according to this matrix.
      Parameters:
      x - the x coordinate of the point
      y - the y coordinate of the point
      Returns:
      The transformed point.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • equals

      public boolean equals(Matrix2D other, double error)
      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 and equals(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()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object