# Finding a surface normal of implicit surface f(x,y,z)

Let's say that I have some function $f(x,y,z)$ representing an implicit surface.

For instance it might be the equation of a sphere like below, but could also be many other types of functions / shapes.

$f(x,y,z) = (x^2+y^2+z^2)^{0.5} - 0.5 = 0$

If I have a point that I know to be on the surface, what is the correct way to get a vector that is perpendicular to the surface (aka the surface normal)?

I've tried normalizing the vector made up of partial derivatives of x, y and z, but that seems to be failing for some situations.

It's difficult to provide specifics of what is failing specifically, but I'm curious, is that the correct way? or is it a problem that I really have an equation like the below with an implicit variable $w$ that I'm not taking into account in the gradient calculation?

$w=f(x,y,z)$

• Seems to be failing in what sense?
– Jan
Feb 2, 2017 at 0:09
• I'm using the normal for shading and it's very obviously wrong, but I did verify that it was normalized. Feb 2, 2017 at 0:12

The unit surface normal of the implicit surface $f(x,y,z)=0$ is indeed the normalized gradient of $f$. You were also right that the unit surface normal of $z=g(x,y)$ is the normalized vector $(-g_x \ -g_y \ 1)$ (the cross product of $\mathbf{x}_u$ and $\mathbf{x}_v$, with $\mathbf{x} = (x \ y \ z)$ and $x=u$ and $y=v$).

Here is a quick proof that the unit surface normal is the normalized gradient. The function $f(x,y, g(x,y))$ is a composition of the mapping $f$ from $\mathbb{R}^3$ to $\mathbb{R}$ and the function $h$ from $\mathbb{R}^2$ to $\mathbb{R}^3$ with $h(x,y)= (x,y,g(x,y))$. The total derivative of this function using the chain rule is $$D(f\circ h) = (Df)(Dh) = (f_x + f_zg_x \ f_y+f_zg_y).$$ Setting this derivative to zero (since $f\circ h(x,y) = 0$ is the definition of the surface) yields $g_x = -f_x/f_z$ and $g_y = -f_y/f_z$. Substituting these values of $g_x$ and $g_y$ into $(-g_x \ -g_y \ 1)$ gives $(f_x \ f_y \ f_z) / f_z,$ a multiple of the gradient.

You must have computed the partial derivatives of $f$ incorrectly, because the unit surface normal of an implicit surface $f(x,y,z)=0$ definitely is the normalized gradient of $f.$

• Thank you so much, I think you are right about where I went wrong. Feb 2, 2017 at 4:07

The gradient of $f$ along any vector tangent to the surface must be zero, so that $f$ is constant along the surface. Solving $\vec{v} \cdot \nabla f(x,y,z) = 0$ for two linearly independent vectors $\vec{v}$ span the tangent surface. You can then construct the normal vector using a cross product ($\vec{v}_1 \times \vec{v}_2$).

• In the case of a function $z=f(x,y)$, i've gotten the gradient by normalizing the vector defined by partial derivative of x, partial derivative of y, and -1. I believe that is correct (it is, right?), so was trying to extend it to $f(x,y,z)$. Feb 2, 2017 at 0:32
• @user34722 I believe that the gradient is always normal to level curves. Feb 2, 2017 at 3:09
• Yes, you are correct, as the commenter below points out. I removed the incorrect comment. Feb 2, 2017 at 3:44

The change of $z$ with respect to moving a unit in the $x$ direction $z_x$. This corresponds vector $\langle 1,0,z_x \rangle$. As for the change in $f$ with respect to moving in the $y$ direction $1$ unit, this is $z_y$. This corresponds to $\langle 0,1,z_y \rangle$.

These two vectors are obviously tangent to the surface and linearly independent. Then taking their cross product, $\langle 1,0,z_x \rangle \times \langle 0,1,z_y \rangle$ should give you (a) normal. Implicit differentiation should work for finding the partials.

But I suppose that exploiting the fact that gradients are parallel to level curves are easier so that a normal to your surface is $\nabla f$.

Then normalizing what you get should give a correct answer.