javaawtaffinetransform

Generate AffineTransform from 3 points


Given 3 points (x and y coordinates) in coordinate system A, and 3 corresponding points in coordinate system B, how can I derive an AffineTransform that will convert from A to B.

My question is similar to Create transform to map from one rectangle to another?, except that question only deals with 2 points - i.e., it assumes there is no rotation.


Solution

  • Suppose your transform is of the form

    x' = px + qy + r
    y' = sx + ty + u
    

    and write your six points as (A1x, A1y), (A2x, A2y), (A3x, A3y), (B1x, B1y), (B2x, B2y), (B3x, B3y). Expressing this in matrix form gives

    /               \       /           \   /               \
    | B1x  B2x  B3x |       | p   q   r |   | A1x  A2x  A3x |
    |               |   =   |           |   |               |
    | B1y  B2y  B3y |       | s   t   u |   | A1y  A2y  A3y |
    \               /       \           /   |               |
                                            |  1    1    1  |
                                            \               /
    

    Now find the inverse of the 3x3 matrix on the right. You'll find plenty of algorithms online telling you how to do this. There's one at http://www.econ.umn.edu/undergrad/math/An%20Algorithm%20for%20Finding%20the%20Inverse.pdf, for example.

    Post-multiply both sides of the equation above by the inverse of the 3x3 matrix, to get the values of p, q, r, s, t, u, v.