Package arc.math

Class Affine2

java.lang.Object
arc.math.Affine2

public final class Affine2 extends Object
A specialized 3x3 matrix that can represent sequences of 2D translations, scales, flips, rotations, and shears. Affine transformations preserve straight lines, and parallel lines remain parallel after the transformation. Operations on affine matrices are faster because the last row can always be assumed (0, 0, 1).
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    float
     
    float
     
    float
     
    float
     
    float
     
    float
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs an identity matrix.
    Constructs a matrix from the given affine matrix.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    applyTo(Vec2 point)
    Applies the affine transformation on a vector.
    float
    det()
    Calculates the determinant of the matrix.
    getTranslation(Vec2 position)
    Get the x-y translation component of the matrix.
    idt()
    Sets this matrix to the identity matrix
    inv()
    Inverts this matrix given that the determinant is != 0.
    boolean
    Check if this is an indentity matrix.
    boolean
    Check if the this is a plain translation matrix.
    mul(Affine2 other)
    Postmultiplies this matrix with the provided matrix and stores the result in this matrix.
    preMul(Affine2 other)
    Premultiplies this matrix with the provided matrix and stores the result in this matrix.
    preRotate(float degrees)
    Premultiplies this matrix with a (counter-clockwise) rotation matrix.
    preRotateRad(float radians)
    Premultiplies this matrix with a (counter-clockwise) rotation matrix.
    preScale(float scaleX, float scaleY)
    Premultiplies this matrix with a scale matrix.
    preScale(Vec2 scale)
    Premultiplies this matrix with a scale matrix.
    preShear(float shearX, float shearY)
    Premultiplies this matrix by a shear matrix.
    preShear(Vec2 shear)
    Premultiplies this matrix by a shear matrix.
    preTranslate(float x, float y)
    Premultiplies this matrix by a translation matrix.
    Premultiplies this matrix by a translation matrix.
    rotate(float degrees)
    Postmultiplies this matrix with a (counter-clockwise) rotation matrix.
    rotateRad(float radians)
    Postmultiplies this matrix with a (counter-clockwise) rotation matrix.
    scale(float scaleX, float scaleY)
    Postmultiplies this matrix with a scale matrix.
    scale(Vec2 scale)
    Postmultiplies this matrix with a scale matrix.
    set(Affine2 other)
    Copies the values from the provided affine matrix to this matrix.
    set(Mat matrix)
    Copies the values from the provided matrix to this matrix.
    Sets this matrix to the product of two matrices.
    setToRotation(float degrees)
    Sets this matrix to a rotation matrix that will rotate any vector in counter-clockwise direction around the z-axis.
    setToRotation(float cos, float sin)
    Sets this matrix to a rotation matrix that will rotate any vector in counter-clockwise direction around the z-axis.
    setToRotationRad(float radians)
    Sets this matrix to a rotation matrix that will rotate any vector in counter-clockwise direction around the z-axis.
    setToScaling(float scaleX, float scaleY)
    Sets this matrix to a scaling matrix.
    Sets this matrix to a scaling matrix.
    setToShearing(float shearX, float shearY)
    Sets this matrix to a shearing matrix.
    Sets this matrix to a shearing matrix.
    setToTranslation(float x, float y)
    Sets this matrix to a translation matrix.
    Sets this matrix to a translation matrix.
    setToTrnRotRadScl(float x, float y, float radians, float scaleX, float scaleY)
    Sets this matrix to a concatenation of translation, rotation and scale.
    setToTrnRotRadScl(Vec2 trn, float radians, Vec2 scale)
    Sets this matrix to a concatenation of translation, rotation and scale.
    setToTrnRotScl(float x, float y, float degrees, float scaleX, float scaleY)
    Sets this matrix to a concatenation of translation, rotation and scale.
    setToTrnRotScl(Vec2 trn, float degrees, Vec2 scale)
    Sets this matrix to a concatenation of translation, rotation and scale.
    setToTrnScl(float x, float y, float scaleX, float scaleY)
    Sets this matrix to a concatenation of translation and scale.
    setToTrnScl(Vec2 trn, Vec2 scale)
    Sets this matrix to a concatenation of translation and scale.
    shear(float shearX, float shearY)
    Postmultiplies this matrix by a shear matrix.
    shear(Vec2 shear)
    Postmultiplies this matrix by a shear matrix.
     
    translate(float x, float y)
    Postmultiplies this matrix by a translation matrix.
    Postmultiplies this matrix by a translation matrix.

    Methods inherited from class java.lang.Object

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

    • m00

      public float m00
    • m01

      public float m01
    • m02

      public float m02
    • m10

      public float m10
    • m11

      public float m11
    • m12

      public float m12
  • Constructor Details

    • Affine2

      public Affine2()
      Constructs an identity matrix.
    • Affine2

      public Affine2(Affine2 other)
      Constructs a matrix from the given affine matrix.
      Parameters:
      other - The affine matrix to copy. This matrix will not be modified.
  • Method Details

    • idt

      public Affine2 idt()
      Sets this matrix to the identity matrix
      Returns:
      This matrix for the purpose of chaining operations.
    • set

      public Affine2 set(Affine2 other)
      Copies the values from the provided affine matrix to this matrix.
      Parameters:
      other - The affine matrix to copy.
      Returns:
      This matrix for the purposes of chaining.
    • set

      public Affine2 set(Mat matrix)
      Copies the values from the provided matrix to this matrix.
      Parameters:
      matrix - The matrix to copy, assumed to be an affine transformation.
      Returns:
      This matrix for the purposes of chaining.
    • setToTranslation

      public Affine2 setToTranslation(float x, float y)
      Sets this matrix to a translation matrix.
      Parameters:
      x - The translation in x
      y - The translation in y
      Returns:
      This matrix for the purpose of chaining operations.
    • setToTranslation

      public Affine2 setToTranslation(Vec2 trn)
      Sets this matrix to a translation matrix.
      Parameters:
      trn - The translation vector.
      Returns:
      This matrix for the purpose of chaining operations.
    • setToScaling

      public Affine2 setToScaling(float scaleX, float scaleY)
      Sets this matrix to a scaling matrix.
      Parameters:
      scaleX - The scale in x.
      scaleY - The scale in y.
      Returns:
      This matrix for the purpose of chaining operations.
    • setToScaling

      public Affine2 setToScaling(Vec2 scale)
      Sets this matrix to a scaling matrix.
      Parameters:
      scale - The scale vector.
      Returns:
      This matrix for the purpose of chaining operations.
    • setToRotation

      public Affine2 setToRotation(float degrees)
      Sets this matrix to a rotation matrix that will rotate any vector in counter-clockwise direction around the z-axis.
      Parameters:
      degrees - The angle in degrees.
      Returns:
      This matrix for the purpose of chaining operations.
    • setToRotationRad

      public Affine2 setToRotationRad(float radians)
      Sets this matrix to a rotation matrix that will rotate any vector in counter-clockwise direction around the z-axis.
      Parameters:
      radians - The angle in radians.
      Returns:
      This matrix for the purpose of chaining operations.
    • setToRotation

      public Affine2 setToRotation(float cos, float sin)
      Sets this matrix to a rotation matrix that will rotate any vector in counter-clockwise direction around the z-axis.
      Parameters:
      cos - The angle cosine.
      sin - The angle sine.
      Returns:
      This matrix for the purpose of chaining operations.
    • setToShearing

      public Affine2 setToShearing(float shearX, float shearY)
      Sets this matrix to a shearing matrix.
      Parameters:
      shearX - The shear in x direction.
      shearY - The shear in y direction.
      Returns:
      This matrix for the purpose of chaining operations.
    • setToShearing

      public Affine2 setToShearing(Vec2 shear)
      Sets this matrix to a shearing matrix.
      Parameters:
      shear - The shear vector.
      Returns:
      This matrix for the purpose of chaining operations.
    • setToTrnRotScl

      public Affine2 setToTrnRotScl(float x, float y, float degrees, float scaleX, float scaleY)
      Sets this matrix to a concatenation of translation, rotation and scale. It is a more efficient form for: idt().translate(x, y).rotate(degrees).scale(scaleX, scaleY)
      Parameters:
      x - The translation in x.
      y - The translation in y.
      degrees - The angle in degrees.
      scaleX - The scale in y.
      scaleY - The scale in x.
      Returns:
      This matrix for the purpose of chaining operations.
    • setToTrnRotScl

      public Affine2 setToTrnRotScl(Vec2 trn, float degrees, Vec2 scale)
      Sets this matrix to a concatenation of translation, rotation and scale. It is a more efficient form for: idt().translate(trn).rotate(degrees).scale(scale)
      Parameters:
      trn - The translation vector.
      degrees - The angle in degrees.
      scale - The scale vector.
      Returns:
      This matrix for the purpose of chaining operations.
    • setToTrnRotRadScl

      public Affine2 setToTrnRotRadScl(float x, float y, float radians, float scaleX, float scaleY)
      Sets this matrix to a concatenation of translation, rotation and scale. It is a more efficient form for: idt().translate(x, y).rotateRad(radians).scale(scaleX, scaleY)
      Parameters:
      x - The translation in x.
      y - The translation in y.
      radians - The angle in radians.
      scaleX - The scale in y.
      scaleY - The scale in x.
      Returns:
      This matrix for the purpose of chaining operations.
    • setToTrnRotRadScl

      public Affine2 setToTrnRotRadScl(Vec2 trn, float radians, Vec2 scale)
      Sets this matrix to a concatenation of translation, rotation and scale. It is a more efficient form for: idt().translate(trn).rotateRad(radians).scale(scale)
      Parameters:
      trn - The translation vector.
      radians - The angle in radians.
      scale - The scale vector.
      Returns:
      This matrix for the purpose of chaining operations.
    • setToTrnScl

      public Affine2 setToTrnScl(float x, float y, float scaleX, float scaleY)
      Sets this matrix to a concatenation of translation and scale. It is a more efficient form for: idt().translate(x, y).scale(scaleX, scaleY)
      Parameters:
      x - The translation in x.
      y - The translation in y.
      scaleX - The scale in y.
      scaleY - The scale in x.
      Returns:
      This matrix for the purpose of chaining operations.
    • setToTrnScl

      public Affine2 setToTrnScl(Vec2 trn, Vec2 scale)
      Sets this matrix to a concatenation of translation and scale. It is a more efficient form for: idt().translate(trn).scale(scale)
      Parameters:
      trn - The translation vector.
      scale - The scale vector.
      Returns:
      This matrix for the purpose of chaining operations.
    • setToProduct

      public Affine2 setToProduct(Affine2 l, Affine2 r)
      Sets this matrix to the product of two matrices.
      Parameters:
      l - Left matrix.
      r - Right matrix.
      Returns:
      This matrix for the purpose of chaining operations.
    • inv

      public Affine2 inv()
      Inverts this matrix given that the determinant is != 0.
      Returns:
      This matrix for the purpose of chaining operations.
      Throws:
      ArcRuntimeException - if the matrix is singular (not invertible)
    • mul

      public Affine2 mul(Affine2 other)
      Postmultiplies this matrix with the provided matrix and stores the result in this matrix. For example:
       A.mul(B) results in A := AB
       
      Parameters:
      other - Matrix to multiply by.
      Returns:
      This matrix for the purpose of chaining operations together.
    • preMul

      public Affine2 preMul(Affine2 other)
      Premultiplies this matrix with the provided matrix and stores the result in this matrix. For example:
       A.preMul(B) results in A := BA
       
      Parameters:
      other - The other Matrix to multiply by
      Returns:
      This matrix for the purpose of chaining operations.
    • translate

      public Affine2 translate(float x, float y)
      Postmultiplies this matrix by a translation matrix.
      Parameters:
      x - The x-component of the translation vector.
      y - The y-component of the translation vector.
      Returns:
      This matrix for the purpose of chaining.
    • translate

      public Affine2 translate(Vec2 trn)
      Postmultiplies this matrix by a translation matrix.
      Parameters:
      trn - The translation vector.
      Returns:
      This matrix for the purpose of chaining.
    • preTranslate

      public Affine2 preTranslate(float x, float y)
      Premultiplies this matrix by a translation matrix.
      Parameters:
      x - The x-component of the translation vector.
      y - The y-component of the translation vector.
      Returns:
      This matrix for the purpose of chaining.
    • preTranslate

      public Affine2 preTranslate(Vec2 trn)
      Premultiplies this matrix by a translation matrix.
      Parameters:
      trn - The translation vector.
      Returns:
      This matrix for the purpose of chaining.
    • scale

      public Affine2 scale(float scaleX, float scaleY)
      Postmultiplies this matrix with a scale matrix.
      Parameters:
      scaleX - The scale in the x-axis.
      scaleY - The scale in the y-axis.
      Returns:
      This matrix for the purpose of chaining.
    • scale

      public Affine2 scale(Vec2 scale)
      Postmultiplies this matrix with a scale matrix.
      Parameters:
      scale - The scale vector.
      Returns:
      This matrix for the purpose of chaining.
    • preScale

      public Affine2 preScale(float scaleX, float scaleY)
      Premultiplies this matrix with a scale matrix.
      Parameters:
      scaleX - The scale in the x-axis.
      scaleY - The scale in the y-axis.
      Returns:
      This matrix for the purpose of chaining.
    • preScale

      public Affine2 preScale(Vec2 scale)
      Premultiplies this matrix with a scale matrix.
      Parameters:
      scale - The scale vector.
      Returns:
      This matrix for the purpose of chaining.
    • rotate

      public Affine2 rotate(float degrees)
      Postmultiplies this matrix with a (counter-clockwise) rotation matrix.
      Parameters:
      degrees - The angle in degrees
      Returns:
      This matrix for the purpose of chaining.
    • rotateRad

      public Affine2 rotateRad(float radians)
      Postmultiplies this matrix with a (counter-clockwise) rotation matrix.
      Parameters:
      radians - The angle in radians
      Returns:
      This matrix for the purpose of chaining.
    • preRotate

      public Affine2 preRotate(float degrees)
      Premultiplies this matrix with a (counter-clockwise) rotation matrix.
      Parameters:
      degrees - The angle in degrees
      Returns:
      This matrix for the purpose of chaining.
    • preRotateRad

      public Affine2 preRotateRad(float radians)
      Premultiplies this matrix with a (counter-clockwise) rotation matrix.
      Parameters:
      radians - The angle in radians
      Returns:
      This matrix for the purpose of chaining.
    • shear

      public Affine2 shear(float shearX, float shearY)
      Postmultiplies this matrix by a shear matrix.
      Parameters:
      shearX - The shear in x direction.
      shearY - The shear in y direction.
      Returns:
      This matrix for the purpose of chaining.
    • shear

      public Affine2 shear(Vec2 shear)
      Postmultiplies this matrix by a shear matrix.
      Parameters:
      shear - The shear vector.
      Returns:
      This matrix for the purpose of chaining.
    • preShear

      public Affine2 preShear(float shearX, float shearY)
      Premultiplies this matrix by a shear matrix.
      Parameters:
      shearX - The shear in x direction.
      shearY - The shear in y direction.
      Returns:
      This matrix for the purpose of chaining.
    • preShear

      public Affine2 preShear(Vec2 shear)
      Premultiplies this matrix by a shear matrix.
      Parameters:
      shear - The shear vector.
      Returns:
      This matrix for the purpose of chaining.
    • det

      public float det()
      Calculates the determinant of the matrix.
      Returns:
      The determinant of this matrix.
    • getTranslation

      public Vec2 getTranslation(Vec2 position)
      Get the x-y translation component of the matrix.
      Parameters:
      position - Output vector.
      Returns:
      Filled position.
    • isTranslation

      public boolean isTranslation()
      Check if the this is a plain translation matrix.
      Returns:
      True if scale is 1 and rotation is 0.
    • isIdt

      public boolean isIdt()
      Check if this is an indentity matrix.
      Returns:
      True if scale is 1 and rotation is 0.
    • applyTo

      public void applyTo(Vec2 point)
      Applies the affine transformation on a vector.
    • toString

      public String toString()
      Overrides:
      toString in class Object