Approximating Pi has fascinated mathematicians and programmers alike for centuries. One particularly elegant approach is the Monte Carlo method, which uses randomness and geometry to estimate this famous constant.

Loading the Monte Carlo Pi Visualizer...

Recently, I started tinkering with SDL2 — a graphics rendering library written in C. Rendering visuals is an important part of understanding how an algorithm or dataset behaves. It also provides an intuitive way to create user interaction. While understanding graphics rendering concepts can be challenging, it is extremely satisfying once mastered.

In this post, I’ll showcase a program I built with SDL2 to visualize how the Monte Carlo method works. The program supports both native builds and WebAssembly, allowing it to run directly in your browser. Users can interact by clicking with the mouse — pausing and resetting the simulation.


Interacting with the Visualization

The Monte Carlo method is a probabilistic technique that relies on random sampling. Here’s a breakdown of the steps:

  1. Generate Random Points: Points are randomly distributed in a square.
  2. Check for Circle Inclusion: For each point, determine if it lies inside a circle inscribed within the square.
  3. Estimate Pi: The ratio of points inside the circle to the total number of points is used to approximate Pi:

    \[\pi \approx 4 \times \frac{\text{Points Inside Circle}}{\text{Total Points}}\]

This technique leverages the geometry of the circle and square, and as more points are sampled, the approximation improves.


How Does the Monte Carlo Method Approximate Pi?

Once the program is loaded, you will see a square with random points being generated. Each point is either inside the circle (blue) or outside (red). The ratio of blue points to the total points approximates Pi in real time.

You can:

  • Pause the Simulation: Left-clicking pauses if active and activates if paused.
  • Reset the Simulation: Right-click while in paused state to reset the simulation.

Building the Program

The program is written in C++ using SDL2 for graphics and text rendering. It supports both desktop builds with CMake and WebAssembly builds using Emscripten. Source files and further instruction for building the program can be found in my Github-repo.