How to define an affine transformation using 2 triangles?

The transformation you're looking for has this form:

$$\left[ \begin{array}{ccc} t_1 & t_2 & t_3\\ t_4 & t_5 & t_6\\ 0 & 0 & 1\\ \end{array} \right] \left[ \begin{array}{ccc} a_1 & b_1 & c_1\\ a_2 & b_2 & c_2 \\ 1 & 1 & 1 \\ \end{array} \right] = \left[\begin{array}{ccc} x_1 & y_1 & z_1\\ x_2 & y_2 & z_2\\ 1 & 1 & 1 \end{array} \right] $$

or

$${\bf T A} = {\bf X}$$ so $${\bf T} = {\bf X}{\bf A}^{-1}$$

Now $\bf T$ is a transformation matrix you can use on any point, like $${\bf T}\left[\begin{array}{c}a_1\\a_2\\1\end{array}\right] = \left[\begin{array}{c}x_1\\x_2\\1\end{array}\right]$$

It's linear, so it has the halfway point property you were looking for.

$$ {\bf A} = \left[\begin{array}{ccc} -3 & 0 & 3\\ 0 & 3 & 0\\ 1 & 1 & 1\\ \end{array}\right] $$ $$ \left[\begin{array}{ccc|ccc} -3 & 0 & 3 & 1 & 0 & 0\\ 0 & 3 & 0 & 0 & 1 & 0\\ 1 & 1 & 1 & 0 & 0 & 1\\ \end{array}\right] $$ $$ \left[\begin{array}{ccc|ccc} 1 & 0 & -1 & -\frac13 & 0 & 0\\ 0 & 3 & 0 & 0 & 1 & 0\\ 0 & 1 & 2 & \frac13 & 0 & 1\\ \end{array}\right] $$

$$ \left[\begin{array}{ccc|ccc} 1 & 0 & -1 & -\frac13 & 0 & 0\\ 0 & 1 & 0 & 0 & \frac13 & 0\\ 0 & 0 & 2 & \frac13 & -\frac13 & 1\\ \end{array}\right] $$

$$ \left[\begin{array}{ccc|ccc} 1 & 0 & 0 & -\frac16 & -\frac16 & \frac12\\ 0 & 1 & 0 & 0 & \frac13 & 0\\ 0 & 0 & 1 & \frac16 & -\frac16 & \frac12\\ \end{array}\right]\\ \text{so } {\bf A}^{-1} = \left[\begin{array}{ccc} -\frac16 & -\frac16 & \frac12\\ 0 & \frac13 & 0\\ \frac16 & -\frac16 & \frac12\\ \end{array}\right]$$

$${\bf XA}^{-1} = \left[\begin{array}{ccc} 2 & 3 & 4\\ 3 & 2 & 3\\ 1 & 1 & 1\\ \end{array}\right] \left[\begin{array}{ccc} -\frac16 & -\frac16 & \frac12\\ 0 & \frac13 & 0\\ \frac16 & -\frac16 & \frac12\\ \end{array}\right] = \left[\begin{array}{ccc} \frac13 & 0 & 3\\ 0 & -\frac13 & 3\\ 0 & 0 & 1\\ \end{array}\right] $$


The form of the affine transformation will be

$$\vec{p}' = \left( \begin{array}{cc} A_{11} & A_{12} \\ A_{21} & A_{22}\end{array} \right) \vec{p} + \vec{d}$$.

This transformation is characterized by six parameters (four for the matrix, two for the displacement vector), and the requirement that $a$ transforms to $x$ and similaryly for $b$ and $c$ going to $y$ and $z$ gives six equations. Assuming no three of the points are co-linear, these can be solved. An easy approach is:

Eliminate the $\vec{d}$ by transforming the difference $(\vec{b}-\vec{a})$; this gives two equations, the first of which looks like $$ A_{11} (b_1 - a_1) + A_{12} (b_2 - a_2) = y_1 - x_1 $$ Then eliminate the $\vec{d}$ agian, this time by transforming the difference $(\vec{c}-\vec{a})$; this gives two equations, the first of which looks like $$ A_{11} (c_1 - a_1) + A_{12} (c_2 - a_2) = z_1 - x_1 $$ These two equations can be solved to determine $A_{11}$ and $A_{12}$. Similarly, the second equation in each elemination can be solved to determine $A_{21}$ and $A_{22}$.

Finally, now that $A$ is known, you can easily find $\vec{d}$.