# Affine Transformations

A transformation changes the positions of points in the plane.  A set of points, when transformed, may as a result acquire a different shape.

The transformations that move lines into lines, while preserving their intersection properties, are special and interesting, because they will move all polylines into polylines and all polygons into polygons.  These are the affine transformations.

Every affine transformation can be expressed as a transformation that fixes some special point (the "origin") followed by a simple translation of the entire plane.  These point-fixing transformations are the linear ones.

The theory of vector spaces demonstrates that linear transformations can be represented by matrices: two-by-two arrays of numbers (three-by-three in three dimensions).

Other representations are possible.  The commonest alternatives use three-by-three arrays in two dimensions and four-by-four arrays in three dimensions.  For a clear, accurate explanation of this matrix approach, please see http://www.css.tayloru.edu/~btoll/s99/424/res/mtu/Notes/geometry/geo-tran.htm .  Some more information about these alternatives also appears the Applications section below.

The most general linear transformation of the plane can further be broken down into a series of transformations with definite geometric interpretations.  For example, half of all linear transformations can be expressed as a skew transformation followed by a "squeeze" (change of aspect ratio) followed by a rotation.  At any point in this sequence a uniform scaling may be applied.  These operations are defined and illustrated below.

The other half of the transformations are obtained by first reflecting the points about a line.  A simple way to accomplish this is to introduce a coordinate system and interchange the x and y coordinates of every point: that is a reflection about the line y=x.

## Affine transformations and GIS

Transformations play many important roles in GIS development and use:

 The change from one projection to another is a transformation.  So is a change of datums or a change of coordinate systems. The "warping" or "rubber-sheeting" of images, to georegister them, is a transformation. Often, applying a suitable transformation to a configuration of shapes can simplify the analysis of their geometric relationships.

Every change of map projection is locally (over sufficiently small areas) well approximated by an affine transformation.  Thus, for example, changes among equal-area projections must have linear components involving skewings, rotations, and squeezes but not scalings: only the scalings change areas.  Similarly, conformal (shape-preserving) projections must locally not have any squeeze or skew components, because these operations change shapes: only the rotations and uniform scalings preserve shapes.  Thus, a good geometric understanding of affine transformations helps to conceptualize changes of projection.

Practically, affine transformations provide a useful family of operations one can apply to georeference map features.  Changes of coordinate system, changes of units of measurement, and referencing scanned paper maps are common applications.

The following sections describe linear transformations--the interesting parts of affine transformations--in more detail.  They are followed by a brief section on GIS applications and another section specific to the ArcView 3.x GIS software.

## Taxonomy of linear transformations

This is the reference figure.  The following figures will show what happens to this figures as it undergoes various transformations.

The origin is a light blue point.  The lines form a regular square grid.  The interior colors distinguish different vertical bands of square cells.

To describe transformations in detail, we use a coordinate system.  Let the coordinates of the origin in the reference figure be (0,0).  Establish units of measurement that make the squares exactly one unit on a side.
The blue vector, e1, extends one unit in the horizontal direction.  The red vector, e2, extends one unit in the vertical direction.  Every point in the plane has a unique expression as the sum of multiples of e1 and e2.  If we write p = x * e1 + y * e2, then x and y are the (usual) coordinates of the point p.  The red and blue vectors are the basis vectors for the coordinate system.
To understand or describe a transformation, it suffices to specify what the transformation does to the basis vectors.  This follows from some simple logic: If p is any point and T denotes transformation, linearity means that T(p) = T( x * e1 + y * e2) = x * T(e1) + y * T(e2).  Thus, once we know T(e1) and T(e2), we can easily determine T(p) for any point p.

Let numbers A, B, C, and D stand for the coefficients for the destinations of the two basis vectors.  Specifically, suppose the transformation moves e1 to A * e1 + C * e2 and e2 to B * e1 + D * e2 .  Conventionally these coefficients are written as the matrix

 A B C D

The effect of the transformation on an arbitrary point p = x * e1 + y * e2 is then the product of this matrix and the column vector (x, y):

