Class AffineTransformMatrix1D

All Implemented Interfaces:
Function<Vector1D,Vector1D>, UnaryOperator<Vector1D>, Transform<Vector1D>, EuclideanTransform<Vector1D>

public final class AffineTransformMatrix1D extends AbstractAffineTransformMatrix<Vector1D,AffineTransformMatrix1D>
Class using a matrix to represent affine transformations in 1 dimensional Euclidean space.

Instances of this class use a 2x2 matrix for all transform operations. The last row of this matrix is always set to the values [0 1] and so is not stored. Hence, the methods in this class that accept or return arrays always use arrays containing 2 elements, instead of 4.

  • Field Details

    • NUM_ELEMENTS

      private static final int NUM_ELEMENTS
      The number of internal matrix elements.
      See Also:
    • MATRIX_START

      private static final String MATRIX_START
      String used to start the transform matrix string representation.
      See Also:
    • MATRIX_END

      private static final String MATRIX_END
      String used to end the transform matrix string representation.
      See Also:
    • ELEMENT_SEPARATOR

      private static final String ELEMENT_SEPARATOR
      String used to separate elements in the matrix string representation.
      See Also:
    • IDENTITY_INSTANCE

      private static final AffineTransformMatrix1D IDENTITY_INSTANCE
      Shared transform set to the identity matrix.
    • m00

      private final double m00
      Transform matrix entry m0,0.
    • m01

      private final double m01
      Transform matrix entry m0,1.
  • Constructor Details

    • AffineTransformMatrix1D

      private AffineTransformMatrix1D(double m00, double m01)
      Simple constructor; sets all internal matrix elements.
      Parameters:
      m00 - matrix entry m0,0
      m01 - matrix entry m0,1
  • Method Details

    • toArray

      public double[] toArray()
      Return a 2 element array containing the variable elements from the internal transformation matrix. The elements are in row-major order. The array indices map to the internal matrix as follows:
            [
                arr[0],   arr[1],
                0         1
            ]
       
      Returns:
      2 element array containing the variable elements from the internal transformation matrix
    • apply

      public Vector1D apply(Vector1D vec)
    • applyX

      public double applyX(double x)
      Apply this transform to the given point coordinate and return the transformed x value. The return value is equal to (x * m00) + m01.
      Parameters:
      x - x coordinate value
      Returns:
      transformed x coordinate value
      See Also:
    • applyVector

      public Vector1D applyVector(Vector1D vec)
      Apply this transform to the given vector, ignoring translations.

      This method can be used to transform vector instances representing displacements between points. For example, if v represents the difference between points p1 and p2, then transform.applyVector(v) will represent the difference between p1 and p2 after transform is applied.

      Parameters:
      vec - the vector to transform
      Returns:
      the new, transformed vector
      See Also:
    • applyVectorX

      public double applyVectorX(double x)
      Apply this transform to the given vector coordinate, ignoring translations, and return the transformed x value. The return value is equal to x * m00.
      Parameters:
      x - x coordinate value
      Returns:
      transformed x coordinate value
      See Also:
    • applyDirection

      public Vector1D.Unit applyDirection(Vector1D vec)
      Apply this transform to the given vector, ignoring translations and normalizing the result. This is equivalent to transform.applyVector(vec).normalize() but without the intermediate vector instance.
      Specified by:
      applyDirection in class AbstractAffineTransformMatrix<Vector1D,AffineTransformMatrix1D>
      Parameters:
      vec - the vector to transform
      Returns:
      the new, transformed unit vector
      See Also:
    • determinant

      public double determinant()
      Get the determinant of the matrix.
      Specified by:
      determinant in class AbstractAffineTransformMatrix<Vector1D,AffineTransformMatrix1D>
      Returns:
      the determinant of the matrix
    • linear

      public AffineTransformMatrix1D linear()
      Return a matrix containing only the linear portion of this transform. The returned instance contains the same matrix elements as this instance but with the translation component set to zero.

      Example

            [ a, b ]   [ a, 0 ]
            [ 0, 1 ] → [ 0, 1 ]
       
      Specified by:
      linear in class AbstractAffineTransformMatrix<Vector1D,AffineTransformMatrix1D>
      Returns:
      a matrix containing only the linear portion of this transform
    • linearTranspose

      public AffineTransformMatrix1D linearTranspose()
      Return a matrix containing the transpose of the linear portion of this transform. The returned instance is linear, meaning it has a translation component of zero.

      In the one dimensional case, this is exactly the same as linear().

      Example

            [ a, b ]   [ a, 0 ]
            [ 0, 1 ] → [ 0, 1 ]
       
      Specified by:
      linearTranspose in class AbstractAffineTransformMatrix<Vector1D,AffineTransformMatrix1D>
      Returns:
      a matrix containing the transpose of the linear portion of this transform
    • translate

      public AffineTransformMatrix1D translate(Vector1D translation)
      Get a new transform containing the result of applying a translation logically after the transformation represented by the current instance. This is achieved by creating a new translation transform and pre-multiplying it with the current instance. In other words, the returned transform contains the matrix B * A, where A is the current matrix and B is the matrix representing the given translation.
      Parameters:
      translation - vector containing the translation values for each axis
      Returns:
      a new transform containing the result of applying a translation to the current instance
    • translate

      public AffineTransformMatrix1D translate(double x)
      Get a new transform containing the result of applying a translation logically after the transformation represented by the current instance. This is achieved by creating a new translation transform and pre-multiplying it with the current instance. In other words, the returned transform contains the matrix B * A, where A is the current matrix and B is the matrix representing the given translation.
      Parameters:
      x - translation in the x direction
      Returns:
      a new transform containing the result of applying a translation to the current instance
    • scale

      public AffineTransformMatrix1D scale(Vector1D scaleFactor)
      Get a new transform containing the result of applying a scale operation logically after the transformation represented by the current instance. This is achieved by creating a new scale transform and pre-multiplying it with the current instance. In other words, the returned transform contains the matrix B * A, where A is the current matrix and B is the matrix representing the given scale operation.
      Parameters:
      scaleFactor - vector containing scale factors for each axis
      Returns:
      a new transform containing the result of applying a scale operation to the current instance
    • scale

      public AffineTransformMatrix1D scale(double x)
      Get a new transform containing the result of applying a scale operation logically after the transformation represented by the current instance. This is achieved by creating a new scale transform and pre-multiplying it with the current instance. In other words, the returned transform contains the matrix B * A, where A is the current matrix and B is the matrix representing the given scale operation.
      Parameters:
      x - scale factor
      Returns:
      a new transform containing the result of applying a scale operation to the current instance
    • multiply

      Get a new transform created by multiplying this instance by the argument. This is equivalent to the expression A * M where A is the current transform matrix and M is the given transform matrix. In terms of transformations, applying the returned matrix is equivalent to applying M and then applying A. In other words, the rightmost transform is applied first.
      Parameters:
      m - the transform to multiply with
      Returns:
      the result of multiplying the current instance by the given transform matrix
    • premultiply

      Get a new transform created by multiplying the argument by this instance. This is equivalent to the expression M * A where A is the current transform matrix and M is the given transform matrix. In terms of transformations, applying the returned matrix is equivalent to applying A and then applying M. In other words, the rightmost transform is applied first.
      Parameters:
      m - the transform to multiply with
      Returns:
      the result of multiplying the given transform matrix by the current instance
    • inverse

      public AffineTransformMatrix1D inverse()
      Get an instance representing the inverse transform.
      Specified by:
      inverse in interface Transform<Vector1D>
      Specified by:
      inverse in class AbstractAffineTransformMatrix<Vector1D,AffineTransformMatrix1D>
      Returns:
      an instance representing the inverse transform
      Throws:
      IllegalStateException - if the matrix cannot be inverted
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Return true if the given object is an instance of AffineTransformMatrix1D and all matrix element values are exactly equal.
      Overrides:
      equals in class Object
      Parameters:
      obj - object to test for equality with the current instance
      Returns:
      true if all transform matrix elements are exactly equal; otherwise false
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • of

      public static AffineTransformMatrix1D of(double... arr)
      Get a new transform with the given matrix elements. The array must contain 2 elements. The first element in the array represents the scale factor for the transform and the second represents the translation.
      Parameters:
      arr - 2-element array containing values for the variable entries in the transform matrix
      Returns:
      a new transform initialized with the given matrix values
      Throws:
      IllegalArgumentException - if the array does not have 2 elements
    • from

      Construct a new transform representing the given function. The function is sampled at the points zero and one and a matrix is created to perform the transformation.
      Parameters:
      fn - function to create a transform matrix from
      Returns:
      a transform matrix representing the given function
      Throws:
      IllegalArgumentException - if the given function does not represent a valid affine transform
    • identity

      public static AffineTransformMatrix1D identity()
      Get the transform representing the identity matrix. This transform does not modify point or vector values when applied.
      Returns:
      transform representing the identity matrix
    • createTranslation

      public static AffineTransformMatrix1D createTranslation(Vector1D translation)
      Get a transform representing the given translation.
      Parameters:
      translation - vector containing translation values for each axis
      Returns:
      a new transform representing the given translation
    • createTranslation

      public static AffineTransformMatrix1D createTranslation(double x)
      Get a transform representing the given translation.
      Parameters:
      x - translation in the x direction
      Returns:
      a new transform representing the given translation
    • createScale

      public static AffineTransformMatrix1D createScale(Vector1D factor)
      Get a transform representing a scale operation.
      Parameters:
      factor - vector containing the scale factor
      Returns:
      a new transform representing a scale operation
    • createScale

      public static AffineTransformMatrix1D createScale(double factor)
      Get a transform representing a scale operation.
      Parameters:
      factor - scale factor
      Returns:
      a new transform representing a scale operation
    • multiply

      Multiply two transform matrices together.
      Parameters:
      a - first transform
      b - second transform
      Returns:
      the transform computed as a x b