Top
AffineTransformation()
AffineTransformation(double[])
AffineTransformation(double, double, double, double, double, double)
AffineTransformation(AffineTransformation)
AffineTransformation(Coordinate, Coordinate, Coordinate, Coordinate, Coordinate, Coordinate)
reflectionInstance(double, double, double, double): AffineTransformation
reflectionInstance(double, double): AffineTransformation
rotationInstance(double): AffineTransformation
rotationInstance(double, double): AffineTransformation
rotationInstance(double, double, double): AffineTransformation
rotationInstance(double, double, double, double): AffineTransformation
scaleInstance(double, double): AffineTransformation
scaleInstance(double, double, double, double): AffineTransformation
shearInstance(double, double): AffineTransformation
translationInstance(double, double): AffineTransformation
setToIdentity(): AffineTransformation
setTransformation(double, double, double, double, double, double): AffineTransformation
setTransformation(AffineTransformation): AffineTransformation
getMatrixEntries(): double[]
getDeterminant(): double
getInverse(): AffineTransformation
setToReflectionBasic(double, double, double, double): AffineTransformation
setToReflection(double, double, double, double): AffineTransformation
setToReflection(double, double): AffineTransformation
setToRotation(double): AffineTransformation
setToRotation(double, double): AffineTransformation
setToRotation(double, double, double): AffineTransformation
setToRotation(double, double, double, double): AffineTransformation
setToScale(double, double): AffineTransformation
setToShear(double, double): AffineTransformation
setToTranslation(double, double): AffineTransformation
reflect(double, double, double, double): AffineTransformation
reflect(double, double): AffineTransformation
rotate(double): AffineTransformation
rotate(double, double): AffineTransformation
rotate(double, double, double): AffineTransformation
rotate(double, double, double, double): AffineTransformation
scale(double, double): AffineTransformation
shear(double, double): AffineTransformation
translate(double, double): AffineTransformation
compose(AffineTransformation): AffineTransformation
composeBefore(AffineTransformation): AffineTransformation
transform(Coordinate, Coordinate): Coordinate
transform(Geometry): Geometry
transform(CoordinateSequence, int): void
filter(CoordinateSequence, int): void
isGeometryChanged(): boolean
isDone(): boolean
isIdentity(): boolean
equals(Object): boolean
toString(): String
clone(): Object
Bottom

Class AffineTransformation

Hierarchy: Object , AffineTransformation
All Implemented Interfaces: Cloneable , CoordinateSequenceFilter
public class AffineTransformation
implements CoordinateSequenceFilter, Cloneable
Represents an affine transformation on the 2D Cartesian plane. It can be used to transform a Coordinate or Geometry. An affine transformation is a mapping of the 2D plane into itself via a series of transformations of the following basic types:
  • reflection (through a line)
  • rotation (around the origin)
  • scaling (relative to the origin)
  • shearing (in both the X and Y directions)
  • translation
In general, affine transformations preserve straightness and parallel lines, but do not preserve distance or shape.

An affine transformation can be represented by a 3x3 matrix in the following form:

 T = | m00 m01 m02 |
     | m10 m11 m12 |
     |  0   0   1  |
 
A coordinate P = (x, y) can be transformed to a new coordinate P' = (x', y') by representing it as a 3x1 matrix and using matrix multiplication to compute:
 | x' |  = T x | x |
 | y' |        | y |
 | 1  |        | 1 |
 

Transformation Composition

Affine transformations can be composed using the compose method. Composition is computed via multiplication of the transformation matrices, and is defined as:
 A.compose(B) = TB x TA
 
This produces a transformation whose effect is that of A followed by B. The methods reflect, rotate, scale, shear, and translate have the effect of composing a transformation of that type with the transformation they are invoked on.

The composition of transformations is in general not commutative.

Transformation Inversion

