My contribution to the Pi-Day 2022
https://www.youtube.com/watch?v=VizcvCPUoo4
simplescreenrecorder-2023-04-11_14.25.34.mp4
The program uses SM3DWFRPG (Super Minimal 3D Wireframe Renderer in Pygame) you can find the documentation to that here
-
Clone the repository or download the source code
-
As the
pygame
pygame package is mandatory to launch the program you have to install that using e. g.pip install pygame
. -
To launch the program run
sphere_and_cube.py
It looks quite cool thats for sure. But how does it actually work? Why does the ratio between all points and the points in the sphere times 6 equal pi? I'm going to answer all of that here.
Imaging you are playing darts. You have a dart board and a bounding square behind it.
So now, let's throw 3 random darts.
As we can see, the 2 darts on the board are coloured green and the one outside is cyan. So now, let's imagine you are throwing infinitely many darts on this board (or the bounding square).
Now the whole area of the board if covered in green and the rest in cyan. So we can say that the amount of all darts in the circle if equal to the area of the circle which has the formula:
Applying this logic to every dart in the square (and the circle, because the circle is in the square) we can just use the formula for the area of a square which would in this case be:
So if we would now calculate the ratio between the area of the circle (points in the circle) and the area of the square (all points), we'd get this:
Now lets rearrange this formula with a multiplication by 4 so that pi is isolated:
Remember the 3 darts from earlier? We can use the above formula now to approximate pi:
If you understood the basics of the "Monte Carlo" method, bringing it to the third dimension is fairly easy. Instead of a circle and a bounding square, imagine their three-dimensional counterparts, a sphere and a cube.
Now we can use the formulas for the volume of a sphere and for the volume of a cube to calculate the ratio between these:
Applying this to an example with 2 points inside the sphere and 4 points in total we get: