Using Class Transform2D

The Transform2D class is responsible for implementing affine transformations org/wiki/Affine_transformation in 2-dimensional space. Affine transformations involve scaling, translating, rotating and sheering.

Any number of affine transformations can be accumulated into a single combined transformation and then applied to any number of coordinates with much lower cost than if all single transformations were applied separately. This is a common and practical scenario. For example, rotating a point P by an angle alpha around an axis of rotation that is not the center of the coordinate system can be achieved by the following sequence of affine transformations:

Implementing the above task with Transform2D would result in the following code:

Example 17.3. Rotating a point around an arbitrary axis

double point[]= { 10.0, 20.0};
double alpha= 60;
        
// builds a new identity transform
Transform2D xform= new Transform2D();
xform.translate(10.0, 20.0);
xform.rotate(alpha);
xform.translate(- 10.0, - 20.0);
        
// apply the transformation
xform.apply(point);

As you can see, the order of the transformations is the reverse of the order that was described in the above list. This is due to the nature of accumulating affine transformations. The accumulation simply has to take place in the reverse order to get the expected results.

Properties Of Affine Transformations

Important properties of affine transformations include:

  • Combining several affine transformations into a single transformation is done by concatenating the single transformations in reverse order.

  • Transformations can be reversed. In order to find the inverse of a given transformation, invoke the invert() method on a Transform2D instance. The transformation will be inverted by bringing any previously transformed points back to their original positions.

  • A matrix with a determinant of 0 is called singular matrix.

  • Not all matrices can be inverted. Inversion is not possible if the matrix is singular.

  • A matrix with a determinant of 1 or -1 is called orthogonal matrix.

  • Transposing a square matrix is a simple process where the values of columns and rows get exchanged. (The matrix is flipped at its diagonal). The resulting matrix is called the transposed matrix.

  • As you remember from your math lessons, the inverse of a square matrix is the same as the transposed matrix if and only if the matrix is orthogonal.

  • In case you have a Transform2D that is orthogonal (for example it consists only of rotation transformation), you can invert it more efficiently by invoking the transpose() method.

  • You can determine whether a Transform2D is orthogonal or not by evaluating the return value of method determinant().