# Finding reflection transformation matrix

I have two 3 dimensional points. $A [x_1, y_1, z_1]$ and $B [x_2, y_2, z_2]$. I need to find a transformation matrix which when multiplied to $A$ will give me $B$ and when multiplied by $B$ give me $A$. The transformation needs to be a reflection against the plane that's perpendicular to the middle of the $AB$ segment and passing through the midpoint of the $AB$.

Sorry if I am not using the right terminology, I studied math 20 years ago so it's a little rusty.

Practical application of the question is to find a color matrix to swap two colors with arbitrary Red, Green and Blue components.

Thank you.

-
You can't do this in general: any $3\times3$ matrix sends the zero vector $(0,0,0)$ to itself, whereas the reflection of the point $(0,0,0)$ in the plane between two arbitrary points need not be $(0,0,0)$. – Donkey_2009 Jul 31 '13 at 21:47
What you need is an affine transformation. One way to do it is to (i) Move the midpoint to $0$ via a translation, and apply that translation to your given points; (ii) Find the matrix that does the reflection job on the shifted points; (iii) Translate back. – André Nicolas Jul 31 '13 at 21:59

If you can find the reflection through the plane perpendicular to the middle of the $AB$ segment, then it automatically sends $A$ to $B$ and $B$ to $A$.

How do we do this reflection? First, suppose that the midpoint of $A$ and $B$ is the origin: $A=-B$. Now, note the mathematical concept of a projection:

Here, $u,v,w,x$ are being multiplied by a projection matrix $P$: one that sends them to their closest point on the line $m$. It turns out that the projection of a vector Now suppose that $m$ is the line from $A$ to the origin.

If we subtract the projection of the line $x$ on to $OA$ from $x$ twice, then we get the reflection of $x$ in the plane through $O$ perpendicular to $OA$. Now, if $a$ is the vector of the point $a$, then the projection of the vector $x$ on to the line $a$ is given by $(x.a)x$ (do you know how to work out the dot product?) If you know properties of the dot product, you can work this out for yourself. So the transformation that reflects $x$ in this plane is given by:

$$x\mapsto x-2(x.a)x$$

This is a linear transformation, so it has a matrix, if you want to work it out: to work out the matrix, substitute in $(1,0,0)$, $(0,1,0)$ and $(0,0,1)$ for $x$, and the vectors you get out will be the columns of the matrix.

What if $A\neq-B$? Then, as I said in my comment above, there is no matrix that will encode the reflection, as any matrix must send the origin to itself. So we have to use a trick called conjugation. First, find the midpoint $c=\frac12(a+b)$, and then let $a'=a-c$ and $b'=b-c$, and for any point $x$, let $x'=x-c$. So we are 'changing our world view' in some sense: we are taking the point $c$ and mapping it to the origin. Then $a'+b'=0$, so we can encode the reflection in the plane between them as a matrix $P'$.

Then, if $x$ is an arbitrary vector, the reflection of $x'$ in the plane between $a'$ and $c'$ is given by $P'x'=P'(x-c)$. If we now add $c$ to everything, we see that the reflection of $x$ in the plane between $a$ and $b$ is given by $P'(x-c)+c$, which we can rearrange to $P'x+d$, where $d=c-P'c$. So the transformation isn't given by a matrix, but it is given by a matrix multiplication, followed by adding a constant vector.

The process of carrying out an operation (subtracting $c$), then carrying out another operation (the reflection) and then doing the inverse of the first operation (adding $c$ back again) is known as conjugation, and is very important in mathematics. Think of it as a temporary change of 'world view': we pretend that $c$ is the origin so we can do our reflection, and then we map everything back again at the end.

-

I actually figured it out.

Midpoint $C [x_c, y_c, z_c] = [ (x_1+x_2)/2, (y_1+y_2)/2, (z_1+z_2)/2 ]$.

Vector $AB [x_{ab}, y_{ab}, z_{ab}] = [ x_2-x_1, y_2-y_1, z_2-z_1 ]$.

Now, to find plane perpendicular to the vector intersecting the midpoint. Plane has canonical form $A_p*x + B_p*y + C_p*z + D_p = 0$ where $A_p, B_p, C_p$ describe the vector so they are equal to $x_{ab}, y_{ab}, z_{ab}$ and $D_p$ is $-x_{ab}*x_c-y_{ab}*y_c-z_{ab}*z_c$.

The reflection matrix is \begin{pmatrix} -2*A_p*A_p+1& -2*B_p*A_p& -2*C_p*A_p& 0 \\ -2*A_p*B_p& -2*B_p*B_p+1& -2*C_p*B_p& 0 \\ -2*A_p*C_p& -2*B_p*C_p& -2*C_p*C_p+1& 0 \\ -2*A_p*D_p& -2*B_p*D_p& -2*C_p*D_p& 1 \\ \end{pmatrix}

I realize it doesn't solve the impossible cases, but it works for me. Thanks.

-