# Move centerpoint of sphere along an axis such that it contacts a plane at a point

I am working in Cartesian coordinates, and have a known plane P in the form Ax + By + Cz = D and a sphere S in the form (x-x0)^2 + (y-y0)^2 + (z - z0)^2 = R^2. I have successfully solved for the circle of intersection between them using the method found here: Determine Circle of Intersection of Plane and Sphere

Assuming the plane and the radius of the sphere do not change, but the y-coordinate of the sphere's center can, I need to solve for the point of intersection between the plane and the sphere when they become a point of tangency instead of an intersecting circle.

That is to say, if the sphere can slide along the y-axis (the x and z coordinate of the center do not change) until it just touches the plane at a point, what is the y-coordinate of the sphere's center, and what is the location of the point where the sphere and the plane meet?

Thank you for any assistance you can provide.

YOu've got a radius $R$ sphere at location $P + t e_2$, where $P = (x_0, y_0, z_0)$, and $e_2 = (0, 1, 0)$, and you want to know "how far to move $P$" to make there sphere tangent to the plane $Ax + By + Cz + D = 0$, i.e., you want to find the value of $t$ that'll make the two tangent.

Just to be clear: there are two answers. Suppose you have a basketball and a thin glass tabletop. You can lower the basketball onto the tabletop, or you can raise it from below until it's tangent to the tabletop ... so two different displacements work.

Now for a solution.

The distance from the sphere-center to your plane is

$$\frac{A\cdot x_0 + B \cdot (y_0 + t) + D \cdot z_0 + D}{\sqrt{A^2 + B^2 + C^2}}$$ You want that to be $R$. So you want \begin{align} R &= \frac{A\cdot x_0 + B \cdot (y_0 + t) + C \cdot z_0 + D}{\sqrt{A^2 + B^2 + C^2}}\\ R\sqrt{A^2 + B^2 + C^2} &= A\cdot x_0 + B \cdot (y_0 + t) + C \cdot z_0 + D\\ R^2\cdot (A^2 + B^2 + C^2) &= (A\cdot x_0 + B \cdot (y_0 + t) +C\cdot z_0 + D)^2\\ \end{align} Calling the stuff on the left hand side $E$, we want to solve \begin{align} E &= (A\cdot x_0 + B \cdot (y_0 + t) + c \cdot z_0 + D)^2\\ \end{align} for $t$. Letting $U = Ax_0+ By_0+ Cz_0 + D$, this becomes \begin{align} E &= ( U + B t)^2\\ \pm\sqrt{E} &= U + B t\\ -U \pm\sqrt{E} &= B t\\ \frac{-U \pm\sqrt{E}}{B} &= t\\ \end{align}

And for either of those two $t$ values, moving the circle center by distance $t$ along the $y$-axis will cause the circle and your plane to be tangent.

Note that if by some change, $B = 0$, then there's no solution; geometrically, it's clear why: In this case, the plane is vertical, and no amount of up/down movement will make the sphere get closer to or farther from it.

Since you’ve already found the center of the circle of intersection, you can work out this distance using similar triangles.

Let $P_S$ be the center of the sphere, $P_C\ne P_S$ the center of this circle and $Q$ the intersection of the plane and the line through $P_S$ parallel to the $y$-axis. If $P_S'$ is a point along this line such that a sphere of the same radius centered there is tangent to the plane, and $P_C'$ the point of tangency, then $\triangle{P_SQP_C}\sim\triangle{P_S'QP_C'}$, so $P_S'Q : P_SQ :: P_S'P_C' : P_SP_C$. Setting $\lambda$ equal to this ratio, we find that $P_S'=(1-\lambda)Q+\lambda P_S$. Note that there are in fact two possible solutions: we get another one by using $-\lambda$ in the above formula. You can view the first solution as pushing the sphere away from the plane until it just touches and the second as pushing it through the plane out to the other side. If the plane is parallel to the $y$-axis, then there’s no intersection point $Q$ and also no solutions (if the sphere isn’t already tangent, that is); no amount of sliding will change the distance between $P_S$ and the plane.

Now, you can compute the distance $P_SP_C$ using your previously-computed circle of intersection, but it is also simply the distance between $P_S$ and the plane, namely, $$P_SP_C = {|Ax_0+By_0+Cz_0-D| \over \sqrt{A^2+B^2+C^2}}.$$ $P_S'P_C'$, on the other hand, is just the radius $R$ of the sphere. Therefore, $$\lambda = \pm{R\sqrt{A^2+B^2+C^2} \over Ax_0+By_0+Cz_0-D},$$ with $\lambda\ge0$ keeping the sphere’s center on the same side of the plane. All that’s left is to find $Q$, but that’s easily done using standard methods for intersecting flats in $\mathbb R^3$. For example, in homogeneous coordinates a formula for the intersection of a line through points $\mathbf p$ and $\mathbf q$ and the plane $\mathbf\pi$ is $\mathbf p\mathbf q^T\mathbf\pi-\mathbf q\mathbf p^T\mathbf\pi.$ For this problem, $\mathbf\pi=[A:B:C:-D]$, and we can take $\mathbf p=[x_0:y_0:z_0:1]$ (i.e., $P_S$) and $\mathbf q=[0:1:0:0]$ (the point at infinity on the $y$-axis), yielding $[Bx_0:D-Ax_0-Cz_0:Bz_0:B]$, or in Cartesian coordinates, $$Q=\left(x_0,\frac1B(D-Ax_0-Cz_0),z_0\right)$$ and finally \begin{align} P_S' &= \left(x_0, {1-\lambda\over B}(D-Ax_0-Cz_0)+\lambda y_0, z_0 \right) \\ &= \left(x_0, \frac1B\left(D-Ax_0-Cz_0\pm R\sqrt{A^2+B^2+C^2}\right), z_0\right). \end{align} If $B=0$, then the plane is parallel to the $y$-axis, in which case there’s no solution if the circle isn’t already tangent to the plane, as noted above.

• "All that’s left is to find Q, but that’s easily done using standard methods for intersecting flats" is part of your answer for someone who's username is "Forgots All My Maths"? You're certainly optimistic. I admire that. :) – John Hughes Oct 17 '17 at 1:27
• @JohnHughes Hope springs eternal. – amd Oct 17 '17 at 1:29
• Thank you both. I am working on a project whose geometry is just beyond my capability to figure out mathematically, but with the help of folks like you who can break it down into simple algebra/vector math, I have been able to proceed. In the process, I am gradually "remembering all my maths" (it's been a long time since I had to do cross-products, etc), and learning more about geometric reasoning, too. – Forgots All My Maths Oct 18 '17 at 16:47