Memo for myself: how to calculate the translation necessary to keep a given location fixed while zooming on an image.
Given \(\vec{d}\) the display coordinates, \(\vec{i}\) the image coordinates, \(\vec{t}\) the translation, and \(s\) the scaling factor, the conversion from image coordinates to display coordinates can be expressed as \(\vec{d}=s(\vec{i}+\vec{t})\).
Given \(s'=cs\) the new scaling factor, and \(\vec{p}\) the fixed point in the display (i.e. the location in the display displaying the same location in the image with the old and new scaling factor). We are looking for the new translation factor \(\vec{t'}\) such as \(s(\vec{i}+\vec{t})=\vec{p}=s'(\vec{i}+\vec{t'})\), or \(\vec{p}/s-\vec{t}=\vec{p}/s'-\vec{t'}\). \(t'\) can be calculated as follow:
$$ \begin{array}{rcl} \frac{\vec{p}}{s}-\vec{t}&=&\frac{\vec{p}}{s'}-\vec{t'}\\ \vec{t'}&=&\vec{t}+\frac{\vec{p}}{s'}-\frac{\vec{p}}{s}\\ \vec{t'}&=&\vec{t}+\frac{s\vec{p}-s'\vec{p}}{ss'}\\ \vec{t'}&=&\vec{t}+\frac{s\vec{p}-cs\vec{p}}{cs^2}\\ \vec{t'}&=&\vec{t}+\frac{(1-c)s}{cs^2}\vec{p}\\ \vec{t'}&=&\vec{t}+\frac{(1-c)}{cs}\vec{p}\\ \end{array} $$
Example: using \(s=2\), \(\vec{t}=(-1,-2)\), \(\vec{p}=(3,4)\), we have \(\vec{i}=(3/2+1,4/2+2)=(2.5, 4)\). Changing the scale by a factor of \(c=1.1\) gives the new translation vector
$$ \vec{t'}=(-1,-2)+\frac{1-1.1}{1.1*2}(3,4)=(-1,-2)-\frac{(3,4)}{22}=(-\frac{25}{22},-\frac{48}{22}) $$
which projects the fixed point in the display at the same coordinates in the image as before changing scale as expected:
$$ \vec{i}=(\frac{3}{2.2}+\frac{25}{22},\frac{4}{2.2}+\frac{48}{22})=(\frac{55}{22},\frac{88}{22})=(2.5,4) $$