A two-point transformation is a 2D linear transformation determined by specifying where two input points should go. Its matrix has four unknown entries, matching the four coordinate equations supplied by two points. section 4.2.1
The number of point constraints matters:
- One point gives two equations for four matrix entries, leaving a continuum of solutions. section 4.2.1
- Two points give exactly four equations, allowing the matrix to be solved symbolically. section 4.2.1
- Three points normally overspecify the four-entry matrix and may produce no solution. section 4.2.1
Solve the transformation symbolically
Represent the source coordinates as (x1, y1) and (x2, y2), and their destinations as (xt1, yt1) and (xt2, yt2). Solving the four resulting equations for a11, a12, a21, and a22 produces the transformation matrix. section 4.2.1
Listing 4.1 Computing the matrix for the linear transformation symbolically
from sympy import *
a11, a12, a21, a22 = symbols('a11 a12 a21 a22') #1
x1, y1, x2, y2 = symbols('x1 y1 x2 y2') #2
xt1, yt1, xt2, yt2 = symbols('xt1 yt1 xt2 yt2') #3
solution = solve([
a11 * x1 + a12 * y1 - xt1, #4
a21 * x1 + a22 * y1 - yt1,
a11 * x2 + a12 * y2 - xt2,
a21 * x2 + a22 * y2 - yt2
], (a11, a12, a21, a22))
print(solution)
Inspect the executable SymPy listing that constructs and solves the four-equation system. section 4.2.1
