TExcelTAD
Home › Calculus Essentials › Surfaces in 3D

Surfaces in 3D

A curve has one degree of freedom, a surface has two. This lesson describes surfaces in three ways: as graphs z=f(x,y)z = f(x, y), as level sets F(x,y,z)=cF(x, y, z) = c, and as parametric patches r(u,v)\mathbf{r}(u, v). It also shows how to find the normal vector that lighting and collision code need.

Three ways to describe a surface

Description Example Good for
Graph z=f(x,y)z = f(x, y) z=x2+y2z = x^2 + y^2 (a bowl) terrain heightmaps, loss surfaces
Level set F(x,y,z)=cF(x, y, z) = c x2+y2+z2=4x^2 + y^2 + z^2 = 4 (a sphere) inside/outside tests, signed distance fields
Parametric r(u,v)\mathbf{r}(u, v) ⟨cos⁡usin⁡v,sin⁡usin⁡v,cos⁡v⟩\langle \cos u \sin v, \sin u \sin v, \cos v \rangle generating meshes, texture coordinates

Each has a use. A level set answers "is this point inside?" in one evaluation (F<cF < c or F>cF > c). A parametric form generates points for a mesh by looping over a grid of (u,v)(u, v). A graph is the special case r(u,v)=⟨u,v,f(u,v)⟩\mathbf{r}(u, v) = \langle u, v, f(u, v) \rangle.

Planes

The simplest surface is a plane. Pick a point P0P_0 on it and a normal vector n\mathbf{n} perpendicular to it. A point PP lies on the plane exactly when P−P0P - P_0 is perpendicular to n\mathbf{n}:

n⋅(P−P0)=0  ⟺  ax+by+cz=d,with n=⟨a,b,c⟩, d=n⋅P0\mathbf{n} \cdot (P - P_0) = 0 \quad\iff\quad ax + by + cz = d, \qquad \text{with } \mathbf{n} = \langle a, b, c \rangle,\ d = \mathbf{n} \cdot P_0

The plane through three points AA, BB, CC has normal n=(B−A)×(C−A)\mathbf{n} = (B - A) \times (C - A), from the cross product. The signed distance from a point QQ to the plane is (n⋅Q−d)/∥n∥(\mathbf{n} \cdot Q - d)/\lVert \mathbf{n} \rVert. Ray-plane intersection in a renderer uses the same formula; see Ray-Plane.

python · try it yourself
import numpy as np

A, B, C = np.array([1.0, 0.0, 0.0]), np.array([0.0, 2.0, 0.0]), np.array([0.0, 0.0, 3.0])
n = np.cross(B - A, C - A)
d = np.dot(n, A)
print(f"plane: {n[0]:g}x + {n[1]:g}y + {n[2]:g}z = {d:g}")

for Q in [np.array([0.0, 0.0, 0.0]), np.array([1.0, 1.0, 1.0]), B]:
    print(Q, "signed distance", round((np.dot(n, Q) - d) / np.linalg.norm(n), 4))
plane: 6x + 3y + 2z = 6
[0. 0. 0.] signed distance -0.8571
[1. 1. 1.] signed distance 0.7143
[0. 2. 0.] signed distance 0.0

The origin and (1, 1, 1) lie on opposite sides of the plane, which shows up as opposite signs.

Quadric surfaces

Second-degree equations in xx, yy, zz give the quadric surfaces, the 3D cousins of the conic sections:

Equation Surface Cross-sections
x2+y2+z2=R2x^2 + y^2 + z^2 = R^2 sphere circles
x2a2+y2b2+z2c2=1\dfrac{x^2}{a^2} + \dfrac{y^2}{b^2} + \dfrac{z^2}{c^2} = 1 ellipsoid ellipses
z=x2+y2z = x^2 + y^2 (circular) paraboloid, a bowl circles (z=cz = c), parabolas (x=cx = c)
z=x2−y2z = x^2 - y^2 hyperbolic paraboloid, a saddle hyperbolas (z=cz = c), parabolas
x2+y2=R2x^2 + y^2 = R^2 cylinder the same circle at every height
z2=x2+y2z^2 = x^2 + y^2 double cone circles, lines

To identify a surface, slice it: set z=constantz = \text{constant} and look at the curve that remains, then try x=constantx = \text{constant}. The saddle is worth knowing in particular: along the x-direction it curves up, along the y-direction it curves down, and its centre is a saddle point that is neither a maximum nor a minimum. Loss surfaces in machine learning are full of them.

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-2, 2, 41)
X, Y = np.meshgrid(x, x)
Z = X**2 - Y**2

