Every year on March the 14th, for one day the world gets irrationally excited about the famous constant \pi. As is tradition, you try to calculate \pi in unusual ways, demonstrating the constant’s ubiquity as it crops up in the most unexpected circumstances.

               u$$mnn:       Y$$$i        .@$$$$$,                    
              $$n           )$$$*         W$$$$$m                     
             -n[            $$$$.        ]$$$$$$                      
                           h$$$w         $$$$$$Y                      
                          [$$$$         X$$$$$$                       
                         "$$$$n        '$$$$$${                       
                        .$$$$$         8$$$$$$                        
                        *$$$$}        :$$$$$$+                        
                       #$$$$u         $$$$$$%                         
                     t$$$$$$         ;$$$$$$`                         
                    u$$$$$$!         $$$$$$W                          
                   Y$$$$$$M         .$$$$$$,                          
                 f$$$$$$$$.         Z$$$$$Z          nn               
               `w$$$$$$$$|          $$$$$$(         v$z               
              n$$$$$$$$$W           $$$$$$$1      'X$8                
             Y$$$$$$$$$$            *$$$$$$$8nnnn$$$p                 
             $$$$$$$$$@.             W$$$$$$$$$$$$$n                  
             _$$$$$$${                x$$$$$$$$$0>                    
                -n{.                     !|nt_.                       

A fairly well-known way to approximate pi is to randomly choose points in a square (often thought of as throwing darts at a square piece of cardboard), determine their distance to a circle’s center and do a division, as I did in my π Generator post.

However, \pi does not only appear in the formula for a circle’s area, A=\pi \cdot r^2, yet also in the formula for a sphere’s volume, V=\frac{4}{3} \cdot \pi \cdot r^3, and for all the infinite hyperspheres above dimension three (view this Wikipedia article for more about volumes of higher-dimensional spheres).

In particular, the formula for the hypervolume of a hypersphere in four dimensions is defined as being V=\frac{\pi^2}{2} \cdot r^4. Using this formula, my Python script randomly chooses four-dimensional points (each in the interval \left[0, 1\right)), calculates their distance to the point \left(0.5, 0.5, 0.5, 0.5\right) and determines if they are in the hypersphere around that point with radius 0.5.
By dividing the number of random points which lie in the hypersphere by the number of iterations used (10^6 in the example below), the script approximates the hypersphere’s hypervolume. By then rearranging the equation V=\frac{\pi^2}{2} \cdot r^4 with r=0.5 to \pi=\sqrt{V\cdot 32}, the desired constant can be approximated.

$ python pi.py

# Python 2.7.7 Code
# Jonathan Frech, 13th of March 2017

Continue reading

Circle Walk II

Continuing the idea from ‘Circle Walk’, I created ‘Circle Walk II’. In this program the entities get – based on their spawn time – a number (just to see the exact spawn time). As they spawn, they get put in a list. From this list their position around the center is calculated (in a similar way as in ‘Polygons’). Their distance to the center equals five times the number of entities (\text{distance to the center} = 5 \cdot \text{number of entities}), but cannot reach outside the screen. Their color is calculated based on their angle.


  • ‘Space’ to toggle if text is shown

The First ExampleThe Second ExampleThe Third Example

# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 15th of March, 2015
#         edited 30th of March, 2015
#     version II 1st  of April, 2015

Continue reading


Using my ‘getCirclePos()’ function I thought of not only creating a circle. Dividing 360° by n, you get x°. Now x° is a n-th of a circle. If I connect n points with each other, always adding x° and starting from 0°, I get a n-gon. ‘Polygons’ does exactly this. The minimum n-gon-count is 3, the maximum 1000.


  • ‘w’ increases the polygon point count
  • ‘s’ decreases up the polygon point count
  • ‘up’ does what ‘w’ does, but you can hold it
  • ‘down’ does what ‘s’ does, but you can hold it
  • ‘r’ starts the random movement (polygon count changes randomly)


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 26th of March, 2015
#         edited 31st of March, 2015

Continue reading

Circle Walk

In an attempt to create systems with entities which influence each other I created ‘Circle Walk’. In this program you can spawn in entities which will go around the center with variant speed. When two entities get near each other, they will randomly change their distance to the center and eventually get away from one another.


  • Click with the left mouse button to spawn in a new entity

The First ExampleThe Second ExampleThe Third Example

# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 15th of March, 2015
#         edited 30th of March, 2015

Continue reading

π Generator

Using Python and Pygame I created a program which approximates π. On the square screen points will randomly appear. Based on the distance to the center the points get a different color. Divided into two groups (the red points and the gray points) the program can roughly estimate π.

How it calculates

The red points represent the circle area.
A' = \pi \cdot r^2
The gray points represent the square area minus the circle area. Thus the gray points and the red points the square area.
A'' = \big(2 \cdot r\big)^2 = 4 \cdot r^2
We get \frac{\pi}{4}, when we divide one by another.
\frac{A'}{A''} = \frac{\pi \cdot r^2}{4 \cdot r^2} = \frac{\pi}{4}
Lastly, we need to multiply by four to get \pi.
\frac{A'}{A''} \cdot 4 = \pi

Main Display Terminal Output

# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 29th of March, 2015

Continue reading