Affine transformations may be invertible or non-invertible. If a transformation is invertible, then there exists an inverse transformation which when composed produces the identity transformation. The getInverse method computes the inverse of a transformation, if one exists.
Authors:
Martin Davis
public AffineTransformation()
Constructs a new identity transformation
public AffineTransformation(double[] matrix)
Constructs a new transformation whose matrix has the specified values.
Parameters:
matrix - matrix an array containing the 6 values { m00, m01, m02, m10, m11, m12 }
Throws:
NullPointerException - NullPointerException if matrix is null
ArrayIndexOutOfBoundsException - ArrayIndexOutOfBoundsException if matrix is too small
public AffineTransformation(double m00, double m01, double m02, double m10, double m11, double m12)
Constructs a new transformation whose matrix has the specified values.
Parameters:
m00 - m00 the entry for the [0, 0] element in the transformation matrix
m01 - m01 the entry for the [0, 1] element in the transformation matrix
m02 - m02 the entry for the [0, 2] element in the transformation matrix
m10 - m10 the entry for the [1, 0] element in the transformation matrix
m11 - m11 the entry for the [1, 1] element in the transformation matrix
m12 - m12 the entry for the [1, 2] element in the transformation matrix
public AffineTransformation(AffineTransformation trans)
Constructs a transformation which is a copy of the given one.
Parameters:
trans - trans the transformation to copy
public AffineTransformation(Coordinate src0, Coordinate src1, Coordinate src2, Coordinate dest0, Coordinate dest1, Coordinate dest2)
Constructs a transformation which maps the given source points into the given destination points.
Parameters:
src0 - src0 source point 0
src1 - src1 source point 1
src2 - src2 source point 2
dest0 - dest0 the mapped point for source point 0
dest1 - dest1 the mapped point for source point 1
dest2 - dest2 the mapped point for source point 2
public static AffineTransformation reflectionInstance(double x0, double y0, double x1, double y1)
Creates a transformation for a reflection about the line (x0,y0) - (x1,y1).
Parameters:
x0 - x0 the x-ordinate of a point on the reflection line
y0 - y0 the y-ordinate of a point on the reflection line
x1 - x1 the x-ordinate of a another point on the reflection line
y1 - y1 the y-ordinate of a another point on the reflection line
Returns:
a transformation for the reflection
public static AffineTransformation reflectionInstance(double x, double y)
Creates a transformation for a reflection about the line (0,0) - (x,y).
Parameters:
x - x the x-ordinate of a point on the reflection line
y - y the y-ordinate of a point on the reflection line
Returns:
a transformation for the reflection
public static AffineTransformation rotationInstance(double theta)
Creates a transformation for a rotation about the origin by an angle theta. Positive angles correspond to a rotation in the counter-clockwise direction.
Parameters:
theta - theta the rotation angle, in radians
Returns:
a transformation for the rotation
public static AffineTransformation rotationInstance(double sinTheta, double cosTheta)
Creates a transformation for a rotation by an angle theta, specified by the sine and cosine of the angle. This allows providing exact values for sin(theta) and cos(theta) for the common case of rotations of multiples of quarter-circles.
Parameters:
sinTheta - sinTheta the sine of the rotation angle
cosTheta - cosTheta the cosine of the rotation angle
Returns:
a transformation for the rotation
public static AffineTransformation rotationInstance(double theta, double x, double y)
Creates a transformation for a rotation about the point (x,y) by an angle theta. Positive angles correspond to a rotation in the counter-clockwise direction.
Parameters:
theta - theta the rotation angle, in radians
x - x the x-ordinate of the rotation point
y - y the y-ordinate of the rotation point
Returns:
a transformation for the rotation
public static AffineTransformation rotationInstance(double sinTheta, double cosTheta, double x, double y)
Creates a transformation for a rotation about the point (x,y) by an angle theta, specified by the sine and cosine of the angle. This allows providing exact values for sin(theta) and cos(theta) for the common case of rotations of multiples of quarter-circles.
Parameters:
sinTheta - sinTheta the sine of the rotation angle
cosTheta - cosTheta the cosine of the rotation angle
x - x the x-ordinate of the rotation point
y - y the y-ordinate of the rotation point
Returns:
a transformation for the rotation
public static AffineTransformation scaleInstance(double xScale, double yScale)
Creates a transformation for a scaling relative to the origin.
Parameters:
xScale - xScale the value to scale by in the x direction
yScale - yScale the value to scale by in the y direction
Returns:
a transformation for the scaling
public static AffineTransformation scaleInstance(double xScale, double yScale, double x, double y)
Creates a transformation for a scaling relative to the point (x,y).
Parameters:
xScale - xScale the value to scale by in the x direction
yScale - yScale the value to scale by in the y direction
x - x the x-ordinate of the point to scale around
y - y the y-ordinate of the point to scale around
Returns:
a transformation for the scaling
public static AffineTransformation shearInstance(double xShear, double yShear)
Creates a transformation for a shear.
Parameters:
xShear - xShear the value to shear by in the x direction
yShear - yShear the value to shear by in the y direction
Returns:
a transformation for the shear
public static AffineTransformation translationInstance(double x, double y)
Creates a transformation for a translation.
Parameters:
x - x the value to translate by in the x direction
y - y the value to translate by in the y direction
Returns:
a transformation for the translation
public AffineTransformation setToIdentity()
Sets this transformation to be the identity transformation. The identity transformation has the matrix:
 | 1 0 0 |
 | 0 1 0 |
 | 0 0 1 |
 
