Class DiscreteTransform2


  • public class DiscreteTransform2
    extends Object
    Represents a transform. It is 2 dimensional and discrete. It will never cause aliasing.

    Rotations are performed around tile centers unless the tile corner flags are set to true. To prevent aliasing, quarter turn rotations are only legal on block centers or corners. Half turns can be performed additionally on line centers.

    • Field Detail

      • IDENTITY

        public static final DiscreteTransform2 IDENTITY
        Represents an identity transformation. Does nothing!
    • Method Detail

      • matrix

        public Matrix3d matrix()
        Returns the matrix representation of the transform. It is 3D to allow it to include a translation.
        Returns:
        The matrix for this transform
      • transform

        public Vector2i transform​(Vector2i vector)
        Transforms a vector using this transforms.
        Parameters:
        vector - The original vector
        Returns:
        The transformed vector
      • transform

        public Vector2i transform​(int x,
                                  int y)
        Transform a vector represented as a pair of coordinates using this transform.
        Parameters:
        x - The x coordinate of the original vector
        y - The y coordinate of the original vector
        Returns:
        The transformed vector
      • transformX

        public int transformX​(Vector2i vector)
        Transforms the x coordinate of a vector using this transform. Only creates a new object on the first call.
        Parameters:
        vector - The original vector
        Returns:
        The transformed x coordinate
      • transformX

        public int transformX​(int x,
                              int y)
        Transforms the x coordinate of a vector using this transform. Only creates a new object on the first call.
        Parameters:
        x - The x coordinate of the original vector
        y - The y coordinate of the original vector
        Returns:
        The transformed x coordinate
      • transformY

        public int transformY​(Vector2i vector)
        Transforms the y coordinate of a vector using this transform. Only creates a new object on the first call.
        Parameters:
        vector - The original vector
        Returns:
        The transformed y coordinate
      • transformY

        public int transformY​(int x,
                              int y)
        Transforms the y coordinate of a vector using this transform. Only creates a new object on the first call.
        Parameters:
        x - The x coordinate of the original vector
        y - The y coordinate of the original vector
        Returns:
        The transformed y coordinate
      • invert

        public DiscreteTransform2 invert()
        Inverts the transform and returns it as a new transform.
        Returns:
        The inverse of this transform
      • compose

        public DiscreteTransform2 compose​(DiscreteTransform2 that)
        Returns a transform that is the composition of this transform and the given transform. The result will apply this transformation after the given one.
        Parameters:
        that - The transform to compose with
        Returns:
        The new composed transform
      • andThen

        public DiscreteTransform2 andThen​(DiscreteTransform2 that)
        Returns a transform that is the composition of the given transform with this transform. The result will apply the given transformation after this one.
        Parameters:
        that - The transform to compose with
        Returns:
        The new composed transform
      • withTranslation

        public DiscreteTransform2 withTranslation​(Vector2i vector)
        Adds a translation to this transform and returns it as a new transform.
        Parameters:
        vector - The translation vector
        Returns:
        The translated transform as a copy
      • withTranslation

        public DiscreteTransform2 withTranslation​(int x,
                                                  int y)
        Adds a translation to this transform and returns it as a new transform.
        Parameters:
        x - The x coordinate of the translation
        y - The y coordinate of the translation
        Returns:
        The translated transform as a copy
      • withScale

        public DiscreteTransform2 withScale​(int a)
        Adds a scale factor to this transform and returns it as a new transform. This factor must be non-zero.
        Parameters:
        a - The scale factor
        Returns:
        The scaled transform as a copy
      • withScale

        public DiscreteTransform2 withScale​(Vector2i vector)
        Adds a scale factor for each axis to this transform and returns it as a new transform. The factors must be non-zero.
        Parameters:
        vector - The scale vector
        Returns:
        The scaled transform as a copy
      • withScale

        public DiscreteTransform2 withScale​(int x,
                                            int y)
        Adds a scale factor for each axis to this transform and returns it as a new transform. The factors must be non-zero.
        Parameters:
        x - The scale factor on x
        y - The scale factor on y
        Returns:
        The scaled transform as a copy
      • withRotation

        public DiscreteTransform2 withRotation​(int quarterTurns)
        Adds a rotation to this transform, in the xy plane, around the origin and returns it as a new transform. The rotation is given is quarter turns. The actual rotation is quarterTurns * 90. The rotation is around the block center, not the corner.
        Parameters:
        quarterTurns - The number of quarter turns in this rotation
        Returns:
        The rotated transform as a copy
      • withRotation

        public DiscreteTransform2 withRotation​(int quarterTurns,
                                               Vector2i point,
                                               boolean tileCorner)
        Adds a a rotation to this transform, in the xy plane, around a given point, and returns it as a new transform. The rotation is given is quarter turns. The actual rotation is quarterTurns * 90. The tile corner flag changes the point to be the tile upper corner instead of the center.
        Parameters:
        quarterTurns - The number of quarter turns in this rotation
        point - The point of rotation, as tile coordinates
        tileCorner - Whether or not to use the corner of the tile instead of the center
        Returns:
        The rotated transform as a copy
      • withRotation

        public DiscreteTransform2 withRotation​(int halfTurns,
                                               Vector2i point,
                                               boolean tileCornerX,
                                               boolean tileCornerY)
        Adds a a rotation to this transform, in the xy plane, around a given point, and returns it as a new transform. The rotation is given is half turns. The actual rotation is halfTurns * 180. The tile corner flags change the point to be the tile corner or edge instead of the center. When both flags are false, the center is used. When only one is true the edge on the opposite axis to the flag is used. When both are true the upper corner is used.
        Parameters:
        halfTurns - The number of half turns in this rotation
        point - The point of rotation, as tile coordinates
        tileCornerX - Whether or not to use the corner of the tile instead of the center on the x axis
        tileCornerY - Whether or not to use the corner of the tile instead of the center on the y axis
        Returns:
        The rotated transform as a copy
      • withTransformation

        public DiscreteTransform2 withTransformation​(DiscreteTransform2 transform)
        Adds another transformation to this transformation and returns int as a new transform.
        Parameters:
        transform - The transformation to add
        Returns:
        The added transforms as a copy
      • of

        public static Optional<DiscreteTransform2> of​(Matrix3d matrix)
        Returns a new transform from the given transformation matrix, if the resulting transform would be discrete.
        Parameters:
        matrix - The matrix to use for the transform
        Returns:
        The new transform, or Optional.empty()
      • fromTranslation

        public static DiscreteTransform2 fromTranslation​(Vector2i vector)
        Returns a new transform representing a translation.
        Parameters:
        vector - The translation vector
        Returns:
        The new translation transform
      • fromTranslation

        public static DiscreteTransform2 fromTranslation​(int x,
                                                         int y)
        Returns a new transform representing a translation.
        Parameters:
        x - The x coordinate of the translation
        y - The y coordinate of the translation
        Returns:
        The new translation transform
      • fromScale

        public static DiscreteTransform2 fromScale​(int a)
        Returns a new transform representing a scaling. The scale factor must be non-zero.
        Parameters:
        a - The scale factor
        Returns:
        The new scale transform
      • fromScale

        public static DiscreteTransform2 fromScale​(Vector2i vector)
        Returns a new transform representing a scaling on each axis. The scale factors must be non-zero.
        Parameters:
        vector - The scale vector
        Returns:
        The new scale transform
      • fromScale

        public static DiscreteTransform2 fromScale​(int x,
                                                   int y)
        Returns a new transform representing a scaling on each axis. The scale factors must be non-zero.
        Parameters:
        x - The scale factor on x
        y - The scale factor on y
        Returns:
        The new scale transform
      • fromRotation

        public static DiscreteTransform2 fromRotation​(int quarterTurns)
        Returns a new transform representing a rotation in the xy plane around the origin. The rotation is given is quarter turns. The actual rotation is quarterTurns * 90. The rotation is around the block center, not the corner.
        Parameters:
        quarterTurns - The number of quarter turns in this rotation
        Returns:
        The new rotation transform
      • fromRotation

        public static DiscreteTransform2 fromRotation​(int quarterTurns,
                                                      Vector2i point,
                                                      boolean tileCorner)
        Returns a new transform representing a rotation in the xy plane, around a given point. The rotation is given is quarter turns. The actual rotation is quarterTurns * 90. The tile corner flag change the point to be the tile corner instead of the center.
        Parameters:
        quarterTurns - The number of quarter turns in this rotation
        point - The point of rotation, as tile coordinates
        tileCorner - Whether or not to use the corner of the tile instead of the center
        Returns:
        The new rotation transform
      • fromRotation

        public static DiscreteTransform2 fromRotation​(int halfTurns,
                                                      Vector2i point,
                                                      boolean tileCornerX,
                                                      boolean tileCornerY)
        Returns a new transform representing a rotation in the xy plane, around a given point. The rotation is given is half turns. The actual rotation is halfTurns * 180. The tile corner flags change the point to be the tile corner or edge instead of the center. When both flags are false, the center is used. When only one is true the edge on the opposite axis to the flag is used. When both are true the upper corner is used.
        Parameters:
        halfTurns - The number of half turns in this rotation
        point - The point of rotation, as tile coordinates
        tileCornerX - Whether or not to use the corner of the tile instead of the center on the x axis
        tileCornerY - Whether or not to use the corner of the tile instead of the center on the y axis
        Returns:
        The new rotation transform
      • rotationAroundCenter

        public static DiscreteTransform2 rotationAroundCenter​(int quarterTurns,
                                                              Vector2i size)
        Returns a new transform representing a centered rotation of an area of tiles. The rotation is given is quarter turns. The actual rotation is quarterTurns * 90. Areas with differing parities on the axes can only be rotated by multiples of 180 degrees.
        Parameters:
        quarterTurns - The amount of quarter turns in this rotation
        size - The size of the area to rotate
        Returns:
        The new rotation transform