Package org.spongepowered.api.util
Class DiscreteTransform2
java.lang.Object
org.spongepowered.api.util.DiscreteTransform2
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 Summary
Modifier and TypeFieldDescriptionstatic final DiscreteTransform2
Represents an identity transformation. -
Method Summary
Modifier and TypeMethodDescriptionandThen
(DiscreteTransform2 that) Returns a transform that is the composition of the given transform with this transform.compose
(DiscreteTransform2 that) Returns a transform that is the composition of this transform and the given transform.static DiscreteTransform2
fromRotation
(int quarterTurns) Returns a new transform representing a rotation in the xy plane around the origin.static DiscreteTransform2
fromRotation
(int quarterTurns, Vector2i point, boolean tileCorner) Returns a new transform representing a rotation in the xy plane, around a given point.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.static DiscreteTransform2
fromScale
(int a) Returns a new transform representing a scaling.static DiscreteTransform2
fromScale
(int x, int y) Returns a new transform representing a scaling on each axis.static DiscreteTransform2
Returns a new transform representing a scaling on each axis.static DiscreteTransform2
fromTranslation
(int x, int y) Returns a new transform representing a translation.static DiscreteTransform2
fromTranslation
(Vector2i vector) Returns a new transform representing a translation.invert()
Inverts the transform and returns it as a new transform.matrix()
Returns the matrix representation of the transform.static Optional
<DiscreteTransform2> Returns a new transform from the given transformation matrix, if the resulting transform would be discrete.static DiscreteTransform2
rotationAroundCenter
(int quarterTurns, Vector2i size) Returns a new transform representing a centered rotation of an area of tiles.transform
(int x, int y) Transform a vector represented as a pair of coordinates using this transform.Transforms a vector using this transforms.int
transformX
(int x, int y) Transforms the x coordinate of a vector using this transform.int
transformX
(Vector2i vector) Transforms the x coordinate of a vector using this transform.int
transformY
(int x, int y) Transforms the y coordinate of a vector using this transform.int
transformY
(Vector2i vector) Transforms the y coordinate of a vector using this transform.withRotation
(int quarterTurns) Adds a rotation to this transform, in the xy plane, around the origin and returns it as a new transform.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.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.withScale
(int a) Adds a scale factor to this transform and returns it as a new transform.withScale
(int x, int y) Adds a scale factor for each axis to this transform and returns it as a new transform.Adds a scale factor for each axis to this transform and returns it as a new transform.withTransformation
(DiscreteTransform2 transform) Adds another transformation to this transformation and returns int as a new transform.withTranslation
(int x, int y) Adds a translation to this transform and returns it as a new transform.withTranslation
(Vector2i vector) Adds a translation to this transform and returns it as a new transform.
-
Field Details
-
IDENTITY
Represents an identity transformation. Does nothing!
-
-
Method Details
-
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
Transforms a vector using this transforms.- Parameters:
vector
- The original vector- Returns:
- The transformed vector
-
transform
Transform a vector represented as a pair of coordinates using this transform.- Parameters:
x
- The x coordinate of the original vectory
- The y coordinate of the original vector- Returns:
- The transformed vector
-
transformX
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 vectory
- The y coordinate of the original vector- Returns:
- The transformed x coordinate
-
transformY
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 vectory
- The y coordinate of the original vector- Returns:
- The transformed y coordinate
-
invert
Inverts the transform and returns it as a new transform.- Returns:
- The inverse of this transform
-
compose
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
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
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
Adds a translation to this transform and returns it as a new transform.- Parameters:
x
- The x coordinate of the translationy
- The y coordinate of the translation- Returns:
- The translated transform as a copy
-
withScale
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
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
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 xy
- The scale factor on y- Returns:
- The scaled transform as a copy
-
withRotation
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 isquarterTurns * 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
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 isquarterTurns * 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 rotationpoint
- The point of rotation, as tile coordinatestileCorner
- 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 ishalfTurns * 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 rotationpoint
- The point of rotation, as tile coordinatestileCornerX
- Whether or not to use the corner of the tile instead of the center on the x axistileCornerY
- 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
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
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
Returns a new transform representing a translation.- Parameters:
vector
- The translation vector- Returns:
- The new translation transform
-
fromTranslation
Returns a new transform representing a translation.- Parameters:
x
- The x coordinate of the translationy
- The y coordinate of the translation- Returns:
- The new translation transform
-
fromScale
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
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
Returns a new transform representing a scaling on each axis. The scale factors must be non-zero.- Parameters:
x
- The scale factor on xy
- The scale factor on y- Returns:
- The new scale transform
-
fromRotation
Returns a new transform representing a rotation in the xy plane around the origin. The rotation is given is quarter turns. The actual rotation isquarterTurns * 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
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 isquarterTurns * 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 rotationpoint
- The point of rotation, as tile coordinatestileCorner
- 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 ishalfTurns * 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 rotationpoint
- The point of rotation, as tile coordinatestileCornerX
- Whether or not to use the corner of the tile instead of the center on the x axistileCornerY
- Whether or not to use the corner of the tile instead of the center on the y axis- Returns:
- The new rotation transform
-
rotationAroundCenter
Returns a new transform representing a centered rotation of an area of tiles. The rotation is given is quarter turns. The actual rotation isquarterTurns * 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 rotationsize
- The size of the area to rotate- Returns:
- The new rotation transform
-