Sculpting with Equations
How to build a coffee mug, a die, and a two-holed doughnut out of a single formula
Every shape in this article — a two-holed doughnut, a coffee mug with a handle, a rounded die with all twenty-one pips — is a single equation. No polygon meshes were sculpted, no 3D modeling software was harmed. The whole trick rests on two ideas simple enough to explain over coffee: you can do arithmetic with yes/no questions, and you can soften a yes/no question into a “maybe.” A third idea, warping space itself, then twists these creations like taffy. Everything is illustrated with free, open-source Python code, so you can sculpt along at home.
1. A shape is a question
There are two very different ways to tell someone what a circle is. One is to give directions for drawing it: put your compass point here, open it this wide, spin. The other is to give a test for membership: the circle of radius \(1\) consists of exactly those points \((x,y)\) for which
\[ x^2 + y^2 = 1 . \]The first description is a recipe for a pencil. The second is a question: hand me any point in the plane and I will answer, using the formula, whether it lies on the circle. Shapes described this way are called implicit, and they are a quietly powerful idea: the formula knows about every point at once. Even better, if we relax the question from “are you on the circle?” to
\[ x^2 + y^2 \le 1 \qquad\text{(“are you inside?”)} \]we get a solid disk instead of a hairline curve. Solid objects in 3D work the same way: \(x^2+y^2+z^2 \le 1\) is a solid ball, \(x^2+y^2 \le 1\) is an infinite solid cylinder around the \(z\)-axis, and \(z \ge 0\) is the half of space above the floor.
That is a fine start, but nobody drinks coffee out of a half-space. Real objects — mugs, dice, pretzels — look hopelessly beyond the reach of one formula. The point of this article is that they are not. We will build them from spheres, cylinders and planes the way a sculptor builds from clay: joining pieces, carving pieces away, and smoothing the seams with a thumb. Our thumb will be a parameter called \(k\).
2. Arithmetic with yes and no
Mathematics has a wonderfully blunt device for turning a shape into a function. Given a set \(A\) (say, the solid disk), its indicator function \(\chi_A\) answers the membership question with a number:
\[ \chi_A(p) = \begin{cases} 1 & \text{if the point } p \text{ belongs to } A,\\ 0 & \text{if it does not.} \end{cases} \]One for yes, zero for no. The surprise is that the standard ways of combining sets become ordinary arithmetic on these answers.
Take two sets \(A\) and \(B\). A point belongs to the intersection \(A \cap B\) when both answers are \(1\), and multiplication detects exactly that:
\[ \chi_{A \cap B} = \chi_A \, \chi_B , \]since a product of zeros and ones equals \(1\) only when every factor is \(1\). Multiplication is the word and. Negation is just as neat: the complement of \(A\) (everything not in \(A\)) has indicator
\[ \chi_{A^{c}} = 1 - \chi_A , \]which flips \(0 \leftrightarrow 1\). Subtraction is the word not. And what about or — the union \(A \cup B\)? A first guess, \(\chi_A + \chi_B\), almost works, but it pays double in the overlap where both answers are \(1\). The correction is to refund the overlap:
\begin{equation} \chi_{A \cup B} = \chi_A + \chi_B - \chi_A \chi_B . \label{eq:union} \end{equation}Finally, carving \(B\) out of \(A\) (the difference \(A - B\)) means “in \(A\) and not in \(B\),” and the dictionary translates it mechanically:
\begin{equation} \chi_{A - B} = \chi_A \, (1 - \chi_B). \label{eq:diff} \end{equation}One more ingredient connects this to the implicit shapes of Section 1. Our primitive solids all have the form “the region where some smooth function \(f\) is positive”:
\[ P_f = \{\, p \;:\; f(p) \ge 0 \,\}, \qquad\text{e.g.}\quad f(x,y,z) = 1 - x^2 - y^2 - z^2 \ \text{for the ball.} \]To get the indicator of \(P_f\) we simply post-process \(f\) with the Heaviside step function \(H\), which crushes every positive number to \(1\) and every negative number to \(0\):
\[ \chi_{P_f} = H \circ f, \qquad H(s) = \begin{cases} 1 & s \ge 0,\\ 0 & s < 0. \end{cases} \]Now we hold a complete toolkit. Want the region inside the ball and above the floor? Multiply their two indicators. Want the union of two cylinders? Use formula \(\eqref{eq:union}\). Any finite sentence made of and, or, not applied to primitive shapes becomes one explicit formula. Engineers know this game as constructive solid geometry, and it is how CAD programs think about machine parts.
There is only one problem, and it is the interesting one: everything so far has sharp edges.
3. The softness knob
The Heaviside function is a cliff. It jumps from \(0\) to \(1\) with no warning, and every shape assembled from it inherits creases, corners, and seams. Sometimes that is what you want (a machined bracket); often it is not (a living form, a worn pebble, a doughnut). Calculus also despises jumps: you cannot differentiate a cliff.
The remedy is to replace the cliff by a ramp. Define, for each \(k > 0\), the sigmoid
\[ \Hk(s) = \frac{1}{1 + e^{-s/k}} . \]Figure 1 shows the whole family. Each \(\Hk\) climbs smoothly from \(0\) to \(1\); the parameter \(k\) decides how briskly. For large \(k\) the climb is leisurely; as \(k \to 0\) the ramp steepens and the family closes in on the Heaviside cliff. Two features deserve a pin in them:
- every curve in the family passes through the same point \(\bigl(0, \half\bigr)\), and
- \(\Hk\) never actually equals \(0\) or \(1\) — it hedges, answering “\(0.93\)” or “\(0.02\)” instead of yes or no.
Now redo Section 2 with the cliff swapped for the ramp. For a shape \(P_f\) we define its smooth indicator
\[ H_{f,k} = \Hk \circ f, \]a function that no longer answers the membership question with a curt yes/no but with a score: close to \(1\) deep inside the shape, close to \(0\) far outside, and gliding smoothly through \(\half\) as you cross the old boundary \(f = 0\). Figure 2 shows the score function of an elliptical disk for two settings of \(k\): each is a plateau of height \(1\) with softened shoulders, and the smaller \(k\) is, the more the plateau looks like a mesa with vertical cliffs.
Here is the beautiful part. The blending formulas \(\eqref{eq:union}\)–\(\eqref{eq:diff}\) never asked their inputs to be exact zeros and ones — they are plain arithmetic and accept scores just as happily. Feed them two smooth indicators \(H_{f,k}\) and \(H_{g,k}\) and they return smooth score-landscapes for the union, the intersection, and the difference of the two shapes. Figure 3 shows all three for a pair of overlapping disks: a two-humped massif, a slim central peak, and a crescent-shaped ridge. No creases anywhere — the arithmetic has melted them.
4. Cut at half height
A landscape of scores is lovely, but we were promised shapes. How do we climb back down? Remember the pinned fact: every sigmoid crosses \(\half\) exactly at \(s = 0\). Consequently the score \(H_{f,k}\) equals \(\half\) exactly where \(f = 0\) — that is, exactly on the boundary of the original shape, whatever the value of \(k\). So the rule is:
slice the score landscape at height \(\half\),
and keep everything above the cut.
For a single primitive this recovers the original circle, sphere, or cylinder without a hair out of place. For a blend it does something better: the half-height contour of, say, \(H_f + H_g - H_f H_g\) is not the sharp-cornered outline of the set-union — it is a smoothed compromise, a curve that hugs the union at a distance and rounds off its crevices. That is a feature, not a bug: it is the mathematical version of running your thumb along the seam between two lumps of clay. Figure 4 shows the three half-height curves from the landscapes of Figure 3, with the original “ingredient” circles dashed underneath.
The knob \(k\) now has a direct sculptural meaning: it is the radius of your thumb. Figure 5 makes the point in 3D by uniting two balls three times, with three settings of \(k\). Small \(k\) gives a crisp weld line; medium \(k\) melts a graceful neck between the balls; large \(k\) fuses them into a single pebble. Readers who remember lava lamps, or the liquid-metal villain of a certain 1991 film, have seen this aesthetic before: computer graphics people call such objects metaballs or blobby models, and they are blends of exactly this kind.
From here on, every picture in this article is obtained the same way: write down score functions for a few primitives, combine them with the arithmetic of Section 2, and draw the set of points where the final score equals \(\half\). One formula per object, as promised.
5. A double doughnut from scratch
Time to build something with real topology: a surface with two holes. The recipe has three lines.
Step 1: the body. Take two flattened ellipsoids, side by side and slightly overlapping — the regions where
\[ e_{\pm}(x,y,z) \;=\; 1 - \Bigl(\frac{x \mp 1}{1.15}\Bigr)^{2} - y^{2} - \Bigl(\frac{z}{0.55}\Bigr)^{2} \;\ge\; 0 . \]Blend their scores with the union formula; the result is a smooth, pillow-like slab with a waist (the blue body in Figure 6).
Step 2: the drills. Take two vertical cylinders of radius \(0.42\) through the centers of the ellipsoids:
\[ c_{\pm}(x,y,z) \;=\; 0.42^{2} - (x \mp 1)^{2} - y^{2} \;\ge\; 0 , \]again united into a single score function (the orange drills in the same figure).
Step 3: drill. Subtract the second score from the first with formula \(\eqref{eq:diff}\) and slice at half height:
\[ F\,(1 - G) \;=\; \tfrac{1}{2}, \qquad \begin{aligned} F &= \text{union score of the two ellipsoids},\\ G &= \text{union score of the two cylinders}. \end{aligned} \]
The result (Figure 7) is the double torus, the mathematician’s genus-two surface, with both tunnels smoothly flared as if the object had been cast in one piece. Written out in full, its equation would sprawl across half a page of exponentials — but we never need to see it, any more than a baker needs to see the chemical formula of a croissant. The recipe is the understanding.
6. The coffee-mug theorem
There is an old joke that a topologist is someone who cannot tell a coffee mug from a doughnut: each has exactly one hole (the handle, the hole), so each can be kneaded into the other without tearing. Let us take the joke literally and manufacture the mug.
The blank. Intersect an infinite vertical cylinder of radius \(1\) with the slab \(-1 \le z \le 1\). Multiplying the two scores gives a solid puck.
The hollow. Take a thinner cylinder, radius \(0.8\), but cut it off below \(z = -0.7\) (intersect with the half-space \(z \ge -0.7\)). Subtract it from the puck. What remains is a cup: walls, a floor, and nothing where the coffee goes.
The handle. Take a torus (a doughnut of tube radius \(0.18\)), stand it upright, and press it into the side wall. The union formula welds it on, and because sigmoids never quite reach \(0\) or \(1\), the weld is a smooth fillet rather than a seam — exactly where a potter would smear the clay with a wet finger.
Figure 8 shows the result next to its doughnut twin. Count holes: the mug’s cavity does not count (a dent, however deep, can be pressed flat), but the handle’s loop does. One hole each. The joke checks out, and we built both punchlines out of sigmoids.
7. A die you could actually roll
Sharp objects are in reach too — and here the “flaw” of blending becomes its best feature. A cube is the intersection of six half-spaces: left of this wall, right of that one, below the ceiling, above the floor, and so on. Multiply the six scores. With a small \(k\) the result is practically a machinist’s cube; with a moderate \(k\) (we used \(k = 0.12\)) the corners and edges come out rounded, because near an edge two scores are simultaneously hedging and their product dips below \(\half\) a little early. A manufacturer would pay extra for that: real dice are tumbled precisely to round their edges.
The pips are twenty-one little balls (that is \(1+2+\dots+6\)), each centered on a face so that half of it pokes into the cube, all subtracted in one long chain of formula \(\eqref{eq:diff}\). Each subtraction carves a neat hemispherical dimple. Figure 9 shows the result — the faces \(1\), \(2\) and \(3\) are visible, and opposite faces sum to seven, as regulation demands.
It is worth pausing on what we have not done. There is no list of vertices in this die, no mesh, no seams between “faces.” There is one smooth function of \((x,y,z)\), and the die is where that function exceeds one half. Roll it in your mind along any direction: every point of the surface answers to the same single formula.
8. Bending space itself
So far we have combined shapes while leaving space alone. The second technique of our title does the opposite: leave the shape’s formula alone and warp the space it lives in.
The principle fits in one line. Suppose a shape is carved out by the inequality \(f(p) \ge \half\), and suppose \(T\) is a transformation that moves points of space around. Then
\[ \boxed{\;\text{the warped shape is carved out by } f\bigl(T^{-1}(p)\bigr) \ge \half .\;} \]Why the inverse? Because to decide whether a point \(p\) belongs to the warped object, we send \(p\) back to where it came from and ask the original formula there. (If a moving company shifts your house three meters east, your old floor plan still answers questions — provided you first walk three meters west while holding it.)
You have used special cases forever. Replacing \(x\) by \(x - 3\) moves a graph three units right; replacing \(x\) by \(x/2\) stretches it to double width. These are the affine transformations: translations, rotations, scalings, shears. They are loyal but predictable — straight lines stay straight.
The fun begins when the amount of transformation depends on where you are. Three classics, applied to a rounded bar in Figure 10:
- Wave. Replace \(x\) by \(x - 0.35\sin(2.2\,z)\): each horizontal slice slides sideways by an amount that oscillates with height. The bar sways like seaweed.
- Taper. Replace \(x, y\) by \(x/s(z),\, y/s(z)\) with \(s(z) = 1 - 0.28\,z\): each slice is scaled by a factor that shrinks with height. The bar becomes an obelisk.
- Twist. Rotate each slice by an angle proportional to its height — replace \((x, y)\) by \(\bigl(x\cos\alpha z + y\sin\alpha z,\; -x\sin\alpha z + y\cos\alpha z\bigr)\). The bar becomes barley sugar.
Note what the box above buys us: the warped objects are still single equations — we literally substitute the new expressions into the old formula. Blending and warping therefore compose freely, and this freedom is where the sculpting gets luxurious. As a finale, take an ordinary torus but flatten its circular cross-section into a thin blade; then let the blade’s tilt angle depend on the azimuth — as you travel once around the ring, the cross-section rotates once about the ring’s core. The result (Figure 11) is a twisted ribbon of a doughnut, the kind of object you would swear needed an artist and a 3D-modeling package. It is four lines of substitutions.
9. Sculpt your own
Every figure here was produced with a small open-source Python package,
gblend, which makes the blending arithmetic feel
like set theory: the Python operators &, | and - are
wired to the product, the inclusion–exclusion formula, and the carve-out formula of Section 2.
The double torus of Figure 7, in its entirety:
from gblend.geometry import Ball3D, Cylinder3D
k = 0.18
e1 = Ball3D(k=k); e1.scale(1.15, 1, 0.55); e1.translate(-1, 0, 0)
e2 = Ball3D(k=k); e2.scale(1.15, 1, 0.55); e2.translate(+1, 0, 0)
c1 = Cylinder3D(x0=-1, r_val=0.42, k=k)
c2 = Cylinder3D(x0=+1, r_val=0.42, k=k)
double_torus = (e1 | e2) - (c1 | c2) # blend, then drill
The mesh is extracted with scikit-image’s marching cubes and rendered with
plotly — free tools, a laptop, no licenses. Install gblend with
pip, then run the article’s script make_figures.py to reproduce every
image.
Beyond the fun, there is a moral. We usually meet functions as graphs to be plotted and formulas as things to be solved. Here they played a different role: a formula was a material — something to be cast, carved, welded and twisted. The dictionary was small (multiply for and, flip for not, refund the overlap for or, soften the step, cut at one half, warp with a substitution), yet it was enough to make a mug, a die, and a two-holed doughnut out of thin air. Pick a shape on your desk and ask: what is its equation? You now own every tool needed to answer.
Further reading
- J. Blinn, A generalization of algebraic surface drawing, ACM Transactions on Graphics 1 (1982) — the birth of blobby modeling.
- A. Barr, Global and local deformations of solid primitives, SIGGRAPH (1984) — bends, tapers and twists.
- W. Lorensen and H. Cline, Marching cubes, SIGGRAPH (1987) — from equations to triangles.
- A. Ricci, A constructive geometry for computer graphics, The Computer Journal 16 (1973) — blending unions and intersections before it was cool.
I work as an independent consultant helping engineering and computational teams solve difficult problems in geometric modeling, surface processing, and algorithmic design. If your team is tackling a non-trivial spatial or mathematical challenge, reach out directly at cvalero@carlosvalero.com.