Processing Pi

Processing Pi

Some of the first programs I wrote on the Apple II (in other words, some of the first programs I ever wrote), were different visualizable ways to calculate  pi , courtesy of a book on the history of pi that I found in the public library (between readings of scifi novels and Agatha Christie mysteries, my tastes were eclectic).

Recreating those programs hardly seemed worth all the setup that a simple Hello World requires these days, but I started playing with  Processing  the other day (another longstanding item on the checklist) and it seemed like the perfect platform for this type of thing.

There are many methods to choose from but probably the  simplest  is to essentially throw darts at a square enclosing a circle. The number of darts in the circle divided by the total number of darts in the square approximates the circle area by the square area and that all should converge toward pi/4.

In Processing, I just typed in this code, hit Play to run and display the window above, and hit Export to deploy it on . Maybe I'll try  Buffon's Needle , next!

float total = 0.0;
float inside = 0.0;
float radius = 640.0;

PFont f;

float pi() {
  if (total == 0) {
    return 0;
  } else {
  return inside/total *4;

void setup() {
  f = createFont("Arial",16,true);

void draw() {
  float x = random(-radius,radius);
  float y = random(-radius,radius);
  total = total +1;
  if (x*x+y*y < radius*radius) {
    inside = inside+1;

Subscribe to Technicat

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.