Rolando Andrade
Back to projects

Built in 2020

PiDay 2020

PI Number Visualization

PiDay 2020 preview

Visualization of the calculation of the PI number through the Leibniz series.

Start with 4. Subtract 4/3. Add 4/5. Subtract 4/7. Keep going forever, and the sum slowly closes in on pi. Gottfried Wilhelm Leibniz found this series in 1676.

Pi shows up in circles, waves, probability and quantum mechanics. Mathematicians have tried to calculate it precisely for thousands of years. Ancient civilizations estimated it. Modern computers find trillions of digits. Some of the methods are surprisingly simple, and the Leibniz series is one of them.

I built this project for Pi Day 2020, which is March 14. I wanted to show the series closing in on pi as a picture, not only as numbers. You watch the estimate swing above and below pi, getting a little closer each step.

A graph that swings

The page draws a live line graph on an HTML Canvas, the browser’s drawing surface. The vertical axis is the current estimate of pi. The horizontal axis counts the steps. Every 200 milliseconds, the code calculates a new term and plots it.

At first the line jumps wildly. It goes above pi, then below, then above again. Each swing is smaller than the last. The graph flattens toward a horizontal line at the true value. The current estimate also appears as a number at the bottom of the screen.

Pi Day 2020 rolandoandrade.github.io

Four over odd numbers

The pattern is simple. Each term is 4 divided by an odd number. The signs take turns: plus, minus, plus, minus. Written out, it looks like this: 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11...

The code tracks three values:

  1. The running sum, which starts at 4, the first term.
  2. The denominator, which starts at 3 and grows by 2 each step.
  3. The step count, which decides whether to add or subtract. Even steps subtract. Odd steps add.

Each calculation updates all three values and returns the new estimate. The simplicity is the point. Four lines of math produce endless precision, given enough time.

Drawing on the canvas

The graph is a 400 by 400 pixel canvas. Each new value goes into a history array. When the graph is drawn, the space between points depends on how many values exist. Early steps spread wide. Later steps squeeze together.

The vertical scale is fixed. The canvas height divided by 5 sets the size of one unit. Pi is about 3.14, so it sits comfortably inside the visible area. The starting value of 4 appears near the top. The swings toward pi draw the curve you see.

Each frame is drawn in layers:

  1. A dark gray rectangle clears the canvas.
  2. The current value appears as text.
  3. The history line is drawn piece by piece, joining each point to the next.

Slow is good here

The Leibniz series is famous for being slow. After 1000 steps, you get maybe 3 correct digits. Other algorithms reach billions of digits in the same time. But a slow series makes a better picture.

A fast algorithm would hit pi almost at once. The line would jump straight to the answer, with no swings to watch. The Leibniz series takes its time. You can follow the math frame by frame.

The slowness also shows an important idea. An infinite series can close in on an exact value without ever reaching it. Each term gets you closer. No term gets you there. The graph turns that abstract idea into something you can see.

Two steps per tick

The animation runs on a fixed timer. Every 200 milliseconds, the code calculates and redraws. That pace is fast enough to show progress and slow enough to follow with your eyes.

Each tick does just two things. It calculates the next term. Then it plots the updated history. A shapes library handles the drawing basics. Lines join the data points. Rectangles clear the background and hold the text.

Simple formulas, real insight

Four lines of arithmetic estimate one of the most important constants in nature. For this project, the beauty of the formula mattered more than its speed.

Watching a series close in on a value is different from reading about it. You see the swings shrink. You see the line settle near pi.

You also do not need a library for many visualizations. The canvas gives you lines, rectangles, text and circles. Its 2D drawing API is simple and fast, and it was enough to draw pi.

Interested in working together? Check out my services and pricing to see how we can collaborate on your next project. Let's Build