PI is something that you're going to come across in creative coding. Most of us will have come across it in school. But what exactly is it?
// returns 3.141592653589793
That's great, but what is it for? It's all to do with circles.
PI is a ratio. The ratio between the diameter of a circle and its diameter. Meaning that to get the circumference you need to multiply the diameter by 3.141592653589793.
Can we prove that with code?
Yes we can!
If PI the ratio between the diameter and the circumference, that's the same ratio between the radius and the circumference of a semicircle, because they're both half as big.
We can use that to prove PI. To do this, we count the length of equal sized lines used to mimic the shape of a semi circle. Then get the ratio of the total length of those lines to the radius of the circle.
This is an interactive demo, try it out on the site to see it in action!
If you play with this demo you'll notice two things as we add more lines. First, it starts to look more like a semicircle. Second, the change to the ratio number is getting smaller and smaller. That means that, as we add more lines, we get closer to PI.
By the time we hit x100 lines we see very little change in the ratio number, and we have PI accurate to four decimal places!
If you enjoyed this article, sign up here to get new articles whisked straight to your inbox!