fig = plt.figure(figsize=(6, 4))
ax = fig.add_subplot(111, projection="3d")
ax.plot_surface(X, Y, Z, cmap="viridis", linewidth=0, antialiased=True)
ax.set_xlabel("x"); ax.set_ylabel("y"); ax.set_zlabel("z")
ax.set_title("Saddle z = x² − y²")
print("z along the x-axis:", Z[20, ::10])
print("z along the y-axis:", Z[::10, 20])
z along the x-axis: [4. 1. 0. 1. 4.]
z along the y-axis: [-4. -1.  0. -1. -4.]

Parametric surfaces and their normals

A parametric surface r(u,v)\mathbf{r}(u, v) has two families of curves on it: fix vv and vary uu, or fix uu and vary vv. Their tangent vectors are the partial derivatives ru=∂r/∂u\mathbf{r}_u = \partial\mathbf{r}/\partial u and rv=∂r/∂v\mathbf{r}_v = \partial\mathbf{r}/\partial v (treated properly in Partial Derivatives). Both lie in the tangent plane, so their cross product is a normal:

n=ru×rv\mathbf{n} = \mathbf{r}_u \times \mathbf{r}_v

For a sphere of radius RR with r(u,v)=⟨Rcos⁡usin⁡v,Rsin⁡usin⁡v,Rcos⁡v⟩\mathbf{r}(u, v) = \langle R\cos u \sin v, R\sin u \sin v, R\cos v \rangle (u=longitudeu = \text{longitude}, v=angle from the north polev = \text{angle from the north pole}), the normal at each point is parallel to the position vector itself, as you would expect for a sphere, and ∥ru×rv∥=R2sin⁡v\lVert \mathbf{r}_u \times \mathbf{r}_v \rVert = R^2 \sin v is the area scale factor used later in surface integrals.

python · try it yourself
import sympy as sp

u, v, R = sp.symbols('u v R', positive=True)
r = sp.Matrix([R*sp.cos(u)*sp.sin(v), R*sp.sin(u)*sp.sin(v), R*sp.cos(v)])

r_u, r_v = r.diff(u), r.diff(v)
n = r_u.cross(r_v)
print("n =", [sp.simplify(c) for c in n])
print("n parallel to r? ", sp.simplify(n + R*sp.sin(v)*r) == sp.zeros(3, 1))
print("|n| =", sp.simplify(sp.sqrt(n.dot(n))))
n = [-R**2*sin(v)**2*cos(u), -R**2*sin(u)*sin(v)**2, -R**2*sin(2*v)/2]
n parallel to r?  True
|n| = R**2*Abs(sin(v))

SymPy writes the last component with sin⁡2v=2sin⁡vcos⁡v\sin 2v = 2\sin v \cos v and keeps an Abs because it does not know that 0≤v≤π0 \le v \le \pi, where sin⁡v≥0\sin v \ge 0. Here n=−Rsin⁡v⋅r\mathbf{n} = -R\sin v \cdot \mathbf{r}, so this ordering of uu and vv gives an inward normal; swap the factors (rv×ru\mathbf{r}_v \times \mathbf{r}_u) to point outward. Mesh code has the same issue with triangle winding.

In software

  • Terrain. A heightmap is z=f(x,y)z = f(x, y) sampled on a grid, often generated with noise (Procedural Maps). Its normal is ⟨−fx,−fy,1⟩\langle -f_x, -f_y, 1 \rangle, estimated from neighbouring heights; lighting uses it.
  • Signed distance fields. Shaders and font renderers describe shapes as level sets F(p)=0F(\mathbf{p}) = 0, where FF is the distance to the surface, negative inside.
  • Meshes. Spheres, tori and cylinders in 3D engines are produced by looping over a parametric (u,v)(u, v) grid; the same (u,v)(u, v) become texture coordinates.

Exercises

Exercise 1: Plane through a point

Find the equation of the plane through (2,−1,3)(2, -1, 3) with normal ⟨1,4,−2⟩\langle 1, 4, -2 \rangle.

Answer

x+4y−2z=−8x + 4y - 2z = -8.

Solution
d=n⋅P0=1⋅2+4⋅(−1)+(−2)⋅3=2−4−6=−8plane: x+4y−2z=−8\begin{aligned} d &= \mathbf{n} \cdot P_0 = 1 \cdot 2 + 4 \cdot (-1) + (-2) \cdot 3 = 2 - 4 - 6 = -8 \\ \text{plane: } & x + 4y - 2z = -8 \end{aligned}

Exercise 2: Name the surface