T(p
= T(x * e1 + y * e2
= x * T(e1) + y * T(e2
= x * (A * e1 + C * e2) + y * (B * e1 + D * e2
= (x*A + y*B) e1  + (x*C + y*D) e2.

Matrices are also useful for describing affine transformations.  This is done with a geometric trick.  The usual (Euclidean) plane can be embedded in three dimensional Euclidean space as the set of all points (x, y, 1) at unit height above the xy plane.  The most general three dimensional linear transformation sends such a point to the point at location (A*x + B*y + C, D*x + E*y + F, G*x + H*y + I) where A, B, ..., I are the coefficients of the three-by-three matrix for the transformation.  When G = H = 0 and I = 1, the third coordinate of the new point will also be 1, so that the new point also lies in the original plane.  Such matrices with G = H = 0 and I = 1 therefore describe linear transformations of the embedded plane.

Evidently coefficients A, B, D, and E determine a linear transformation and coefficients C and F determine a parallel translation: that is, such three-by-three matrices describe affine two-dimensional transformations.  Here is what they look like in full matrix form:

 A B C D E F 0 0 1

Incidentally, even when G or H are nonzero or I is not 1, we can still create a transformation of the embedded plane by rescaling the new point until its height above the xy-plane is 1.  That is, the transformation determined by the most general three-by-three matrix is of the form

x --> (A*x + B*y + C)/(G*x + H*y + I)

y --> (D*x + E*y + F)/(G*x + H*y + I)

These are the projective transformations of the plane.

### Skew transformations

This is the result of a skew transformation of the reference figure.  The original reference lines are overlaid in gray.  The transformed basis vectors are shown in their original colors.  Evidently, e1 was not moved while e2 was moved to e1 + e2.  More formally, the coordinates for the transformation of e1 are (1, 0) and for the transformation of e2 are (1, 1).  This is written as the matrix
 1 1 0 1
The first column of numbers specifies where e1 goes and the second column specifies where e2 goes.

All skew transformations "look" like this one in the sense that

1. They preserve some line: every point on this line is left unmoved.
2. Lines parallel to the fixed line are sent to themselves.  Points along these parallel lines are shifted by an amount proportional to the (signed) distance from the fixed line.
3. The areas of shapes are unchanged.

If you choose a basis with e1 on the fixed line, then the matrix for any skew transformation will look like

 1 n 0 1

where n is some number.

Evidently, skew transformations change angles and distances in general.  (Distances along the preserved line are not changed, but most distances are changed.)

### "Squeezes" (aspect ratio changes)

A "squeeze" changes the aspect ratio of all horizontal rectangles without changing their areas.  The illustrated squeeze has shrunk horizontal distances by one-third (they were all multiplied by 2/3) while expanding all vertical distances by one-half (they were all multiplied by 3/2) to compensate.

The matrix for this squeeze is

 2/3 0 0 3/2
The most general squeeze looks like this, always expanding one direction to compensate for shrinking the other so that areas do not change.

If you choose a basis with e1 along one direction of a squeeze and e2 in the perpendicular direction, then the matrix will look like

 t 0 0 1/t

where t is a positive number.

Squeezes change angles and distances, but preserve areas.

### Rotations

A rotation preserves distance, angle, orientation, and area.  All shapes are transformed into congruent shapes.

(A modern view of geometry is that it is the study of groups of transformations.  From this point of view, rotations and translations define congruence.  That is, congruent shapes are those that can be transformed into each other by some sequence of rotations and translations.  Furthermore, what we mean by "shape" is whatever properties happen to be preserved by exactly these operations.)

This figure shows a 30 degree counterclockwise rotation.  It is considered a "positive" rotation because it moves the first basis vector  e1 toward the second, e2.  Its matrix is approximately

 0.866025 -0.5 0.5 0.866025

The value 0.866025 represents one-half the square root of three.

When the basis vectors are chosen perpendicular and of equal length, all rotation matrices are of the form

 Cos(w) -Sin(w) Sin(w) Cos(w)

where w is the angle of rotation.  This is practically the definition of the sine and cosine functions.  In particular, given any two numbers c and s for which c2 + s2 = 1, there exists an angle w for which c = cos(w) and s = sin(w).  The angle is unique up to a multiple of a full circle (360 degrees).

### Scalings

A (uniform) scaling simply expands or shrinks all distances by a constant amount.  This figure shows the result of scaling by 1.5.  Its matrix is
 1.5 0 0 1.5

No matter how the basis is selected, the matrix for a uniform scaling is always of the form

 r 0 0 r

for some number r.

Any scaling sends all shapes to similar shapes.  It preserves all angles and even all directions, too.  It changes areas, but in a uniform way: the area of any figure will be r2 times its original area after it is scaled by r.  Scalings change all nonzero distances.
The combination of a squeeze of size t and a scaling of size r will differentially scale the two basis directions.  The matrix for the combination is
 r*t 0 0 r/t

The scaling factor in the first (x) direction is r*t and in the second (y) direction is r/t.  Conversely, if these two nonzero scaling factors A and D, respectively, are independently specified, then you can determine that r = sqrt(A*D) and t = sqrt(A/D).  Thus squeezes and uniform scalings are just another way to describe possibly independent rescalings in the two basis directions.

There are other linear transformations, too, such as linear projections (all points in the plane are collapsed onto a single line), but these are not invertible: they cannot be undone, because two or more points end up at the same location.

The invertible linear transformations GL(2,R) form a group: any succession of invertible linear transformations is an invertible linear transformation.  The identity in this group is the transformation that fixes all points.  Its matrix is

 1 0 0 1

It is simultaneously a skew transformation (n=0), a squeeze (t=1), a rotation (w=0), and a scaling (r=1).

The order in which linear operations are applied matters.  (Uniform scalings are the exception: you can apply a uniform scaling at any point in a sequence of transformations without changing the result.)  For any other kind of transformation, however, the results will usually be different.  For example, a squeeze of size t=2 followed by a 90 degree rotation will send the point (1,0) to the point (0,2), whereas a 90 degree rotation followed by the same squeeze sends (1,0) to (0, 1/2).

You can compute the effect of a series of linear transformations by multiplying their matrices right to left.  Our assertion above that every linear transformation is (after possibly switching x and y) the result of a skewing, squeezing, rotation, and scaling is easily proven this way.  Simply do the multiplication:
 |r 0| * |c -s| * |t 0| * |1 n| = |crt cnrt - sr/t| |0 r| |s c| |0 1/t| |0 1| |srt snrt + cr/t|

(where, for brevity, c = Cos(w) and s = Sin(w)).  If we now write the four coefficients crt = A, cnrt - sr/t = B, srt = C, and snrt + cr/t = D we can solve for w, n, r, and t.  Specifically, let's assume A*D - B*C is nonzero; the case A*D - B*C = 0 (for noninvertible transformations) can be treated specially.  The solution, which you can check with simple algebra, is:

1. If A*D - B*C < 0, first switch the coordinates.  This makes A*D - B*C > 0.  (This operation effectively switches the columns of the matrix.)  A*D - B*C is the determinant of the linear transformation.

2. Let F = 1/(A*A + C*C).  (This exists because if A*A + C*C = 0, then A = C = 0, implying A*D - B*C = 0, which is the special case we excluded above.)  Using the definitions of A and C in terms of c, s, r, and t, compute that F = 1/(r2t2).

3. n = (A*B + C*D) * F.

4. t = 1/Sqrt(F * (A*D - B*C))

5. r = Sqrt(A*D - B*C)

6. c = A/(r*t), s = C/(r*t).  From (2) it is evident that c2 + s2 = (A2 + C2) / (A2 + C2) = 1, so the rotation matrix (and therefore its angle w) is determined.

For example, you can check that the matrix A = 0, B = 1, C = 2, D = 1 corresponds to:
 A reflection about y=x (x --> y, y --> x), followed by a horizontal skew (y --> y + n*x) of size n=1, followed by a rescaling (x --> r*x, y --> r*y) of r=1.4142135624, followed by a rotation in the positive (counterclockwise) direction of 45 degrees.

No squeeze is listed because the squeeze in this case is the identity operation, which does nothing.  We might say that there is no squeeze component to this particular transformation.  Likewise, other transformations may omit one or more of their possible components.

## Applications

### Georeferencing images

Raster data sets are fundamentally arrays of numbers.  Their natural coordinates are the array indices.  These place the numbers at points of a unit square grid.  Georeferencing a raster data set therefore involves applying an affine transformation to move the grid points onto the desired data locations.  This is usually done by specifying the matrix coefficients in a "header" or "world" file.

We will continue to write the three-by-three transformation matrix as

 A B C D E F 0 0 1

representing the transformation

x --> A*x + B*y + C

y --> D*x + E*y + F

With this notation a standard "world file" for an image is an ASCII (text) file with six lines.  On each line is a number representing one of the coefficients.  They appear in the order  A, D, B, E, C, F (that is, arranged by columns, not rows, with the bottom row suppressed).  (Other conventions also exist for "world files," depending on the file format and the software, but they generally contain the same six matrix coefficients in some order.)

### Transforming features

Sometimes map features need transformation to adjust units of measurement, rotate a map, change the origin of a coordinate system, or remove a uniform distortion.  Frequently an affine transformation will provide sufficient accuracy.

Figuring out the transformation is amazingly easy.  Recall that, for a linear transformation, the first column of the matrix contains the coefficients of the point where the first basis vector (1, 0) is sent and the second column contains the coefficients of the point where the second basis vector (0, 1) is sent.  Evidently the third column contains the translation part of the affine transformation.

Therefore, simply write down where (1, 0), (0, 1), and (0, 0) must go.  Suppose these are the points (a,d), (b,e), and (C, F), respectively.  This implies that before everything is translated by (C,F), point (1,0) must go to (A, D) = (a-C, d-F) and point (0, 1) must go to (B, E) = (b-D, e-F).  Values A, B, C, D, E, F provide the three-by-three matrix coefficients.

For example, suppose you need to transform a set of features so that

1. Units of measurement are converted from meters to feet (that is, multiplied by 39.37/12),
2. then rotated 90 degrees clockwise,
3. then shifted by (500000, 150000) to a new origin.

Before the shift is applied, step (1) first converts basis vector (1, 0) to (39.37/12, 0) and then step (2) rotates that to (0, -39.37/12); similarly, basis vector (0, 1) is converted to (0, 39.37/12) and then rotated to (39.37/12, 0).  Therefore A=0, B = 39.37/12, C = 500000, D = -39.37/12, E = 0, and F = 1500000.

This example illustrates the power of the geometric interpretation of transformations and matrices: the entire transformation matrix was computed without doing a single matrix multiplication.

## The ArcView Transform2D Class

ArcView GIS software in versions 3.2 and later supports affine transformation of shapes through the Transform2D Class.  We have created a user interface to this class and posted it on the ArcScripts pages for free download.  The interface represents the Avenue requests rather than the natural geometric interpretation outlined above.

 This is the Transform 2D dialog.  The buttons correspond to Avenue "requests."  Requests are operations that potentially modify a transformation. The fill-in text lines in the top half of the dialog specify parameters for the buttons: scale factors, translation components, and rotation angle (in degrees counterclockwise).  Filling in the parameters does nothing; you have to press a button to apply the desired transformation to the matrix. The "Invert" and "Reset" buttons need no parameters.  "Invert" computes the affine transformation that undoes the current one.  "Reset" replaces the matrix by the identity (the transformation that keeps all points in place).

The checkboxes offer two independent reflections.  The "horizontal" reflection is the transformation x --> -x, y --> y and the "vertical" reflection is x --> x, y --> -y.  Again, no reflection is applied until the "Reflect" button is pressed.

The fill-in text lines in the bottom half of the dialog show the three-by-three affine transformation matrix.  Pressing the buttons applies the specified transformation to the current matrix, rather than replacing the matrix.  In this way you can build a transformation piece-by-piece by accumulating simpler transformations.

You can also specify a transformation by typing matrix coefficients directly.  This is necessary for skew transformations because ArcView does not provide a skew request.  Because the Transform2D class uses all nine coefficients of the matrix, nine coefficients are displayed.  You will discover, however, that ArcView almost ignores the bottom row of values: it does not do projective transformations.  (The exception concerns the lower right entry, the "I" coefficient.  ArcView's Transform2D object uses it to scale the translation!  For further information, you will have to experiment--there is no documentation.)

Actions causing a change in the matrix are followed by an audible confirmation (a beep).  Undesired changes are readily undone ("Undo").

The bottom third of the dialog provides some information about the current transformation matrix.  The "Scaling is uniform," "X Scale," and "Y Scale" values show the results of ArcView  requests.  However, these results do not correspond to the scale factors r*t and r/t described above.  It is unclear how ArcView calculates them.  They do not appear to have a consistent geometric meaning.

The "Info" button therefore computes the geometric components of the current matrix for you.

 Consider the previous example.  This figure shows the dialog after the scale, rotation, and move transformations are applied (in that order). Pressing the "Info" button produces this report: The transformation is: A rescaling (x --> r*x, y --> r*y) of r=3.28083333 Followed by a rotation in the positive (counterclockwise) direction of 90 degrees Followed by a translation (x --> x + e, y --> y + f) of (e,f) = (500000, 1500000). NB: This is just one of many possible geometric interpretations. This report is computed directly from the matrix coefficients.

Pressing "OK" to this dialog will apply the transformation shown by the matrix to all selected shapes in the active ArcView feature theme.

### Modification history:

20 September 2002: Minor amplifications, including pointing out which geometric properties--distance, angle, area, direction--are preserved by which types of transformations..

10 April 2002: Systematic, minor clarifications introduced throughout.  Minor changes in formatting.  Link to Taylor U. added.

30 November 2001: Minor corrections.

21 September 2000: Minor corrections.

8 June 2000: Some minor typographical errors were corrected.  Inadvertent transposition of "squeezes" and "scalings" in one sentence was rectified.

22 May 2000: First posted.