LearnFirst steps

Lesson 10 of 30 · 50 min

Polar coordinates

Think in angle and distance to draw rays, flowers, spirals and kaleidoscopes

In this lesson you will

  • Describe a position as angle and distance with atan(y, x) and length
  • Define PI and TAU with #define, and map the angle to 0–1 to draw a color wheel
  • Make rays, flower shapes with cos(a * n), and spirals
  • Repeat and fold the angle with mod to build a kaleidoscope
  • Spot and fix problems at the ±π seam of the angle

01Position as angle and distance

A point on the screen is usually described by two numbers, (x, y). But you can also describe the same point by two other numbers: which direction it lies from the center, and how far away. That's called polar coordinates. Patterns that spread out from the center or wrap around it are much easier to draw in terms of angle and distance than x and y.

The distance is length(p), from Circles and distance. The angle comes from atan(p.y, p.x). The answer is in radians, measured counterclockwise starting from 0 at the right-pointing (1, 0). Up, (0, 1), is about 1.57; left, (-1, 0), is about 3.14 (π); down, (0, -1), is about -1.57. The range is -π to π, and it jumps from π to -π at the left.

Watch out

atan(y, x) takes y first. There's also a one-argument form, atan(p.y / p.x), but it can't tell the right half from the left half, and it only returns -π/2 to π/2. To get an angle, always use the two-argument atan(p.y, p.x).

Angle math uses π a lot, but GLSL doesn't provide it, so — just as we did for TAU in Shaping functions — we define it at the top of the code with #define. #define PI 3.14159265 means "before compiling, replace the text PI with 3.14159265 in the code that follows". It isn't a variable, so there's no type and no ;. TAU is twice π: the angle of one full turn.

1

2

#define PI 3.14159265

#define TAU 6.28318531

Let's look at angle and distance as colors. The angle is mapped to 0–1 with a / TAU + 0.5 and shown in red; the distance goes straight into green.

1

2

3

4

5

6

7

8

9

Starts when it scrolls into view
Open in /edit ↗
▸Change vec4(u, r, 0.0, 1.0) to vec4(vec3(u), 1.0) to see only the angle, or vec4(vec3(r), 1.0) to see only the distance

Red (the angle) grows brighter going counterclockwise from the left — down, then right, then up — and when it comes back around to the left it snaps from the brightest 1 to the darkest 0. Remember this seam on the left; it will cause trouble later. Green (the distance) brightens in rings from the center outward.

You can also go the other way, from angle a and distance r back to ordinary coordinates: r * vec2(cos(a), sin(a)). It's the same formula we used in Time and motion to move a point around a circle. We'll use this round trip in the kaleidoscope at the end of the lesson.

02Coloring by angle: a color wheel

The angle u, mapped to 0–1, fits perfectly as the hue for hsv2rgb from Mixing colors, because hue wraps around like a ring: 0 and 1 are the same red. Use the distance as saturation and you get the familiar color-picker wheel: white in the center, more vivid toward the rim.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

Starts when it scrolls into view
Open in /edit ↗
▸Use u * 2.0 and the rainbow goes around twice per turn. With u * 2.5, a seam appears on the left

The previous playground had a seam on the left, but the color wheel doesn't. u still jumps from 1 to 0 there, but hue 1 and hue 0 are both red, so the colors join up. With u * 2.0, 2 and 0 are also the same color, so it joins. With u * 2.5, though, 2.5 (cyan) and 0 (red) differ, and the seam shows.

03The seam at ±π

Here's why the seam happens. On the left, atan's answer leaps from π to -π, a jump of 6.28. Use the angle directly as a brightness or a position and the value changes abruptly there, leaving a visible line. To avoid the seam, run the angle through a formula that gives the same value when the angle changes by a full turn (TAU).

  • sin(a * n) or cos(a * n) with whole-number n … the wave repeats exactly n times per turn, so the values match at the seam
  • Hue … 0 and 1 are the same color
  • abs(u * 2.0 - 1.0) … turns the 0-to-1 u into a peak shape that has the same value at 0 and 1 (a mirror)

1

2

3

4

5

6

7

8

9

10

11

12

Starts when it scrolls into view
Open in /edit ↗
▸Remove the leading // on one line at a time and watch the seam disappear. What about cos(a * 3.0) instead of cos(a)?

Tip

At the center, (0, 0), there's no direction, so the angle means nothing. Near the center, stripes made from the angle crowd together until they flicker. Covering the center with a small circle, or fading the pattern toward the center, are the standard fixes.

04Rays

sin(a * n) swings positive and negative n times in one turn. Color the positive and negative parts differently and you get rays spreading out from the center. With n = 12, that's 12 positive and 12 negative, 24 rays in all. Add time to the angle and the rays rotate.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Starts when it scrolls into view
Open in /edit ↗
▸Change 12.0 to 12.5 and the rays break off unnaturally at a seam on the left. Change + time * 0.5 to - time * 0.5 to reverse the spin

Adding time to the angle makes the rays turn clockwise. Just like the traveling wave in Time and motion, adding moves things in the negative direction (decreasing angle, which is clockwise), and subtracting moves them in the positive direction (counterclockwise). The ray edges use smoothstep(-0.15, 0.15, rays). Since rays is a sine value rather than a distance, the width is chosen by eye instead of using px.

05Flower shapes