Identify each surface: (a) z=2x2+2y2z = 2x^2 + 2y^2, (b) x2+y2−z2=0x^2 + y^2 - z^2 = 0, (c) x2/9+y2/4+z2=1x^2/9 + y^2/4 + z^2 = 1.

Answer

(a) circular paraboloid (bowl), (b) double cone, (c) ellipsoid.

Solution

(a) Slices z=c>0z = c > 0 are circles x2+y2=c/2x^2 + y^2 = c/2 and slices x=0x = 0 are parabolas z=2y2z = 2y^2: a paraboloid. (b) Rewrite as z2=x2+y2z^2 = x^2 + y^2; slices z=cz = c are circles of radius ∣c∣\lvert c \rvert, which grow linearly, and the slice x=0x = 0 is the pair of lines z=±yz = \pm y: a cone. (c) Every slice is an ellipse and the surface is bounded (∣x∣≤3\lvert x \rvert \le 3, ∣y∣≤2\lvert y \rvert \le 2, ∣z∣≤1\lvert z \rvert \le 1): an ellipsoid.

Exercise 3: Inside or outside?

For the ellipsoid of Exercise 2(c), which of (2.5, 1, 0.5) and (1, 1, 0.5) are inside?

Answer

(1, 1, 0.5) is inside; (2.5, 1, 0.5) is outside.

Solution

Evaluate F(x,y,z)=x2/9+y2/4+z2F(x, y, z) = x^2/9 + y^2/4 + z^2 and compare with 1:

F(2.5,1,0.5)=6.25/9+1/4+1/4≈0.694+0.5=1.194>1outsideF(1,1,0.5)=1/9+1/4+1/4≈0.111+0.5=0.611<1inside\begin{aligned} F(2.5, 1, 0.5) &= 6.25/9 + 1/4 + 1/4 \approx 0.694 + 0.5 = 1.194 > 1 && \text{outside} \\ F(1, 1, 0.5) &= 1/9 + 1/4 + 1/4 \approx 0.111 + 0.5 = 0.611 < 1 && \text{inside} \end{aligned}

Exercise 4: Terrain normal (code)

A terrain is z=0.1x2+0.05xyz = 0.1x^2 + 0.05xy. Compute its unit upward normal at (2,4)(2, 4) using n=⟨−fx,−fy,1⟩\mathbf{n} = \langle -f_x, -f_y, 1 \rangle with fxf_x, fyf_y estimated by central differences (h=0.001h = 0.001).

Answer

fx=0.6f_x = 0.6, fy=0.1f_y = 0.1, so the unit normal is about ⟨−0.5126,−0.0854,0.8544⟩\langle -0.5126, -0.0854, 0.8544 \rangle.

Solution

fx=0.2x+0.05y=0.4+0.2=0.6f_x = 0.2x + 0.05y = 0.4 + 0.2 = 0.6 and fy=0.05x=0.1f_y = 0.05x = 0.1 at (2,4)(2, 4). Normalise ⟨−0.6,−0.1,1⟩\langle -0.6, -0.1, 1 \rangle (length 1.37≈1.1705\sqrt{1.37} \approx 1.1705).

python · try it yourself
import numpy as np
f = lambda x, y: 0.1 * x**2 + 0.05 * x * y
h, x, y = 1e-3, 2.0, 4.0
fx = (f(x + h, y) - f(x - h, y)) / (2 * h)
fy = (f(x, y + h) - f(x, y - h)) / (2 * h)
n = np.array([-fx, -fy, 1.0])
print(round(fx, 6), round(fy, 6), np.round(n / np.linalg.norm(n), 4))
0.6 0.1 [-0.5126 -0.0854  0.8544]

Test yourself

Which surface is z=x2−y2z = x^2 - y^2?

  1. A bowl
  2. A cone
  3. A saddle (hyperbolic paraboloid)
  4. A cylinder

How do you get a normal vector of a parametric surface r(u,v)\mathbf{r}(u, v)?

  1. ru⋅rv\mathbf{r}_u \cdot \mathbf{r}_v
  2. ru×rv\mathbf{r}_u \times \mathbf{r}_v
  3. ru+rv\mathbf{r}_u + \mathbf{r}_v
  4. ∂2r/∂u∂v\partial^2\mathbf{r}/\partial u \partial v

What is a normal vector of the plane 3x−y+5z=73x - y + 5z = 7?

  1. ⟨3,−1,5⟩\langle 3, -1, 5 \rangle
  2. ⟨7,0,0⟩\langle 7, 0, 0 \rangle
  3. ⟨3,1,5⟩\langle 3, 1, 5 \rangle
  4. ⟨1,−3,0⟩\langle 1, -3, 0 \rangle