Class Affine2

  • All Implemented Interfaces:
    java.io.Serializable

    public final class Affine2
    extends java.lang.Object
    implements java.io.Serializable
    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).
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      float m00  
      float m01  
      float m02  
      float m10  
      float m11  
      float m12  
    • Constructor Summary

      Constructors 
      Constructor Description
      Affine2()
      Constructs an identity matrix.
      Affine2​(Affine2 other)
      Constructs a matrix from the given affine matrix.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void applyTo​(Vector2 point)
      Applies the affine transformation on a vector.
      float det()
      Calculates the determinant of the matrix.
      Vector2 getTranslation​(Vector2 position)
      Get the x-y translation component of the matrix.
      Affine2 idt()
      Sets this matrix to the identity matrix
      Affine2 inv()
      Inverts this matrix given that the determinant is != 0.
      boolean isIdt()
      Check if this is an indentity matrix.
      boolean isTranslation()
      Check if the this is a plain translation matrix.
      Affine2 mul​(Affine2 other)
      Postmultiplies this matrix with the provided matrix and stores the result in this matrix.
      Affine2 preMul​(Affine2 other)
      Premultiplies this matrix with the provided matrix and stores the result in this matrix.
      Affine2 preRotate​(float degrees)
      Premultiplies this matrix with a (counter-clockwise) rotation matrix.
      Affine2 preRotateRad​(float radians)
      Premultiplies this matrix with a (counter-clockwise) rotation matrix.
      Affine2 preScale​(float scaleX, float scaleY)
      Premultiplies this matrix with a scale matrix.
      Affine2 preScale​(Vector2 scale)
      Premultiplies this matrix with a scale matrix.
      Affine2 preShear​(float shearX, float shearY)
      Premultiplies this matrix by a shear matrix.
      Affine2 preShear​(Vector2 shear)
      Premultiplies this matrix by a shear matrix.
      Affine2 preTranslate​(float x, float y)
      Premultiplies this matrix by a translation matrix.
      Affine2 preTranslate​(Vector2 trn)
      Premultiplies this matrix by a translation matrix.
      Affine2 rotate​(float degrees)
      Postmultiplies this matrix with a (counter-clockwise) rotation matrix.
      Affine2 rotateRad​(float radians)
      Postmultiplies this matrix with a (counter-clockwise) rotation matrix.
      Affine2 scale​(float scaleX, float scaleY)
      Postmultiplies this matrix with a scale matrix.
      Affine2 scale​(Vector2 scale)
      Postmultiplies this matrix with a scale matrix.
      Affine2 set​(Affine2 other)
      Copies the values from the provided affine matrix to this matrix.
      Affine2 set​(Matrix3 matrix)
      Copies the values from the provided matrix to this matrix.
      Affine2 set​(Matrix4 matrix)
      Copies the 2D transformation components from the provided 4x4 matrix.
      Affine2 setToProduct​(Affine2 l, Affine2 r)
      Sets this matrix to the product of two matrices.
      Affine2 setToRotation​(float degrees)
      Sets this matrix to a rotation matrix that will rotate any vector in counter-clockwise direction around the z-axis.
      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.
      Affine2 setToRotationRad​(float radians)
      Sets this matrix to a rotation matrix that will rotate any vector in counter-clockwise direction around the z-axis.
      Affine2 setToScaling​(float scaleX, float scaleY)
      Sets this matrix to a scaling matrix.
      Affine2 setToScaling​(Vector2 scale)
      Sets this matrix to a scaling matrix.
      Affine2 setToShearing​(float shearX, float shearY)
      Sets this matrix to a shearing matrix.
      Affine2 setToShearing​(Vector2 shear)
      Sets this matrix to a shearing matrix.
      Affine2 setToTranslation​(float x, float y)
      Sets this matrix to a translation matrix.
      Affine2 setToTranslation​(Vector2 trn)
      Sets this matrix to a translation matrix.
      Affine2 setToTrnRotRadScl​(float x, float y, float radians, float scaleX, float scaleY)
      Sets this matrix to a concatenation of translation, rotation and scale.
      Affine2 setToTrnRotRadScl​(Vector2 trn, float radians, Vector2 scale)
      Sets this matrix to a concatenation of translation, rotation and scale.
      Affine2 setToTrnRotScl​(float x, float y, float degrees, float scaleX, float scaleY)
      Sets this matrix to a concatenation of translation, rotation and scale.
      Affine2 setToTrnRotScl​(Vector2 trn, float degrees, Vector2 scale)
      Sets this matrix to a concatenation of translation, rotation and scale.
      Affine2 setToTrnScl​(float x, float y, float scaleX, float scaleY)
      Sets this matrix to a concatenation of translation and scale.
      Affine2 setToTrnScl​(Vector2 trn, Vector2 scale)
      Sets this matrix to a concatenation of translation and scale.
      Affine2 shear​(float shearX, float shearY)
      Postmultiplies this matrix by a shear matrix.
      Affine2 shear​(Vector2 shear)
      Postmultiplies this matrix by a shear matrix.
      java.lang.String toString()  
      Affine2 translate​(float x, float y)
      Postmultiplies this matrix by a translation matrix.
      Affine2 translate​(Vector2 trn)
      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 Detail

      • 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 Detail

      • 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 Detail

      • 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​(Matrix3 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.
      • set

        public Affine2 set​(Matrix4 matrix)
        Copies the 2D transformation components from the provided 4x4 matrix. The values are mapped as follows:
              [  M00  M01  M03  ]
              [  M10  M11  M13  ]
              [   0    0    1   ]
         
        Parameters:
        matrix - The source matrix, assumed to be an affine transformation within XY plane. This matrix will not be modified.
        Returns:
        This matrix for the purpose of chaining operations.
      • 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​(Vector2 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​(Vector2 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​(Vector2 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​(Vector2 trn,
                                      float degrees,
                                      Vector2 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​(Vector2 trn,
                                         float radians,
                                         Vector2 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​(Vector2 trn,
                                   Vector2 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:
        GdxRuntimeException - 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​(Vector2 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​(Vector2 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​(Vector2 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​(Vector2 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​(Vector2 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​(Vector2 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 Vector2 getTranslation​(Vector2 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​(Vector2 point)
        Applies the affine transformation on a vector.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object