Returns:
this transformation, with an updated matrix
public AffineTransformation setTransformation(double m00, double m01, double m02, double m10, double m11, double m12)
Sets this transformation's matrix to have the given values.
Parameters:
m00 - m00 the entry for the [0, 0] element in the transformation matrix
m01 - m01 the entry for the [0, 1] element in the transformation matrix
m02 - m02 the entry for the [0, 2] element in the transformation matrix
m10 - m10 the entry for the [1, 0] element in the transformation matrix
m11 - m11 the entry for the [1, 1] element in the transformation matrix
m12 - m12 the entry for the [1, 2] element in the transformation matrix
Returns:
this transformation, with an updated matrix
public AffineTransformation setTransformation(AffineTransformation trans)
Sets this transformation to be a copy of the given one
Parameters:
trans - trans a transformation to copy
Returns:
this transformation, with an updated matrix
public double[] getMatrixEntries()
Gets an array containing the entries of the transformation matrix. Only the 6 non-trivial entries are returned, in the sequence:
 m00, m01, m02, m10, m11, m12
 
Returns:
an array of length 6
public double getDeterminant()
Computes the determinant of the transformation matrix. The determinant is computed as:
 | m00 m01 m02 |
 | m10 m11 m12 | = m00 * m11 - m01 * m10
 |  0   0   1  |
 
If the determinant is zero, the transform is singular (not invertible), and operations which attempt to compute an inverse will throw a NoninvertibleTransformException.
See also:
#getInverse()
Returns:
the determinant of the transformation
public AffineTransformation getInverse()
Computes the inverse of this transformation, if one exists. The inverse is the transformation which when composed with this one produces the identity transformation. A transformation has an inverse if and only if it is not singular (i.e. its determinant is non-zero). Geometrically, an transformation is non-invertible if it maps the plane to a line or a point. If no inverse exists this method will throw a NoninvertibleTransformationException.

The matrix of the inverse is equal to the inverse of the matrix for the transformation. It is computed as follows:

                 1
 inverse(A)  =  ---   x  adjoint(A)
                det

             =   1       |  m11  -m01   m01*m12-m02*m11  |
                ---   x  | -m10   m00  -m00*m12+m10*m02  |
                det      |  0     0     m00*m11-m10*m01  |

             = |  m11/det  -m01/det   m01*m12-m02*m11/det |
               | -m10/det   m00/det  -m00*m12+m10*m02/det |
               |   0           0          1               |

 
See also:
#getDeterminant()
Returns:
a new inverse transformation
Throws:
NoninvertibleTransformationException - NoninvertibleTransformationException
public AffineTransformation setToReflectionBasic(double x0, double y0, double x1, double y1)
Explicitly computes the math for a reflection. May not work.
Parameters:
x0 - x0 the X ordinate of one point on the reflection line
y0 - y0 the Y ordinate of one point on the reflection line
x1 - x1 the X ordinate of another point on the reflection line
y1 - y1 the Y ordinate of another point on the reflection line
Returns:
this transformation, with an updated matrix
public AffineTransformation setToReflection(double x0, double y0, double x1, double y1)
Sets this transformation to be a reflection about the line defined by a line (x0,y0) - (x1,y1).
Parameters:
x0 - x0 the X ordinate of one point on the reflection line
y0 - y0 the Y ordinate of one point on the reflection line
x1 - x1 the X ordinate of another point on the reflection line
y1 - y1 the Y ordinate of another point on the reflection line
Returns:
this transformation, with an updated matrix
public AffineTransformation setToReflection(double x, double y)
Sets this transformation to be a reflection about the line defined by vector (x,y). The transformation for a reflection is computed by:
 d = sqrt(x2 + y2)
 sin = y / d;
 cos = x / d;

 Tref = Trot(sin, cos) x Tscale(1, -1) x Trot(-sin, cos)
 
