↓ Skip to main content

Drawing a raven with circles

·1307 words·7 mins
Yenjia Chen
Author
Yenjia Chen
Deep learning, medical AI, data science, and mathematics.

When the homepage loads, a pen at the end of a chain of spinning circles draws the raven’s outline before the ASCII portrait appears. That animation is a Fourier series: a closed curve written as a sum of rotations. The figure below uses the same coefficients as the homepage. Change the number of circles and watch the outline sharpen.

                    .:=++++++*%@@@%#+:
                   .+#%@@@@@@@@@@@@@@@*:
                        .-+%@@@@@@@@@@@@-
                           .#@@@@@@@@@@@@.
                            -*#%@@@@@@@@@+
                          -#%@@@@@@@@@@@@#
                       .+%@@@@@@@@@@@@@@@@.
                     :*@@@@@@@@@@@@@@@@@@@+
                   .*@@@@@@@@@@@@@@@@@@@@@%
                 .+@@@@@@@@@@@@@@@@@@@@@@@%
               .+%@@@@@@@@@@@@@@@@@@@@@@@@+
             .+%@@@@@@@@@@@@@@@@@@@@@@@@@#
           .=%@@@@@@@@@@@@@@@@@@@@@@@@@%=
          =%@@@@@@@@@@@@@@@@@@@@@@@@@#=.
        -%@@@@@@@@@@@@@@@@@@@@@@@@*=:
       -**%@@@@@@@@@@@@@@@@@@@@@@=
        :*@@@@@@@@@@@@@@@@%*=*@@%
      .*@@@@@@@@@@@@@@@%%@#  .*@@:
     =@@@@@@@@@%@@@%*=: .-##:  .+%+.
   -%@@@@@@@@**@%*-.       =#*:  .+#=.
 .*@@@@@@@%+::=:            =%@#*=-#@%*+==-.
-%@@@@@%*-.                 . ........:...:.
%@@%#+-.
The dashed outline is the reference curve made from all 320 terms. The solid line uses only the largest K. RMS is the root-mean-square distance between the two curves as a share of the raven’s height. The bars below show every term’s size; highlighted bars are in use.

A closed curve is a function of time
#

Imagine tracing the raven’s outline once at a steady pace, taking one unit of time for the whole loop. At each time tt the pen has a position (x(t),y(t))(x(t), y(t)). Writing that position as a single complex number turns the drawing into one function:

z(t)=x(t)+i y(t),0≤t<1.z(t) = x(t) + i\,y(t), \qquad 0 \le t < 1.

Because the outline is closed, the pen returns to its starting point: z(t+1)=z(t)z(t+1) = z(t). The curve is a periodic function, and periodic functions are what Fourier series describe.

One rotating arrow
#

The simplest periodic motion in the complex plane is

c e2πint.c\,e^{2\pi i n t}.

Here cc is a complex number and nn is an integer. As tt runs from 0 to 1, this point moves around a circle:

  • its radius is ∣c∣|c|;
  • its starting angle is the angle of cc;
  • it completes nn full turns; positive and negative nn rotate in opposite directions;
  • with n=0n = 0 it does not move. That term fixes the drawing’s centre.

Set the figure to 1 circle: the pen moves around a single circle. It is the raven’s largest term, with n=1n = 1, and sets the overall size of the loop.

Arrows placed tip to tail
#

One arrow can only draw a circle. To draw something more complex, connect several arrows: the second starts at the tip of the first, the third at the tip of the second, and so on.

In the complex plane, placing arrows tip to tail is simply addition: adding two complex numbers joins the arrows that represent them. So the end of the chain is the sum of all the arrows:

z(t)=c0+c1e2πit+c−1e−2πit+c2e4πit+c−2e−4πit+⋯=∑ncn e2πint.\begin{aligned} z(t) &= c_0 + c_1 e^{2\pi i t} + c_{-1} e^{-2\pi i t} \\ &\quad + c_2 e^{4\pi i t} + c_{-2} e^{-4\pi i t} + \cdots \\ &= \sum_{n} c_n\, e^{2\pi i n t}. \end{aligned}

Each arrow still turns at its own constant speed, but joined together, their end traces a path far more complex than any single circle. This sum is the curve’s Fourier series. Each cnc_n sets the size and starting angle of one circle.

Start with the simplest pair: add the second-largest term, n=−1n = -1, to n=1n = 1. It turns as fast as the first arrow, but in the opposite direction. While the large arrow makes one turn, the small arrow on its tip makes one turn the other way, stretching the pen’s path into an ellipse:

n = 1n = −1their sum
The large arrow (n=1n = 1) turns once while the small arrow (n=−1n = -1) turns once the other way. The pen traces the same ellipse as “2 circles” in the interactive figure.

Then keep adding terms in the interactive figure above:

  • 3 circles: adding n=−2n = -2 starts to bend the loop.
  • After that: keep adding circles and watch when the beak, wing and tail appear.

