AffineTransform
class represents a 2D affine transform that performs a linear mapping from 2D coordinates to other 2D coordinates that preserves the "straightness" and "parallelness" of lines. Affine transformations can be constructed using sequences of translations, scales, flips, rotations, and shears. Such a coordinate transformation can be represented by a 3 row by 3 column matrix with an implied last row of [ 0 0 1 ]. This matrix transforms source coordinates (x, y)
into destination coordinates (x', y')
by considering them to be a column vector and multiplying the coordinate vector by the matrix according to the following process:
[ x'] [ m00 m01 m02 ] [ x ] [ m00x + m01y + m02 ] [ y'] = [ m10 m11 m12 ] [ y ] = [ m10x + m11y + m12 ] [ 1 ] [ 0 0 1 ] [ 1 ] [ 1 ]
Field Detail |
public static final int TYPE_IDENTITY
public static final int TYPE_TRANSLATION
public static final int TYPE_UNIFORM_SCALE
public static final int TYPE_GENERAL_SCALE
public static final int TYPE_MASK_SCALE
public static final int TYPE_FLIP
public static final int TYPE_QUADRANT_ROTATION
public static final int TYPE_GENERAL_ROTATION
public static final int TYPE_MASK_ROTATION
public static final int TYPE_GENERAL_TRANSFORM
static final int APPLY_IDENTITY
static final int APPLY_TRANSLATE
static final int APPLY_SCALE
static final int APPLY_SHEAR
double m00
double m10
double m01
double m11
double m02
double m12
transient int state
Constructor Detail |
public AffineTransform()
AffineTransform
representing the Identity transformation.public AffineTransform(AffineTransform Tx)
AffineTransform
that is a copy of the specified AffineTransform
object.Tx
- the AffineTransform
object to copypublic AffineTransform(float m00, float m10, float m01, float m11, float m02, float m12)
AffineTransform
from 6 floating point values representing the 6 specifiable entries of the 3x3 transformation matrix.m00, m01, m02, m10, m11, m12
- the 6 floating point values that compose the 3x3 transformation matrixpublic AffineTransform(float[] flatmatrix)
AffineTransform
from an array of floating point values representing either the 4 non-translation enries or the 6 specifiable entries of the 3x3 transformation matrix. The values are retrieved from the array as { m00 m10 m01 m11 [m02 m12]}.flatmatrix
- the float array containing the values to be set in the new AffineTransform
object. The length of the array is assumed to be at least 4. If the length of the array is less than 6, only the first 4 values are taken. If the length of the array is greater than 6, the first 6 values are taken.public AffineTransform(double m00, double m10, double m01, double m11, double m02, double m12)
AffineTransform
from 6 double precision values representing the 6 specifiable entries of the 3x3 transformation matrix.m00, m01, m02, m10, m11, m12
- the 6 floating point values that compose the 3x3 transformation matrixpublic AffineTransform(double[] flatmatrix)
AffineTransform
from an array of double precision values representing either the 4 non-translation entries or the 6 specifiable entries of the 3x3 transformation matrix. The values are retrieved from the array as { m00 m10 m01 m11 [m02 m12]}.flatmatrix
- the double array containing the values to be set in the new AffineTransform
object. The length of the array is assumed to be at least 4. If the length of the array is less than 6, only the first 4 values are taken. If the length of the array is greater than 6, the first 6 values are taken.Method Detail |
public static AffineTransform getTranslateInstance(double tx, double ty)
[ 1 0 tx ] [ 0 1 ty ] [ 0 0 1 ]
tx
- the distance by which coordinates are translated in the X axis directionty
- the distance by which coordinates are translated in the Y axis directionAffineTransform
object that represents a translation transformation, created with the specified vector.public static AffineTransform getRotateInstance(double theta)
[ cos(theta) -sin(theta) 0 ] [ sin(theta) cos(theta) 0 ] [ 0 0 1 ]Rotating with a positive angle theta rotates points on the positive x axis toward the positive y axis.
theta
- the angle of rotation in radiansAffineTransform
object that is a rotation transformation, created with the specified angle of rotation.public static AffineTransform getRotateInstance(double theta, double x, double y)
This operation is equivalent to the following sequence of calls:
AffineTransform Tx = new AffineTransform(); Tx.setToTranslation(x, y); // S3: final translation Tx.rotate(theta); // S2: rotate around anchor Tx.translate(-x, -y); // S1: translate anchor to originThe matrix representing the returned transform is:
[ cos(theta) -sin(theta) x-x*cos+y*sin ] [ sin(theta) cos(theta) y-x*sin-y*cos ] [ 0 0 1 ]Rotating with a positive angle theta rotates points on the positive x axis toward the positive y axis.
theta
- the angle of rotation in radiansx, y
- the coordinates of the anchor point of the rotationAffineTransform
object that rotates coordinates around the specified point by the specified angle of rotation.public static AffineTransform getScaleInstance(double sx, double sy)
[ sx 0 0 ] [ 0 sy 0 ] [ 0 0 1 ]
sx
- the factor by which coordinates are scaled along the X axis directionsy
- the factor by which coordinates are scaled along the Y axis directionAffineTransform
object that scales coordinates by the specified factors.public static AffineTransform getShearInstance(double shx, double shy)
[ 1 shx 0 ] [ shy 1 0 ] [ 0 0 1 ]
shx
- the multiplier by which coordinates are shifted in the direction of the positive X axis as a factor of their Y coordinateshy
- the multiplier by which coordinates are shifted in the direction of the positive Y axis as a factor of their X coordinateAffineTransform
object that shears coordinates by the specified multipliers.public int getType()
public double getDeterminant()
If the determinant is non-zero, then this transform is invertible and the various methods that depend on the inverse transform do not need to throw a {@link NoninvertibleTransformException}. If the determinant is zero then this transform can not be inverted since the transform maps all input coordinates onto a line or a point. If the determinant is near enough to zero then inverse transform operations might not carry enough precision to produce meaningful results.
If this transform represents a uniform scale, as indicated by the getType
method then the determinant also represents the square of the uniform scale factor by which all of the points are expanded from or contracted towards the origin. If this transform represents a non-uniform scale or more general transform then the determinant is not likely to represent a value useful for any purpose other than determining if inverse transforms are possible.
Mathematically, the determinant is calculated using the formula:
| m00 m01 m02 | | m10 m11 m12 | = m00 * m11 - m01 * m10 | 0 0 1 |
void updateState()
public void getMatrix(double[] flatmatrix)
flatmatrix
- the double array used to store the returned values.public double getScaleX()
public double getScaleY()
public double getShearX()
public double getShearY()
public double getTranslateX()
public double getTranslateY()
public void translate(double tx, double ty)
AffineTransform
represented by the following matrix: [ 1 0 tx ] [ 0 1 ty ] [ 0 0 1 ]
tx
- the distance by which coordinates are translated in the X axis directionty
- the distance by which coordinates are translated in the Y axis directionpublic void rotate(double theta)
AffineTransform
represented by the following matrix: [ cos(theta) -sin(theta) 0 ] [ sin(theta) cos(theta) 0 ] [ 0 0 1 ]Rotating with a positive angle theta rotates points on the positive x axis toward the positive y axis.
theta
- the angle of rotation in radianspublic void rotate(double theta, double x, double y)
This operation is equivalent to the following sequence of calls:
translate(x, y); // S3: final translation rotate(theta); // S2: rotate around anchor translate(-x, -y); // S1: translate anchor to originRotating with a positive angle theta rotates points on the positive x axis toward the positive y axis.
theta
- the angle of rotation in radiansx, y
- the coordinates of the anchor point of the rotationpublic void scale(double sx, double sy)
AffineTransform
represented by the following matrix: [ sx 0 0 ] [ 0 sy 0 ] [ 0 0 1 ]
sx
- the factor by which coordinates are scaled along the X axis directionsy
- the factor by which coordinates are scaled along the Y axis directionpublic void shear(double shx, double shy)
AffineTransform
represented by the following matrix: [ 1 shx 0 ] [ shy 1 0 ] [ 0 0 1 ]
shx
- the multiplier by which coordinates are shifted in the direction of the positive X axis as a factor of their Y coordinateshy
- the multiplier by which coordinates are shifted in the direction of the positive Y axis as a factor of their X coordinatepublic void setToIdentity()
public void setToTranslation(double tx, double ty)
[ 1 0 tx ] [ 0 1 ty ] [ 0 0 1 ]
tx
- the distance by which coordinates are translated in the X axis directionty
- the distance by which coordinates are translated in the Y axis directionpublic void setToRotation(double theta)
[ cos(theta) -sin(theta) 0 ] [ sin(theta) cos(theta) 0 ] [ 0 0 1 ]Rotating with a positive angle theta rotates points on the positive x axis toward the positive y axis.
theta
- the angle of rotation in radianspublic void setToRotation(double theta, double x, double y)
This operation is equivalent to the following sequence of calls:
setToTranslation(x, y); // S3: final translation rotate(theta); // S2: rotate around anchor translate(-x, -y); // S1: translate anchor to originThe matrix representing this transform becomes:
[ cos(theta) -sin(theta) x-x*cos+y*sin ] [ sin(theta) cos(theta) y-x*sin-y*cos ] [ 0 0 1 ]Rotating with a positive angle theta rotates points on the positive x axis toward the positive y axis.
theta
- the angle of rotation in radiansx, y
- the coordinates of the anchor point of the rotationpublic void setToScale(double sx, double sy)
[ sx 0 0 ] [ 0 sy 0 ] [ 0 0 1 ]
sx
- the factor by which coordinates are scaled along the X axis directionsy
- the factor by which coordinates are scaled along the Y axis directionpublic void setToShear(double shx, double shy)
[ 1 shx 0 ] [ shy 1 0 ] [ 0 0 1 ]
shx
- the multiplier by which coordinates are shifted in the direction of the positive X axis as a factor of their Y coordinateshy
- the multiplier by which coordinates are shifted in the direction of the positive Y axis as a factor of their X coordinatepublic void setTransform(AffineTransform Tx)
AffineTransform
object.Tx
- the AffineTransform
object from which to copy the transformpublic void setTransform(double m00, double m10, double m01, double m11, double m02, double m12)
m00, m01, m02, m10, m11, m12
- the 6 floating point values that compose the 3x3 transformation matrixpublic void concatenate(AffineTransform Tx)
AffineTransform
Tx
to this AffineTransform
Cx in the most commonly useful way to provide a new user space that is mapped to the former user space by Tx
. Cx is updated to perform the combined transformation. Transforming a point p by the updated transform Cx' is equivalent to first transforming p by Tx
and then transforming the result by the original transform Cx like this: Cx'(p) = Cx(Tx(p)) In matrix notation, if this transform Cx is represented by the matrix [this] and Tx
is represented by the matrix [Tx] then this method does the following: [this] = [this] x [Tx]
Tx
- the AffineTransform
object to be concatenated with this AffineTransform
object.public void preConcatenate(AffineTransform Tx)
AffineTransform
Tx
to this AffineTransform
Cx in a less commonly used way such that Tx
modifies the coordinate transformation relative to the absolute pixel space rather than relative to the existing user space. Cx is updated to perform the combined transformation. Transforming a point p by the updated transform Cx' is equivalent to first transforming p by the original transform Cx and then transforming the result by Tx
like this: Cx'(p) = Tx(Cx(p)) In matrix notation, if this transform Cx is represented by the matrix [this] and Tx
is represented by the matrix [Tx] then this method does the following: [this] = [Tx] x [this]
Tx
- the AffineTransform
object to be concatenated with this AffineTransform
object.public AffineTransform createInverse()
AffineTransform
object representing the inverse transformation. The inverse transform Tx' of this transform Tx maps coordinates transformed by Tx back to their original coordinates. In other words, Tx'(Tx(p)) = p = Tx(Tx'(p)). If this transform maps all coordinates onto a point or a line then it will not have an inverse, since coordinates that do not lie on the destination point or line will not have an inverse mapping. The getDeterminant
method can be used to determine if this transform has no inverse, in which case an exception will be thrown if the createInverse
method is called.
AffineTransform
object representing the inverse transformation.NoninvertibleTransformException
- if the matrix cannot be inverted.public Point2D transform(Point2D ptSrc, Point2D ptDst)
ptSrc
and stores the result in ptDst
. If ptDst
is null
, a new {@link Point2D} object is allocated and then the result of the transformation is stored in this object. In either case, ptDst
, which contains the transformed point, is returned for convenience. If ptSrc
and ptDst
are the same object, the input point is correctly overwritten with the transformed point.ptSrc
- the specified Point2D
to be transformedptDst
- the specified Point2D
that stores the result of transforming ptSrc
ptDst
after transforming ptSrc
and stroring the result in ptDst
.public void transform(Point2D[] ptSrc, int srcOff, Point2D[] ptDst, int dstOff, int numPts)
ptDst
array is null
, a new Point2D
object is allocated and stored into that element before storing the results of the transformation. Note that this method does not take any precautions to avoid problems caused by storing results into Point2D
objects that will be used as the source for calculations further down the source array. This method does guarantee that if a specified Point2D
object is both the source and destination for the same single point transform operation then the results will not be stored until the calculations are complete to avoid storing the results on top of the operands. If, however, the destination Point2D
object for one operation is the same object as the source Point2D
object for another operation further down the source array then the original coordinates in that point are overwritten before they can be converted.
ptSrc
- the array containing the source point objectsptDst
- the array into which the transform point objects are returnedsrcOff
- the offset to the first point object to be transformed in the source arraydstOff
- the offset to the location of the first transformed point object that is stored in the destination arraynumPts
- the number of point objects to be transformedpublic void transform(float[] srcPts, int srcOff, float[] dstPts, int dstOff, int numPts)
[x0, y0, x1, y1, ..., xn, yn]
.ptSrc
- the array containing the source point coordinates. Each point is stored as a pair of x, y coordinates.ptDst
- the array into which the transformed point coordinates are returned. Each point is stored as a pair of x, y coordinates.srcOff
- the offset to the first point to be transformed in the source arraydstOff
- the offset to the location of the first transformed point that is stored in the destination arraynumPts
- the number of points to be transformedpublic void transform(double[] srcPts, int srcOff, double[] dstPts, int dstOff, int numPts)
[x0, y0, x1, y1, ..., xn, yn]
.srcPts
- the array containing the source point coordinates. Each point is stored as a pair of x, y coordinates.dstPts
- the array into which the transformed point coordinates are returned. Each point is stored as a pair of x, y coordinates.srcOff
- the offset to the first point to be transformed in the source arraydstOff
- the offset to the location of the first transformed point that is stored in the destination arraynumPts
- the number of point objects to be transformedpublic void transform(float[] srcPts, int srcOff, double[] dstPts, int dstOff, int numPts)
[x0, y0, x1, y1, ..., xn, yn]
.ptSrc
- the array containing the source point coordinates. Each point is stored as a pair of x, y coordinates.ptDst
- the array into which the transformed point coordinates are returned. Each point is stored as a pair of x, y coordinates.srcOff
- the offset to the first point to be transformed in the source arraydstOff
- the offset to the location of the first transformed point that is stored in the destination arraynumPts
- the number of points to be transformedpublic void transform(double[] srcPts, int srcOff, float[] dstPts, int dstOff, int numPts)
[x0, y0, x1, y1, ..., xn, yn]
.srcPts
- the array containing the source point coordinates. Each point is stored as a pair of x, y coordinates.dstPts
- the array into which the transformed point coordinates are returned. Each point is stored as a pair of x, y coordinates.srcOff
- the offset to the first point to be transformed in the source arraydstOff
- the offset to the location of the first transformed point that is stored in the destination arraynumPts
- the number of point objects to be transformedpublic Point2D inverseTransform(Point2D ptSrc, Point2D ptDst)
ptSrc
and stores the result in ptDst
. If ptDst
is null
, a new Point2D
object is allocated and then the result of the transform is stored in this object. In either case, ptDst
, which contains the transformed point, is returned for convenience. If ptSrc
and ptDst
are the same object, the input point is correctly overwritten with the transformed point.ptSrc
- the point to be inverse transformedptDst
- the resulting transformed pointptDst
, which contains the result of the inverse transform.NoninvertibleTransformException
- if the matrix cannot be inverted.public void inverseTransform(double[] srcPts, int srcOff, double[] dstPts, int dstOff, int numPts)
[x0, y0, x1, y1, ..., xn, yn]
.srcPts
- the array containing the source point coordinates. Each point is stored as a pair of x, y coordinates.dstPts
- the array into which the transformed point coordinates are returned. Each point is stored as a pair of x, y coordinates.srcOff
- the offset to the first point to be transformed in the source arraydstOff
- the offset to the location of the first transformed point that is stored in the destination arraynumPts
- the number of point objects to be transformedNoninvertibleTransformException
- if the matrix cannot be inverted.public Point2D deltaTransform(Point2D ptSrc, Point2D ptDst)
ptSrc
and stores the result in ptDst
. A relative distance vector is transformed without applying the translation components of the affine transformation matrix using the following equations: [ x' ] [ m00 m01 (m02) ] [ x ] [ m00x + m01y ] [ y' ] = [ m10 m11 (m12) ] [ y ] = [ m10x + m11y ] [ (1) ] [ (0) (0) ( 1 ) ] [ (1) ] [ (1) ]If
ptDst
is null
, a new Point2D
object is allocated and then the result of the transform is stored in this object. In either case, ptDst
, which contains the transformed point, is returned for convenience. If ptSrc
and ptDst
are the same object, the input point is correctly overwritten with the transformed point.ptSrc
- the distance vector to be delta transformedptDst
- the resulting transformed distance vectorptDst
, which contains the result of the transformation.public void deltaTransform(double[] srcPts, int srcOff, double[] dstPts, int dstOff, int numPts)
[ x' ] [ m00 m01 (m02) ] [ x ] [ m00x + m01y ] [ y' ] = [ m10 m11 (m12) ] [ y ] = [ m10x + m11y ] [ (1) ] [ (0) (0) ( 1 ) ] [ (1) ] [ (1) ]The two coordinate array sections can be exactly the same or can be overlapping sections of the same array without affecting the validity of the results. This method ensures that no source coordinates are overwritten by a previous operation before they can be transformed. The coordinates are stored in the arrays starting at the indicated offset in the order
[x0, y0, x1, y1, ..., xn, yn]
.srcPts
- the array containing the source distance vectors. Each vector is stored as a pair of relative x, y coordinates.dstPts
- the array into which the transformed distance vectors are returned. Each vector is stored as a pair of relative x, y coordinates.srcOff
- the offset to the first vector to be transformed in the source arraydstOff
- the offset to the location of the first transformed vector that is stored in the destination arraynumPts
- the number of vector coordinate pairs to be transformedpublic Shape createTransformedShape(Shape pSrc)
Shape
after it has been transformed by this transform.pSrc
- the specified Shape
object to be transformed by this transform.Shape
object that defines the geometry of the transformed Shape
.public String toString()
String
that represents the value of this {@link Object}.String
representing the value of this Object
.public boolean isIdentity()
true
if this AffineTransform
is an identity transform.true
if this AffineTransform
is an identity transform; false
otherwise.public Object clone()
AffineTransform
object.Object
that is a copy of this AffineTransform
object.public int hashCode()
public boolean equals(Object obj)
true
if this AffineTransform
represents the same affine coordinate transform as the specified argument.obj
- the Object
to test for equality with this AffineTransform
true
if obj
equals this AffineTransform
object; false
otherwise.