Parameters:
x - x the x-component of the reflection line vector
y - y the y-component of the reflection line vector
Returns:
this transformation, with an updated matrix
public AffineTransformation setToRotation(double theta)
Sets this transformation to be a rotation around the origin. A positive rotation angle corresponds to a counter-clockwise rotation. The transformation matrix for a rotation by an angle theta has the value:
 |  cos(theta)  -sin(theta)   0 |
 |  sin(theta)   cos(theta)   0 |
 |           0            0   1 |
 
Parameters:
theta - theta the rotation angle, in radians
Returns:
this transformation, with an updated matrix
public AffineTransformation setToRotation(double sinTheta, double cosTheta)
Sets this transformation to be a rotation around the origin by specifying the sin and cos of the rotation angle directly. The transformation matrix for the rotation has the value:
 |  cosTheta  -sinTheta   0 |
 |  sinTheta   cosTheta   0 |
 |         0          0   1 |
 
Parameters:
sinTheta - sinTheta the sine of the rotation angle
cosTheta - cosTheta the cosine of the rotation angle
Returns:
this transformation, with an updated matrix
public AffineTransformation setToRotation(double theta, double x, double y)
Sets this transformation to be a rotation around a given point (x,y). A positive rotation angle corresponds to a counter-clockwise rotation. The transformation matrix for a rotation by an angle theta has the value:
 |  cosTheta  -sinTheta   x-x*cos+y*sin |
 |  sinTheta   cosTheta   y-x*sin-y*cos |
 |           0            0   1 |
 
Parameters:
theta - theta the rotation angle, in radians
x - x the x-ordinate of the rotation point
y - y the y-ordinate of the rotation point
Returns:
this transformation, with an updated matrix
public AffineTransformation setToRotation(double sinTheta, double cosTheta, double x, double y)
Sets this transformation to be a rotation around a given point (x,y) by specifying the sin and cos of the rotation angle directly. The transformation matrix for the rotation has the value:
 |  cosTheta  -sinTheta   x-x*cos+y*sin |
 |  sinTheta   cosTheta   y-x*sin-y*cos |
 |         0          0         1       |
 
Parameters:
sinTheta - sinTheta the sine of the rotation angle
cosTheta - cosTheta the cosine of the rotation angle
x - x the x-ordinate of the rotation point
y - y the y-ordinate of the rotation point
Returns:
this transformation, with an updated matrix
public AffineTransformation setToScale(double xScale, double yScale)
Sets this transformation to be a scaling. The transformation matrix for a scale has the value:
 |  xScale      0  dx |
 |  1      yScale  dy |
 |  0           0   1 |
 
Parameters:
xScale - xScale the amount to scale x-ordinates by
yScale - yScale the amount to scale y-ordinates by
Returns:
this transformation, with an updated matrix
public AffineTransformation setToShear(double xShear, double yShear)
Sets this transformation to be a shear. The transformation matrix for a shear has the value:
 |  1      xShear  0 |
 |  yShear      1  0 |
 |  0           0  1 |
 
Note that a shear of (1, 1) is not equal to shear(1, 0) composed with shear(0, 1). Instead, shear(1, 1) corresponds to a mapping onto the line x = y.
Parameters:
xShear - xShear the x component to shear by
yShear - yShear the y component to shear by
Returns:
this transformation, with an updated matrix
public AffineTransformation setToTranslation(double dx, double dy)
Sets this transformation to be a translation. For a translation by the vector (x, y) the transformation matrix has the value:
 |  1  0  dx |
 |  1  0  dy |
 |  0  0   1 |
 