Finding the size of each circle
#

Now reverse the question: given the raven’s outline z(t)z(t), how do we find each cnc_n?

The key is a simple fact: an arrow that makes a whole number of turns averages to zero over one loop. It spends equal time pointing in every direction, and those directions cancel:

∫01e2πimt dt=0,m≠0.\int_0^1 e^{2\pi i m t}\, dt = 0, \qquad m \ne 0.

Suppose we want c3c_3. Multiplying the whole curve by e−2πi⋅3te^{-2\pi i \cdot 3t} makes every arrow turn three extra times in the opposite direction:

  • the n=3n = 3 arrow’s rotation is cancelled exactly, leaving c3c_3 standing still;
  • every other arrow now makes n−3n - 3 turns, still a non-zero whole number.

Averaging over one loop, every arrow that still turns averages to zero, and only the motionless c3c_3 remains. The same works for any nn:

cn=∫01z(t) e−2πint dt.c_n = \int_0^1 z(t)\, e^{-2\pi i n t}\, dt.

In practice a computer has samples, not a continuous curve. The homepage outline was traced from the ASCII raven itself and sampled at N=2048N = 2048 points z0,z1,…,zN−1z_0, z_1, \dots, z_{N-1} around the loop, the kk-th point at time t=k/Nt = k/N. Replacing “the average over one loop” with “the average of these 2,048 points” turns the integral into a sum:

cn=1N∑k=0N−1zk e−2πink/N.c_n = \frac{1}{N} \sum_{k=0}^{N-1} z_k\, e^{-2\pi i n k / N}.

This is the discrete Fourier transform (DFT). 2,048 points give 2,048 coefficients, and the fast Fourier transform (FFT) computes them all efficiently. This was done once, when the animation was made; your browser only reads the results.

Which circles to keep
#

Of the 2,048 possible terms, the homepage keeps 320. The choice is simple: sort by radius ∣cn∣|c_n| and keep the largest. The figure’s spectrum shows the same ordering.

The reason comes from Parseval’s theorem. If zKz_K keeps only some of the terms, the average squared distance between the two curves equals the total squared size of the terms that were dropped:

∫01∣z(t)−zK(t)∣2 dt=∑n dropped∣cn∣2.\int_0^1 \bigl|z(t) - z_K(t)\bigr|^2\, dt = \sum_{n \text{ dropped}} |c_n|^2.

Every dropped term adds its own ∣cn∣2|c_n|^2 to the error, and the terms do not interfere with each other. So for a fixed number of circles, keeping the largest ones gives the smallest average squared error. The figure’s RMS and energy readouts are computed directly from this identity.

Compared with the 320-term curve:

CirclesEnergy keptRMS gap (share of height)
186.62%15.4%
291.56%12.3%
395.69%8.8%
899.15%3.9%
3299.98%0.58%
96> 99.99%0.16%

Energy here means ∑∣cn∣2\sum |c_n|^2 over the rotating terms. The first eight circles already hold more than 99% of it. The remaining 1% is what separates a blob from a raven.

What the circles find hard
#

Smooth, gently curving stretches are cheap: low frequencies describe them well. Sharp corners such as the tip of the beak, the claws and the tail are expensive. A corner changes direction suddenly, and only fast-turning arrows can follow that. With few circles every corner is rounded off; more circles make the corners sharper.

That is why the spectrum has a long, slowly shrinking tail. Among the 32 largest terms, the fastest turns 25 times per loop. Among the 96 largest, it turns 68 times. Further down the list, some kept terms turn up to 1,023 times per loop, the highest frequency 2,048 samples can represent. Every term faster than 200 turns, however, has a radius under 0.004% of the raven’s height. They hardly change the picture.

From the formula to the homepage
#

The same mathematics shaped some engineering choices:

  • The homepage does not compute the series. A short script, generate_raven_path.py, adds up all 320 terms in advance and stores every point of the 2,048-sample loop as a 16-bit integer pair. The browser only decodes them.
  • The homepage draws at most 96 circles. Circles with a radius under 0.2 pixels cannot be seen. The outline still uses all 320 terms, so skipping tiny circles only simplifies the illustration.
  • This page computes the curve in your browser because you choose the number of circles. Recomputing 2,048 points from up to 320 terms takes no more than a few hundredths of a second.

How this website is built describes the site around these pieces.

Circles on circles
#

Drawing with circles on circles is an old idea. Ancient astronomers described planetary motion with epicycles: a small circle whose centre moves around a larger one. A Fourier series is the same construction with as many circles as needed. Given enough of them, it can follow almost any closed path. That flexibility explains both why epicycles could match observations well and why a good fit alone does not show that a model explains the motion.

The raven on the homepage is not a planet, but the principle is the same: 320 circles, each turning at its own constant speed, are enough to draw it.