Affine2D 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 ]
In some variations of the rotate methods in the
Affine2D class, a double-precision argument
specifies the angle of rotation in radians.
These methods have special handling for rotations of approximately
90 degrees (including multiples such as 180, 270, and 360 degrees),
so that the common case of quadrant rotation is handled more
efficiently.
This special handling can cause angles very close to multiples of
90 degrees to be treated as if they were exact multiples of
90 degrees.
For small multiples of 90 degrees the range of angles treated
as a quadrant rotation is approximately 0.00000121 degrees wide.
This section explains why such special care is needed and how
it is implemented.
Since 90 degrees is represented as PI/2 in radians,
and since PI is a transcendental (and therefore irrational) number,
it is not possible to exactly represent a multiple of 90 degrees as
an exact double precision value measured in radians.
As a result it is theoretically impossible to describe quadrant
rotations (90, 180, 270 or 360 degrees) using these values.
Double precision floating point values can get very close to
non-zero multiples of PI/2 but never close enough
for the sine or cosine to be exactly 0.0, 1.0 or -1.0.
The implementations of Math.sin() and
Math.cos() correspondingly never return 0.0
for any case other than Math.sin(0.0).
These same implementations do, however, return exactly 1.0 and
-1.0 for some range of numbers around each multiple of 90
degrees since the correct answer is so close to 1.0 or -1.0 that
the double precision significand cannot represent the difference
as accurately as it can for numbers that are near 0.0.
The net result of these issues is that if the
Math.sin() and Math.cos() methods
are used to directly generate the values for the matrix modifications
during these radian-based rotation operations then the resulting
transform is never strictly classifiable as a quadrant rotation
even for a simple case like rotate(Math.PI/2.0),
due to minor variations in the matrix caused by the non-0.0 values
obtained for the sine and cosine.
If these transforms are not classified as quadrant rotations then
subsequent code which attempts to optimize further operations based
upon the type of the transform will be relegated to its most general
implementation.
Because quadrant rotations are fairly common,
this class should handle these cases reasonably quickly, both in
applying the rotations to the transform and in applying the resulting
transform to the coordinates.
To facilitate this optimal handling, the methods which take an angle
of rotation measured in radians attempt to detect angles that are
intended to be quadrant rotations and treat them as such.
These methods therefore treat an angle theta as a quadrant
rotation if either Math.sin(theta) or
Math.cos(theta) returns exactly 1.0 or -1.0.
As a rule of thumb, this property holds true for a range of
approximately 0.0000000211 radians (or 0.00000121 degrees) around
small multiples of Math.PI/2.0.
| Modifier and Type | Field and Description |
|---|---|
| private static final long |
| Access | Constructor and Description |
|---|---|
| private | |
| public | |
| public | Affine2D(BaseTransform
the Tx)BaseTransform object to copyConstructs a new |
| public | Affine2D(float
the X coordinate scaling element of the 3x3 matrix mxx, float the Y coordinate shearing element of the 3x3 matrix myx, float the X coordinate shearing element of the 3x3 matrix mxy, float the Y coordinate scaling element of the 3x3 matrix myy, float the X coordinate translation element of the 3x3 matrix mxt, float the Y coordinate translation element of the 3x3 matrix myt)Constructs a new |
| public | Affine2D(double
the X coordinate scaling element of the 3x3 matrix mxx, double the Y coordinate shearing element of the 3x3 matrix myx, double the X coordinate shearing element of the 3x3 matrix mxy, double the Y coordinate scaling element of the 3x3 matrix myy, double the X coordinate translation element of the 3x3 matrix mxt, double the Y coordinate translation element of the 3x3 matrix myt)Constructs a new |
| Modifier and Type | Method and Description |
|---|---|
| private static double | |
| public BaseTransform | |
| public Affine2D | Returns: a newAffine2D object representing the
inverse transformation.Implements abstract com. Returns an |
| public Point2D | Returns: ptDst, which contains the result of the
transformation.the distance vector to be delta transformed ptSrc, Point2D the resulting transformed distance vector ptDst)Transforms the relative distance vector specified by
|
| public BaseTransform | deriveWithConcatenation(double mxx, double myx, double mxy, double myy, double mxt, double myt)
Implements abstract com.
|
| public BaseTransform | deriveWithConcatenation(double mxx, double mxy, double mxz, double mxt, double myx, double myy, double myz, double myt, double mzx, double mzy, double mzz, double mzt)
Implements abstract com.
|
| public BaseTransform | deriveWithConcatenation(BaseTransform tx)
Implements abstract com.
|
| public BaseTransform | deriveWithNewTransform(BaseTransform tx)
Implements abstract com.
|
| public BaseTransform | |
| public BaseTransform | deriveWithPreTranslation(double mxt, double myt)
Implements abstract com.
|
| public BaseTransform | deriveWithRotation(double theta, double axisX, double axisY, double axisZ)
Implements abstract com.
|
| public BaseTransform | deriveWithScale(double mxx, double myy, double mzz)
Implements abstract com.
|
| public BaseTransform | deriveWithTranslation(double mxt, double myt)
Implements abstract com.
|
| public BaseTransform | deriveWithTranslation(double mxt, double myt, double mzt)
Implements abstract com.
|
| public boolean | Returns: true if obj equals this
Affine2D object; false otherwise.the obj)Object to test for equality with this
Affine2DOverrides com. Returns |
| public BaseTransform. | |
| public int | Returns: a hash code for this transform.Overrides com. Returns the hashcode for this transform. |
| public boolean | |
| public void | preConcatenate(BaseTransform
the Tx)BaseTransform object to be
concatenated with this Affine2D object.Concatenates a |
| public void | quadrantRotate(int
the number of 90 degree arcs to rotate by numquadrants)Concatenates this transform with a transform that rotates coordinates by the specified number of quadrants. |
| public void | quadrantRotate(int
the number of 90 degree arcs to rotate by numquadrants, double the X coordinate of the rotation anchor point anchorx, double the Y coordinate of the rotation anchor point anchory)Concatenates this transform with a transform that rotates coordinates by the specified number of quadrants around the specified anchor point. |
| protected void | reset3Delements()
Implements abstract com. Resets the 3D (Z) components of the matrix to identity settings (if they are present). |
| public void | restoreTransform(double mxx, double myx, double mxy, double myy, double mxt, double myt)
Implements abstract com. This function is only guaranteed to succeed if the transform is of degree AFFINE2D or less and the matrix parameters specified came from this same instance. |
| public void | restoreTransform(double mxx, double mxy, double mxz, double mxt, double myx, double myy, double myz, double myt, double mzx, double mzy, double mzz, double mzt)
Implements abstract com. This function is only guaranteed to succeed if the matrix parameters specified came from this same instance. |
| public void | rotate(double
the angle of rotation measured in radians theta, double the X coordinate of the rotation anchor point anchorx, double the Y coordinate of the rotation anchor point anchory)Concatenates this transform with a transform that rotates coordinates around an anchor point. |
| public void | rotate(double
the X coordinate of the rotation vector vecx, double the Y coordinate of the rotation vector vecy)Concatenates this transform with a transform that rotates coordinates according to a rotation vector. |
| public void | rotate(double
the X coordinate of the rotation vector vecx, double the Y coordinate of the rotation vector vecy, double the X coordinate of the rotation anchor point anchorx, double the Y coordinate of the rotation anchor point anchory)Concatenates this transform with a transform that rotates coordinates around an anchor point according to a rotation vector. |
| public void | setToQuadrantRotation(int
the number of 90 degree arcs to rotate by numquadrants)Sets this transform to a rotation transformation that rotates coordinates by the specified number of quadrants. |
| public void | setToQuadrantRotation(int
the number of 90 degree arcs to rotate by numquadrants, double the X coordinate of the rotation anchor point anchorx, double the Y coordinate of the rotation anchor point anchory)Sets this transform to a translated rotation transformation that rotates coordinates by the specified number of quadrants around the specified anchor point. |
| public void | setToRotation(double
the angle of rotation measured in radians theta)Sets this transform to a rotation transformation. |
| public void | setToRotation(double
the angle of rotation measured in radians theta, double the X coordinate of the rotation anchor point anchorx, double the Y coordinate of the rotation anchor point anchory)Sets this transform to a translated rotation transformation. |
| public void | setToRotation(double
the X coordinate of the rotation vector vecx, double the Y coordinate of the rotation vector vecy)Sets this transform to a rotation transformation that rotates coordinates according to a rotation vector. |
| public void | setToRotation(double
the X coordinate of the rotation vector vecx, double the Y coordinate of the rotation vector vecy, double the X coordinate of the rotation anchor point anchorx, double the Y coordinate of the rotation anchor point anchory)Sets this transform to a rotation transformation that rotates coordinates around an anchor point according to a rotation vector. |
| public void | setToScale(double
the factor by which coordinates are scaled along the
X axis direction sx, double the factor by which coordinates are scaled along the
Y axis direction sy)Sets this transform to a scaling transformation. |
| public void | setToTranslation(double
the distance by which coordinates are translated in the
X axis direction tx, double the distance by which coordinates are translated in the
Y axis direction ty)Sets this transform to a translation transformation. |
| public void | setTransform(BaseTransform
the Tx)BaseTransform object from which to
copy the transformImplements abstract com. Sets this transform to a copy of the transform in the specified
|
| public String | Returns: aString representing the value of this
Object.Overrides com. Returns a |
| public void | transform(Point2D[]
the array containing the source point objects ptSrc, int the offset to the first point object to be
transformed in the source array srcOff, Point2D[] the array into which the transform point objects are
returned ptDst, int the offset to the location of the first
transformed point object that is stored in the destination array dstOff, int the number of point objects to be transformed numPts)Transforms an array of point objects by this transform. |
| BASE_HASH | back to summary |
|---|---|
| private static final long BASE_HASH | |
| Affine2D | back to summary |
|---|---|
| private Affine2D(double mxx, double myx, double mxy, double myy, double mxt, double myt, int state) | |
| Affine2D | back to summary |
|---|---|
| public Affine2D() Constructs a new | |
| Affine2D | back to summary |
|---|---|
| public Affine2D(BaseTransform Tx) Constructs a new
| |
| Affine2D | back to summary |
|---|---|
| public Affine2D(float mxx, float myx, float mxy, float myy, float mxt, float myt) Constructs a new
| |
| Affine2D | back to summary |
|---|---|
| public Affine2D(double mxx, double myx, double mxy, double myy, double mxt, double myt) Constructs a new
| |
| _matround | back to summary |
|---|---|
| private static double _matround(double matval) | |
| copy | back to summary |
|---|---|
| public BaseTransform copy() Implements abstract com.
| |
| createInverse | back to summary |
|---|---|
| public Affine2D createInverse() throws NoninvertibleTransformException Implements abstract com. Returns an
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
| |
| deltaTransform | back to summary |
|---|---|
| public Point2D deltaTransform(Point2D ptSrc, Point2D ptDst) Transforms the relative distance vector specified by
[ 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.
| |
| deriveWithConcatenation | back to summary |
|---|---|
| public BaseTransform deriveWithConcatenation(double mxx, double myx, double mxy, double myy, double mxt, double myt) Implements abstract com.
| |
| deriveWithConcatenation | back to summary |
|---|---|
| public BaseTransform deriveWithConcatenation(double mxx, double mxy, double mxz, double mxt, double myx, double myy, double myz, double myt, double mzx, double mzy, double mzz, double mzt) Implements abstract com.
| |
| deriveWithConcatenation | back to summary |
|---|---|
| public BaseTransform deriveWithConcatenation(BaseTransform tx) Implements abstract com.
| |
| deriveWithNewTransform | back to summary |
|---|---|
| public BaseTransform deriveWithNewTransform(BaseTransform tx) Implements abstract com.
| |
| deriveWithPreConcatenation | back to summary |
|---|---|
| public BaseTransform deriveWithPreConcatenation(BaseTransform tx) Implements abstract com.
| |
| deriveWithPreTranslation | back to summary |
|---|---|
| public BaseTransform deriveWithPreTranslation(double mxt, double myt) Implements abstract com.
| |
| deriveWithRotation | back to summary |
|---|---|
| public BaseTransform deriveWithRotation(double theta, double axisX, double axisY, double axisZ) Implements abstract com.
| |
| deriveWithScale | back to summary |
|---|---|
| public BaseTransform deriveWithScale(double mxx, double myy, double mzz) Implements abstract com.
| |
| deriveWithTranslation | back to summary |
|---|---|
| public BaseTransform deriveWithTranslation(double mxt, double myt) Implements abstract com.
| |
| deriveWithTranslation | back to summary |
|---|---|
| public BaseTransform deriveWithTranslation(double mxt, double myt, double mzt) Implements abstract com.
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object obj) Overrides com. Returns | |
| getDegree | back to summary |
|---|---|
| public BaseTransform. Implements abstract com.
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides com. Returns the hashcode for this transform. The base algorithm for
computing the hashcode is defined by the implementation in
the
| |
| is2D | back to summary |
|---|---|
| public boolean is2D() Overrides com.
| |
| preConcatenate | back to summary |
|---|---|
| public void preConcatenate(BaseTransform Tx) Concatenates a
[this] = [Tx] x [this]
| |
| quadrantRotate | back to summary |
|---|---|
| public void quadrantRotate(int numquadrants) Concatenates this transform with a transform that rotates coordinates by the specified number of quadrants. This is equivalent to calling:
rotate(numquadrants * Math.PI / 2.0);
Rotating by a positive number of quadrants rotates points on
the positive X axis toward the positive Y axis.
| |
| quadrantRotate | back to summary |
|---|---|
| public void quadrantRotate(int numquadrants, double anchorx, double anchory) Concatenates this transform with a transform that rotates coordinates by the specified number of quadrants around the specified anchor point. This method is equivalent to calling:
rotate(numquadrants * Math.PI / 2.0, anchorx, anchory);
Rotating by a positive number of quadrants rotates points on
the positive X axis toward the positive Y axis.
| |
| reset3Delements | back to summary |
|---|---|
| protected void reset3Delements() Implements abstract com. Doc from com. Resets the 3D (Z) components of the matrix to identity settings (if they are present). This is a NOP unless the transform is Affine3D in which case it needs to reset its added fields.
| |
| restoreTransform | back to summary |
|---|---|
| public void restoreTransform(double mxx, double myx, double mxy, double myy, double mxt, double myt) Implements abstract com. Doc from com. This function is only guaranteed to succeed if the transform is of degree AFFINE2D or less and the matrix parameters specified came from this same instance. In practice, they will also tend to succeed if they specify a transform of Degree less than or equal to the Degree of this instance as well, but the intent of this method is to restore the transform that had been read out of this transform into local variables.
| |
| restoreTransform | back to summary |
|---|---|
| public void restoreTransform(double mxx, double mxy, double mxz, double mxt, double myx, double myy, double myz, double myt, double mzx, double mzy, double mzz, double mzt) Implements abstract com. Doc from com. This function is only guaranteed to succeed if the matrix parameters specified came from this same instance. In practice, they will also tend to succeed if they specify a transform of Degree less than or equal to the Degree of this instance as well, but the intent of this method is to restore the transform that had been read out of this transform into local variables.
| |
| rotate | back to summary |
|---|---|
| public void rotate(double theta, double anchorx, double anchory) Concatenates this transform with a transform that rotates coordinates around an anchor point. This operation is equivalent to translating the coordinates so that the anchor point is at the origin (S1), then rotating them about the new origin (S2), and finally translating so that the intermediate origin is restored to the coordinates of the original anchor point (S3). This operation is equivalent to the following sequence of calls:
translate(anchorx, anchory); // S3: final translation
rotate(theta); // S2: rotate around anchor
translate(-anchorx, -anchory); // S1: translate anchor to origin
Rotating by a positive angle theta rotates points on the positive
X axis toward the positive Y axis.
Note also the discussion of
Handling 90-Degree Rotations
above.
| |
| rotate | back to summary |
|---|---|
| public void rotate(double vecx, double vecy) Concatenates this transform with a transform that rotates
coordinates according to a rotation vector.
All coordinates rotate about the origin by the same amount.
The amount of rotation is such that coordinates along the former
positive X axis will subsequently align with the vector pointing
from the origin to the specified vector coordinates.
If both
rotate(Math.atan2(vecy, vecx));
| |
| rotate | back to summary |
|---|---|
| public void rotate(double vecx, double vecy, double anchorx, double anchory) Concatenates this transform with a transform that rotates
coordinates around an anchor point according to a rotation
vector.
All coordinates rotate about the specified anchor coordinates
by the same amount.
The amount of rotation is such that coordinates along the former
positive X axis will subsequently align with the vector pointing
from the origin to the specified vector coordinates.
If both
rotate(Math.atan2(vecy, vecx), anchorx, anchory);
| |
| setToQuadrantRotation | back to summary |
|---|---|
| public void setToQuadrantRotation(int numquadrants) Sets this transform to a rotation transformation that rotates coordinates by the specified number of quadrants. This operation is equivalent to calling:
setToRotation(numquadrants * Math.PI / 2.0);
Rotating by a positive number of quadrants rotates points on
the positive X axis toward the positive Y axis.
| |
| setToQuadrantRotation | back to summary |
|---|---|
| public void setToQuadrantRotation(int numquadrants, double anchorx, double anchory) Sets this transform to a translated rotation transformation that rotates coordinates by the specified number of quadrants around the specified anchor point. This operation is equivalent to calling:
setToRotation(numquadrants * Math.PI / 2.0, anchorx, anchory);
Rotating by a positive number of quadrants rotates points on
the positive X axis toward the positive Y axis.
| |
| setToRotation | back to summary |
|---|---|
| public void setToRotation(double theta) Sets this transform to a rotation transformation. The matrix representing this transform becomes:
[ cos(theta) -sin(theta) 0 ]
[ sin(theta) cos(theta) 0 ]
[ 0 0 1 ]
Rotating by a positive angle theta rotates points on the positive
X axis toward the positive Y axis.
Note also the discussion of
Handling 90-Degree Rotations
above.
| |
| setToRotation | back to summary |
|---|---|
| public void setToRotation(double theta, double anchorx, double anchory) Sets this transform to a translated rotation transformation. This operation is equivalent to translating the coordinates so that the anchor point is at the origin (S1), then rotating them about the new origin (S2), and finally translating so that the intermediate origin is restored to the coordinates of the original anchor point (S3). This operation is equivalent to the following sequence of calls:
setToTranslation(anchorx, anchory); // S3: final translation
rotate(theta); // S2: rotate around anchor
translate(-anchorx, -anchory); // S1: translate anchor to origin
The 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 by a positive angle theta rotates points on the positive
X axis toward the positive Y axis.
Note also the discussion of
Handling 90-Degree Rotations
above.
| |
| setToRotation | back to summary |
|---|---|
| public void setToRotation(double vecx, double vecy) Sets this transform to a rotation transformation that rotates
coordinates according to a rotation vector.
All coordinates rotate about the origin by the same amount.
The amount of rotation is such that coordinates along the former
positive X axis will subsequently align with the vector pointing
from the origin to the specified vector coordinates.
If both
setToRotation(Math.atan2(vecy, vecx));
| |
| setToRotation | back to summary |
|---|---|
| public void setToRotation(double vecx, double vecy, double anchorx, double anchory) Sets this transform to a rotation transformation that rotates
coordinates around an anchor point according to a rotation
vector.
All coordinates rotate about the specified anchor coordinates
by the same amount.
The amount of rotation is such that coordinates along the former
positive X axis will subsequently align with the vector pointing
from the origin to the specified vector coordinates.
If both
setToTranslation(Math.atan2(vecy, vecx), anchorx, anchory);
| |
| setToScale | back to summary |
|---|---|
| public void setToScale(double sx, double sy) Sets this transform to a scaling transformation. The matrix representing this transform becomes:
[ sx 0 0 ]
[ 0 sy 0 ]
[ 0 0 1 ]
| |
| setToTranslation | back to summary |
|---|---|
| public void setToTranslation(double tx, double ty) Sets this transform to a translation transformation. The matrix representing this transform becomes:
[ 1 0 tx ]
[ 0 1 ty ]
[ 0 0 1 ]
| |
| setTransform | back to summary |
|---|---|
| public void setTransform(BaseTransform Tx) Implements abstract com. Sets this transform to a copy of the transform in the specified
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides com. Returns a | |
| transform | back to summary |
|---|---|
| public void transform(Point2D[] ptSrc, int srcOff, Point2D[] ptDst, int dstOff, int numPts) Transforms an array of point objects by this transform.
If any element of the
Note that this method does not take any precautions to
avoid problems caused by storing results into
| |