Parameters:
dx - dx the x component to translate by
dy - dy the y component to translate by
Returns:
this transformation, with an updated matrix
public AffineTransformation reflect(double x0, double y0, double x1, double y1)
Updates the value of this transformation to that of a reflection transformation composed with the current value.
Parameters:
x0 - x0 the x-ordinate of a point on the line to reflect around
y0 - y0 the y-ordinate of a point on the line to reflect around
x1 - x1 the x-ordinate of a point on the line to reflect around
y1 - y1 the y-ordinate of a point on the line to reflect around
Returns:
this transformation, with an updated matrix
public AffineTransformation reflect(double x, double y)
Updates the value of this transformation to that of a reflection transformation composed with the current value.
Parameters:
x - x the x-ordinate of the line to reflect around
y - y the y-ordinate of the line to reflect around
Returns:
this transformation, with an updated matrix
public AffineTransformation rotate(double theta)
Updates the value of this transformation to that of a rotation transformation composed with the current value. Positive angles correspond to a rotation in the counter-clockwise direction.
Parameters:
theta - theta the angle to rotate by, in radians
Returns:
this transformation, with an updated matrix
public AffineTransformation rotate(double sinTheta, double cosTheta)
Updates the value of this transformation to that of a rotation around the origin composed with the current value, with the sin and cos of the rotation angle specified directly.
Parameters:
sinTheta - sinTheta the sine of the angle to rotate by
cosTheta - cosTheta the cosine of the angle to rotate by
Returns:
this transformation, with an updated matrix
public AffineTransformation rotate(double theta, double x, double y)
Updates the value of this transformation to that of a rotation around a given point composed with the current value. Positive angles correspond to a rotation in the counter-clockwise direction.
Parameters:
theta - theta the angle to rotate by, in radians
x - x the x-ordinate of the rotation point
y - y the y-ordinate of the rotation point
Returns:
this transformation, with an updated matrix
public AffineTransformation rotate(double sinTheta, double cosTheta, double x, double y)
Updates the value of this transformation to that of a rotation around a given point composed with the current value, with the sin and cos of the rotation angle specified directly.
Parameters:
sinTheta - sinTheta the sine of the angle to rotate by
cosTheta - cosTheta the cosine of the angle to rotate by
x - x the x-ordinate of the rotation point
y - y the y-ordinate of the rotation point
Returns:
this transformation, with an updated matrix
public AffineTransformation scale(double xScale, double yScale)
Updates the value of this transformation to that of a scale transformation composed with the current value.
Parameters:
xScale - xScale the value to scale by in the x direction
yScale - yScale the value to scale by in the y direction
Returns:
this transformation, with an updated matrix
public AffineTransformation shear(double xShear, double yShear)
Updates the value of this transformation to that of a shear transformation composed with the current value.
Parameters:
xShear - xShear the value to shear by in the x direction
yShear - yShear the value to shear by in the y direction
Returns:
this transformation, with an updated matrix
public AffineTransformation translate(double x, double y)
Updates the value of this transformation to that of a translation transformation composed with the current value.
Parameters:
x - x the value to translate by in the x direction
y - y the value to translate by in the y direction
Returns:
this transformation, with an updated matrix
Updates this transformation to be the composition of this transformation with the given AffineTransformation. This produces a transformation whose effect is equal to applying this transformation followed by the argument transformation. Mathematically,
 A.compose(B) = TB x TA
 
Parameters:
trans - trans an affine transformation
Returns:
this transformation, with an updated matrix
public AffineTransformation composeBefore(AffineTransformation trans)
Updates this transformation to be the composition of a given AffineTransformation with this transformation. This produces a transformation whose effect is equal to applying the argument transformation followed by this transformation. Mathematically,
 A.composeBefore(B) = TA x TB
 
Parameters:
trans - trans an affine transformation
Returns:
this transformation, with an updated matrix
public Coordinate transform(Coordinate src, Coordinate dest)
Applies this transformation to the src coordinate and places the results in the dest coordinate (which may be the same as the source).
Parameters:
src - src the coordinate to transform
dest - dest the coordinate to accept the results
Returns:
the dest coordinate
public Geometry transform(Geometry g)
Creates a new Geometry which is the result of this transformation applied to the input Geometry.
Parameters:
g - g a Geometry
Returns:
a transformed Geometry
public void transform(CoordinateSequence seq, int i)
Applies this transformation to the i'th coordinate in the given CoordinateSequence.
Parameters:
seq - seq a CoordinateSequence
i - i the index of the coordinate to transform
public void filter(CoordinateSequence seq, int i)
Transforms the i'th coordinate in the input sequence
Parameters:
seq - seq a CoordinateSequence
i - i the index of the coordinate to transform
public boolean isGeometryChanged()
public boolean isDone()
Reports that this filter should continue to be executed until all coordinates have been transformed.
Returns:
false
public boolean isIdentity()
Tests if this transformation is the identity transformation.
Returns:
true if this is the identity transformation
public boolean equals(Object obj)
Tests if an object is an AffineTransformation and has the same matrix as this transformation.
Parameters:
obj - obj an object to test
Returns:
true if the given object is equal to this object
public String toString()
Gets a text representation of this transformation. The string is of the form:
 AffineTransformation[[m00, m01, m02], [m10, m11, m12]]
 
Returns:
a string representing this transformation
public Object clone()
Clones this transformation
Returns:
a copy of this transformation