A circle is a shape whose edge is the same radius at every angle. Let that radius depend on the angle and you can make all sorts of shapes. edge = 0.5 + 0.15 * cos(a * 5.0) has a radius of 0.65 in the five directions where cos is 1, and 0.35 where it's -1. Fill wherever the distance r is less than edge and you get a flower with five petals.

Check with numbers: at a = 0 (pointing right), cos(0) = 1, so the edge is at 0.65. At a = 0.628 (π/5, 36°), cos(π) = -1, so the edge is at 0.35. A petal tip comes every 72°. d = r - edge is negative inside, so the fill from Combining shapes works here too.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

Starts when it scrolls into view
Open in /edit ↗
▸Try n = 3.0 or 8.0. Change 0.15 to 0.3 for deeper cuts between the petals. Replace cos(a * n + time * 0.5) with abs(cos(a * 3.0)) for six rounded petals

abs(cos(a * 3.0)) folds the negative lobes of the cosine up, giving six bumps per turn with sharp pinches between them — handy for petal-like shapes. The color comes from passing r to the palette, so it changes from the center outward.

Tip

r - edge isn't exactly the distance to the edge (it's slightly off wherever the edge doesn't face the center). Around the pinches between petals the smooth edge width varies a little, but you'll hardly notice. Exact distances are covered in Drawing with distance (2D SDF).

06Spirals

Take the rays sin(a * n) and add the distance, and you get a spiral. The crests of sin(a * 3.0 + r * 10.0) (lines of equal value) lie where a * 3.0 + r * 10.0 stays constant. Move 0.1 further out and r * 10.0 grows by 1, so to stay on the same line the angle must shrink by 1/3. The angle keeps shifting as you go outward, so the line winds around the center as it spreads.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

Starts when it scrolls into view
Open in /edit ↗
▸Change 3.0 to 1.0 for a single arm. Change r * 10.0 to r * -10.0 to reverse the twist. What does r * 25.0 do?

The number multiplying the angle, 3.0, is the number of arms, and it must be a whole number to avoid a seam. The number multiplying the distance, 10.0, sets how tightly it winds: bigger is tighter. - time * 2.0 makes the spiral seem to turn while flowing outward. As in Time and motion, subtracting time moves the places of equal value in the positive direction: outward in distance, counterclockwise in angle.

07Kaleidoscopes

In Repetition and tiles we repeated x and y with fract. Do the same with the angle and you cut the screen into identical slices, like a pizza. With one slice's angle as slice = TAU / n, mod(a, slice) runs from 0 to slice inside every slice. Then fold it at the middle of the slice with abs(a - slice * 0.5), and neighbors face each other like mirror images. That's a kaleidoscope.

With n = 6, one slice is about 1.047 (60°). A point at angle 2.5 becomes 2.5 - 1.047 × 2 ≈ 0.41 after mod, and folding gives abs(0.41 - 0.52) ≈ 0.12. Finally, turn the folded angle and the original distance back into an ordinary coordinate q with r * vec2(cos(a), sin(a)). Draw anything you like in q and it appears as if reflected in 12 mirrors.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

Starts when it scrolls into view
Open in /edit ↗
▸Try n = 8.0 or 3.0. Put // in front of the a = abs(…); line to stop mirroring: you get rotated copies all facing the same way

All we draw is one moving circle, one spinning box and one pulsing ring. Repeating and folding the angle multiplies them many times over. mod returns a non-negative answer even for negative angles, and TAU divides evenly by slice, so the pattern lines up even across the ±π seam. Keep n a whole number and no seam appears anywhere.

08Exercises

Use angle and distance to make patterns. If a seam shows up, check that the number multiplying the angle is a whole number and that your formula wraps around after a full turn.

Exercise 1

A dartboard

Right now the target has only concentric rings. Turn it into a polar checkerboard like a dartboard, where the color alternates both across the rings and across 20 equal wedges.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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

A blooming flower

Make a six-petaled flower that slowly opens and closes, over and over. A short pause when fully open and fully closed makes it feel alive. If you have time, add a second layer of petals in another color peeking out between the first.

1

2

3

4

5

6

7

8

9

10

11

12

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

Radar

Make a radar sweep that turns counterclockwise, with a tail that fades out behind the bright line. The key is keeping the tail from breaking at the ±π seam on the left. If you have time, add a blip that lights up only when the sweep passes over it.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

Starts when it scrolls into view
Open in /edit ↗

09Recap

  • Distance is length(p); angle is atan(p.y, p.x) (y first, -π to π, counterclockwise)
  • Write #define PI 3.14159265 and #define TAU 6.28318531 yourself; a / TAU + 0.5 maps the angle to 0–1
  • The angle jumps from π to -π on the left; hide the seam with whole-number multiples in sin/cos, hue, mirroring, or fract of a difference
  • sin(a * n) makes rays, r - (0.5 + 0.15 * cos(a * n)) makes a flower, angle + distance makes a spiral
  • Repeat with mod(a, TAU / n), fold with abs(a - slice * 0.5), and convert back with r * vec2(cos(a), sin(a)) for a kaleidoscope

That wraps up Part 1, First steps. Color, coordinates, shapes, motion, repetition and polar coordinates — you now have the foundations for drawing with shaders. Part 2 begins with Making randomness, adding variety to orderly patterns, and goes on to use noise for natural patterns like clouds and fire.

Finished lessons get a mark in the course contents