LearnFirst steps

Lesson 4 of 30 · 50 min

Circles and distance

Draw circles and rings from the distance to a center, with smooth edges

In this lesson you will

  • Measure the distance from a point with length and distance
  • Draw a circle from distance with step and smoothstep, smoothing its edge over one pixel, px = 2.0 / resolution.y
  • Draw rings, off-center circles, ellipses and several overlapping circles
  • Inspect a distance by turning it into colors and contour lines
  • Understand sdCircle, a distance that goes negative inside, and the fill formula 1.0 - smoothstep(-px, px, d)

01Measuring distance

A circle is the set of points that are all the same distance from a center. So drawing a circle in a shader starts with working out how far each pixel is from the center.

The distance from a point p to the origin (0, 0) is sqrt(p.x * p.x + p.y * p.y), by Pythagoras. In GLSL you write it as length(p). For example, if p is (0.3, 0.4), length(p) is √(0.09 + 0.16) = √0.25 = 0.5.

1

2

3

4

5

Starts when it scrolls into view
Open in /edit ↗
▸Black at the center, brighter further out. What if d * becomes (1.0 - d) *? With length(uv) instead of length(p), where is the center?

This picture shows distance as brightness. Move the same amount from the center in any direction and you get the same brightness, so equal brightness forms concentric circles. Because we use p, they are truly round (thanks to the aspect correction from Knowing where you are).

02A circle with step

Once you have the distance, a circle is simply "where the distance is less than the radius". In Shaping functions, step(r, d) gave 0 where d is below r and 1 elsewhere. We want 1 inside the circle, so we flip it by subtracting from 1.0.

1

2

3

4

5

6

Starts when it scrolls into view
Open in /edit ↗
▸Change 0.5 to 0.2 or 0.9. And why does the circle get squashed with length(uv - 0.5) instead of length(p)?

circle is either 0 or 1. Multiply a color by it and add it to the background, and the color lands only inside the circle. In this course we call a number like this — 1 inside a shape, 0 outside — a mask.

03Moving the center

To center the circle on some point c other than (0, 0), subtract c from p before measuring the length. length(p - c) is the distance between points p and c, and you can also write it as distance(p, c). Where p is exactly at c, p - c is (0, 0) and the distance is 0. It is the same rule as in Knowing where you are: subtract from the coordinates, and the shape moves that way.

1

2

3

4

5

6

7

Starts when it scrolls into view
Open in /edit ↗
▸Set c to vec2(-0.8, -0.5). With vec2(sin(time), 0.0), the circle swings left and right

04Smoothing the edge

Look closely and the circle's edge is jagged. Each pixel can only be the inside color or the outside color, so a curved edge turns into a staircase. Those steps are called jaggies.

The fix is to give pixels that straddle the edge an in-between color. Use smoothstep instead of step, so the value moves smoothly from 0 to 1 over a very narrow range around the radius.

How wide should that range be? Too wide and the edge blurs; too narrow and the jaggies stay. The sweet spot is one pixel. As you saw in Knowing where you are, p.y goes from -1 to 1 — 2 units — over the resolution.y pixels of the screen's height, so one pixel is 2.0 / resolution.y. On a 450-pixel-tall preview that is about 0.0044. This course always calls this value px. With smoothstep(r - px, r + px, d), the edge fades over one pixel inside and one outside: two pixels in total.

1

2

3

4

5

6

7

8

9

10

Starts when it scrolls into view
Open in /edit ↗
▸Compare the two edges. What happens if r - px and r + px become r - px * 20.0 and r + px * 20.0?

Tip

The fwidth(d) function also tells you how much d changes from one pixel to the next, and can stand in for px. This course sticks with 2.0 / resolution.y because its meaning is easier to see.

05Rings

To draw only the outline of a circle, use how far each point is from the rim. d - r is 0 on the rim, negative inside and positive outside. abs(d - r) grows the further you get from the rim in either direction — the same trick as drawing graph lines in Shaping functions. Fill wherever it is less than w and you get a ring 2w thick.

1

2

3

4

5

6

7

8

Starts when it scrolls into view
Open in /edit ↗
▸Set w to 0.005 or 0.1. What about abs(d - 0.5 - 0.1 * sin(time * 3.0)) instead of abs(d - 0.6)?

06Seeing distance as contour lines

A distance is like a map of numbers spread across the screen. Let's look at its shape more closely. fract(d * 5.0) repeats 0 to 1 every time the distance grows by 0.2 (the fract from Shaping functions). Draw a line where each repeat begins and, like contour lines on a map, points at the same distance link up.

1

2

3

4

5

6

7

8

Starts when it scrolls into view
Open in /edit ↗
▸Change 5.0 to 15.0: how far apart are the lines now? With length(p * vec2(1.0, 2.0)) in place of length(p - vec2(0.3, 0.0)), what shape do the contours take?

If the contours are neat concentric circles, evenly spaced, the number is a proper distance. Distort the coordinates, as with p * vec2(1.0, 2.0), and the contours distort too, with uneven spacing. When a shape looks wrong, viewing its distance like this often reveals why.

07sdCircle: a distance that goes negative inside

