[Math] Using a 4D transformation matrix to scale a 3D object around a given point

matricestransformation

I have a 3D object – let's call it $B$ – and I want to scale it by a given amount – let's say $M$ – around a particular point (maybe the center, a corner, or maybe an arbitrary point) – let's say $P$ – using a 4×4 transformation matrix. (For example, if I want to double a certain sphere in size about its center, I specify $B$ to be a sphere, $P$ its center and $M$ to be 2).

My question is, what is this generalized 4×4 matrix?

[For reference, I'm writing a program that I need to use this in and can get a bounding box around $B$, the coordinates of the point $P$ and a double $M$.]

I'm pretty sure my matrix has to be something like:

$$ \left( \begin{array}{cccc}
M_{11} & M_{12} & M_{13} & 0 \\
M_{21} & M_{22} & M_{23} & 0 \\
M_{31} & M_{32} & M_{33} & 0 \\
0 & 0 & 0 & 1 \end{array} \right) $$

Best Answer

If you already implemented translation, then you can translate $P$, the center of your scaling, to $\underline{0}$, the center of your reference system. (The origin of the axis).

Then you apply a scaling centered in the origin: a scaling by a factor $k$ is represented by the matrix: $$\left[ \begin{matrix} k & 0 & 0 & 0 \\ 0 & k & 0 & 0 \\ 0 & 0 & k & 0 \\ 0 & 0 & 0 & 1 \end{matrix} \right] $$

And then you apply the opposite translation to what you did before, bringing $\underline{0}$ back to $P$.

If you want to use a single matrix transformation, then all you have to do is multiply the appropriate matrixes "by hand":

$$ \left[ \begin{matrix} 1 & 0 & 0 & P_1 \\ 0 & 1 & 0 & P_2 \\ 0 & 0 & 1 & P_3 \\ 0 & 0 & 0 & 1 \end{matrix} \right] \left[ \begin{matrix} k & 0 & 0 & 0 \\ 0 & k & 0 & 0 \\ 0 & 0 & k & 0 \\ 0 & 0 & 0 & 1 \end{matrix} \right] \left[ \begin{matrix} 1 & 0 & 0 & -P_1 \\ 0 & 1 & 0 & -P_2 \\ 0 & 0 & 1 & -P_3 \\ 0 & 0 & 0 & 1 \end{matrix} \right] = \left[ \begin{matrix} k & 0 & 0 & (1-k)P_1 \\ 0 & k & 0 & (1-k)P_2 \\ 0 & 0 & k & (1-k)P_3 \\ 0 & 0 & 0 & 1 \end{matrix} \right] $$