TL;DR
A signed distance function is a rule that gives every point in space one number: how far the point is from the edge of a shape. The number is positive outside the shape, negative inside, and zero on the edge. The function turns a shape into something you can measure, differentiate and combine with other functions, and it matters wherever a program needs to know not only whether a point is inside, but how far it is from the boundary.
Builds on: Euclidean Distance, Gradient · Leads to: Level Set Method
Where it's used
Drawing text and icons at any size. Some game engines and font renderers store a letter as a grid of signed distances instead of as pixels. Each pixel is then drawn by asking whether its stored value is above or below zero. Because the values change smoothly across the edge, the letter can be enlarged or rotated without the blocky look that a plain bitmap gives.
Following a moving boundary in simulation. The Level Set Method tracks an interface such as the surface of a water wave or a growing bubble. It stores a signed distance function on a fixed grid and calls the interface the points where the value is zero. When the interface moves, the grid values change smoothly, and the interface can split or merge with no extra bookkeeping.
Safety margins for robots. An arm that plans a path must keep away from obstacles. If each obstacle is stored as a signed distance function, one lookup gives the distance to the nearest surface, and the sign says whether the arm is already inside. A planner can then require, say, a margin of 0.5 units without solving any geometry at run time.
Stepping rays through a scene. Renderers that draw scenes described by formulas, rather than by triangles, look for the first point where a line of sight touches a surface. The signed distance at the current point is the distance to the nearest surface, so the renderer can jump that far safely. The third worked example traces one such step.
How it works
Take a one-dimensional shape: the interval of numbers from 2 to 5. Its edges are the points 2 and 5. We want a function φ, the signed distance, that answers for any point x how far x is from the interval, with a negative sign when x is inside.
-
Find the edges. The shape is all x with 2 ≤ x ≤ 5. Its edges are 2 and 5. Check: both are exactly where the interval stops.
-
Measure from a point outside. Take x = 0. The nearest edge is 2, and the distance from 0 to 2 is 2. The point is outside, so φ(0) = +2. Check: 0 is two steps to the left of the left edge.
-
Measure from a point inside. Take x = 3. The nearest edge is 2, at distance 1. The point is inside, so φ(3) = −1. Check: 3 is one step from the left edge and two from the right one. The nearer edge decides the answer, so the value is −1.
-
Read off the zero. Take x = 5. The distance to the nearest edge is 0, so φ(5) = 0. Check: the edges are exactly the points where φ is zero. That zero set is the boundary of the shape, and it is the reason the function exists.
-
Find the formula. Repeating steps 2 to 4 at more points gives a clear pattern. Moving left to right, the values fall from +2 at x = 0 to zero at 2, reach the lowest value of −1.5 at the centre 3.5, climb back through zero at 5, and keep rising on the right. The centre is 3.5 and the half-width is 1.5, so φ(x) = |x − 3.5| − 1.5. Check: at x = 0, |0 − 3.5| − 1.5 = 3.5 − 1.5 = 2. ✓ At x = 3, |3 − 3.5| − 1.5 = 0.5 − 1.5 = −1. ✓
- φ(x) for the interval [2, 5]
The same recipe works in two dimensions: the distance from a point to a shape is the smallest distance from that point to any point on the shape's edge.
The math
Let be a shape in the plane or in space, and let be its boundary, the set of its edge points. For a point , the distance from to the boundary is . The signed distance function is
so it is positive outside , negative inside, and zero exactly on .
Step 1 in symbols: the distance to the edge
The distance from a point to a set is the smallest of the distances to the points of the set:
Here is the ordinary Euclidean distance from Euclidean Distance. The minimum means: try every edge point , measure, and keep the smallest result. In the interval example, , so for we get .
Step 2 in symbols: the sign
The sign depends only on whether the point is inside. The point lies in , and . So , the value in step 3 of the walkthrough.
Step 3 in symbols: the slope is always one
The important property is that the gradient of has length one wherever is differentiable:
For the interval, . Its derivative is , which is to the right of the centre and to the left. Its absolute value is one everywhere except at the centre, where the function has a corner.
For a disk of radius 2 centred at the origin, . The gradient of is , so its length is for every . The equation is the Eikonal Equation, and a signed distance function solves it.
Worked examples
Use , the function built in the walkthrough.
- Find . The nearest edge is 2, at distance 2. The point is outside, so the value is . Formula: .
- Find . The edge 5 is at distance 1 and the edge 2 is at distance 2, so the nearest is 5 at distance 1. The point is inside, so the value is . Formula: .
- Find . The nearest edge is 5, at distance 1, and the point is outside. Formula: .
- Interpretation: the point 6 lies exactly one unit beyond the right edge, and the sign says it is outside.
The disk is . Its boundary is the circle of radius 2, so .
- Find . The length of is , so .
- Interpretation: the point is outside and three units from the circle. The nearest circle point is . The distance between and is . ✓
- Find . The length of is 1, so .
- Interpretation: the point is inside, one unit from the circle. The nearest circle point is , and the distance from to it is 1. ✓
A renderer searches along a line of sight for the first point that touches a surface. Take a circle with centre and radius 2. The ray starts at the origin and points at the centre, with unit direction . Its signed distance is .
- Find at the origin. The distance from to is 5, so . The ray can safely travel 3 units, because no surface is closer than 3.
- Move 3 units along : the new point is .
- Interpretation: the ray has advanced 3 units in one jump, without testing any point in between.
Try it: compute . Answer: the vector from to is , whose length is . So . The ray has touched the surface in one step.
Common mistakes and tradeoffs
It is tempting to think that any function which is zero on the edge is a signed distance function. In fact the zero set does not decide the function. The function is also zero on the same circle, but its gradient has length 2, not 1, so it measures distance at twice the true rate. A signed distance function is the exact distance, and the test is what makes it exact.
It is tempting to think the sign tells you the direction to the shape. In fact the sign only says whether the point is inside or outside. The magnitude is the distance. The gradient points away from the nearest edge point, so the direction toward that edge point is the negative gradient.
It is tempting to think that the function is smooth everywhere. In fact it has corners along points that are equally far from two different parts of the edge. In the interval example, the centre 3.5 is such a point, and the gradient is undefined there.
Use a signed distance function when you need distances, margins, or a smooth boundary that can move, as in the uses above. Do not use one when you only need a yes or no answer for whether a point is inside, because a boolean test is cheaper. Computing the exact distance for a complicated shape can be expensive, so many systems store an approximate or truncated distance instead. Those versions often lose the exact property .
History
Image processing long used distance transforms, which give every pixel its distance to the nearest feature of a picture. Those were the discrete ancestors of the continuous function here. In 1988, Stanley Osher and James Sethian introduced the level set method, which keeps a moving front as the zero set of such a function, so that the front can change its shape and topology without special handling (Fronts Propagating with Curvature-Dependent Speed (Level Set Method)). Distance fields later became common in real-time graphics, once graphics hardware was fast enough to evaluate them for every pixel.
Recap
- A signed distance function gives each point its distance to a shape's edge: positive outside, negative inside, zero on the edge.
- The distance to a set is the smallest distance to its points, built from the Euclidean Distance.
- Its gradient has length one wherever it is differentiable, so it is the exact distance and can be trusted as a safe step.
- Read next: Level Set Method, which uses it to track a moving boundary, and the Eikonal Equation, which the function solves.
Check yourself
QuestionWhat does the sign of a signed distance function tell you about a point, and what is its zero set?Show answer
The sign says whether the point is inside the shape (negative) or outside it (positive). The zero set is the set of points exactly on the boundary, the edge of the shape.
QuestionLet the shape be the interval from 0 to 4. Its signed distance is . Compute and .Show answer
For : . The point is inside and one unit from the edge 0, so the answer is . For : . The point is outside, two units beyond the edge 4.
QuestionWhy must the gradient of a signed distance function have length one wherever it is differentiable?Show answer
Take a small step of length directly away from the nearest edge point. The distance to the edge grows by exactly , so the slope in that direction is one. No step of length can change the distance by more than : by the triangle inequality, the distance from the new point to the nearest edge point is at most more than from the old point. The slope is therefore exactly one.
QuestionA robot must keep a margin of at least 0.5 units from an obstacle whose shape is stored as a signed distance function. At the arm tip, the function has the value 0.8. Is the margin kept?Show answer
Yes. The value is the distance from the tip to the surface, since the tip is outside the obstacle. A value of 0.8 is larger than the required 0.5, so the tip is farther from the obstacle than the margin.