So far we have compared d with the radius r to decide inside or outside. Subtract the two up front and you get an even more useful number.

1

float sdCircle(vec2 p, float r) { return length(p) - r; }

sdCircle(p, r) returns the distance from point p to the rim of a circle of radius r — positive outside, exactly 0 on the rim and negative inside. With r = 0.5 it is -0.5 at the center (0, 0) and 0.3 at (0.8, 0.0). A distance whose sign flips between inside and outside is called a signed distance; that is what the sd stands for. This course uses sdCircle, with exactly this name and body, all the way to the end.

With one signed distance d in hand, filling and outlining are both easy:

  • Fill: 1.0 - smoothstep(-px, px, d) … 1 where d is negative (inside), fading over one pixel on each side of the rim
  • Outline: 1.0 - smoothstep(w - px, w + px, abs(d)) … 1 within w of the rim
  • Move the center: sdCircle(p - c, r)

The fill formula 1.0 - smoothstep(-px, px, d) shows up again and again for the rest of the course. Since -px < px, it keeps smoothstep's rule (a < b). The playground below shows a signed distance as color: blue outside, orange inside, with each stripe marking a step of 0.1 in distance.

1

2

3

4

5

6

7

8

9

10

Starts when it scrolls into view
Open in /edit ↗
▸Change 0.5 to 0.2: where does the color boundary go? What about sdCircle(p - vec2(0.5, 0.2), 0.5)?

Tip

This way of viewing a signed distance as color comes back in Drawing with distance, where we extend it to boxes, line segments and more.

08Ellipses

An ellipse is a circle stretched in one direction only. Instead of stretching the shape, shrink the coordinates. Multiply p.x by 0.5 and, even where p.x is 0.8, the circle only sees 0.4, so the circle appears twice as wide. It is the reverse of "double the coordinates and the shape halves" from Knowing where you are.

1

2

3

4

5

6

7

8

9

10

Starts when it scrolls into view
Open in /edit ↗
▸Try vec2(1.0, 0.6) instead of vec2(0.5, 1.0). With vec2(1.0, 0.8 + 0.2 * sin(time * 3.0)), it wobbles like jelly

Watch out

Squashing or stretching the coordinates throws the distance off by the same factor. In this ellipse, with x halved, d changes half as fast at the left and right ends, so the edge blur there is twice as wide. A little is hard to notice, but the more you stretch, the blurrier (or more jagged) the edge gets. Moving space shows the proper fix.

09Combining circles

With two or more circles, there are three main ways to combine them:

  • Add the colors … overlaps get brighter, like overlapping lights
  • Combine the masks with max … 1 if inside either one; overlaps are no brighter
  • Combine the distances with min … the nearer distance, giving you the distance to both shapes as one

First, addition. Overlap a red, a green and a blue circle and add them, and you can see the light mixing from Your first shader right on screen.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Starts when it scrolls into view
Open in /edit ↗
▸Overlaps turn yellow, cyan and magenta, and all three together make white. Remove the // on the line just before return to merge them into one shape with max

Next, min. min(d1, d2) is, at every point, the distance to the nearer circle. It is negative inside either one, so filling it gives the two circles merged. So far that looks just like masks combined with max. The difference is that the result is still a distance, so you can draw an outline around both circles as a single shape, as with abs(d - 0.06). A mask is just 0 or 1, with no memory of how far away anything is, so it can't do this.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Starts when it scrolls into view
Open in /edit ↗
▸Change min(d1, d2) to max(d1, d2) and only the overlap remains (we use this in Combining shapes). What happens with 0.0 instead of 0.06?

10Exercises

Use distance to draw pictures made of circles. Always smooth the edges with px.

Exercise 1

Three bubbles

Draw three bubbles, each a different size, position and color. A bubble is a circle with a faint tint inside, a bright ring on its rim, and a small highlight in its upper left.

1

2

3

4

5

6

7

8

9

10

Starts when it scrolls into view
Open in /edit ↗
Exercise 2

Ripples

Make concentric waves spreading outward from one point, like ripples after a stone hits water. The waves should weaken as they travel out and fade away before the edge of the screen.

1

2

3

4

5

6

Starts when it scrolls into view
Open in /edit ↗
Exercise 3

A snowman

Join a big and a small circle with min to make a snowman. Fill the body white and draw one blue outline around the outside of the whole shape (with no line where the circles overlap). Add two eyes to finish.

1

2

3

4

5

6

7

8

9

10

Starts when it scrolls into view
Open in /edit ↗

11Recap

  • length(p) is the distance from the origin; distance(p, c), or length(p - c), is the distance from point c
  • A circle is where the distance is less than the radius; a mask is 1 inside and 0 outside
  • Smooth an edge with smoothstep over one pixel, px = 2.0 / resolution.y
  • A ring uses abs(d - r); an ellipse shrinks the coordinates in one direction
  • Combine by adding colors, taking max of masks, or min of distances — and min keeps the result usable as a distance
  • sdCircle(p, r) = length(p) - r is a distance that goes negative inside; fill it with 1.0 - smoothstep(-px, px, d)

Next time we blend colors with mix, build color palettes, and layer shapes over a background to finish a complete picture.

Finished lessons get a mark in the course contents