| | 74 | def deriveTransformation(a1x, a1y, a2x, a2y, b1x, b1y, b2x, b2y, c1x, c1y, c2x, c2y): |
|---|
| | 75 | """ Generates a transform based on three pairs of points, a1 -> a2, b1 -> b2, c1 -> c2. |
|---|
| | 76 | """ |
|---|
| | 77 | ax, bx, cx = linearSolution(a1x, a1y, a2x, b1x, b1y, b2x, c1x, c1y, c2x) |
|---|
| | 78 | ay, by, cy = linearSolution(a1x, a1y, a2y, b1x, b1y, b2y, c1x, c1y, c2y) |
|---|
| | 79 | |
|---|
| | 80 | return Transformation(ax, bx, cx, ay, by, cy) |
|---|
| | 81 | |
|---|
| | 82 | def linearSolution(r1, s1, t1, r2, s2, t2, r3, s3, t3): |
|---|
| | 83 | """ Solves a system of linear equations. |
|---|
| | 84 | |
|---|
| | 85 | t1 = (a * r1) + (b + s1) + c |
|---|
| | 86 | t2 = (a * r2) + (b + s2) + c |
|---|
| | 87 | t3 = (a * r3) + (b + s3) + c |
|---|
| | 88 | |
|---|
| | 89 | r1 - t3 are the known values. |
|---|
| | 90 | a, b, c are the unknowns to be solved. |
|---|
| | 91 | returns the a, b, c coefficients. |
|---|
| | 92 | """ |
|---|
| | 93 | |
|---|
| | 94 | # make them all floats |
|---|
| | 95 | r1, s1, t1, r2, s2, t2, r3, s3, t3 = map(float, (r1, s1, t1, r2, s2, t2, r3, s3, t3)) |
|---|
| | 96 | |
|---|
| | 97 | a = (((t2 - t3) * (s1 - s2)) - ((t1 - t2) * (s2 - s3))) \ |
|---|
| | 98 | / (((r2 - r3) * (s1 - s2)) - ((r1 - r2) * (s2 - s3))) |
|---|
| | 99 | |
|---|
| | 100 | b = (((t2 - t3) * (r1 - r2)) - ((t1 - t2) * (r2 - r3))) \ |
|---|
| | 101 | / (((s2 - s3) * (r1 - r2)) - ((s1 - s2) * (r2 - r3))) |
|---|
| | 102 | |
|---|
| | 103 | c = t1 - (r1 * a) - (s1 * b) |
|---|
| | 104 | |
|---|
| | 105 | return a, b, c |
|---|
| | 106 | |
|---|