p5.js is JavaScript library aimed at simplifying canvas sketching and manipulation
As we can see from the demo, a practical square wave can be obtained from summing sine waves, and this approximation gets better with increase in number of terms. Try for 9 terms vs 999 terms in the demo.
The idea behind this is draw a bunch of random points on the canvas and count the number of points falling inside the circle. The ratio of the number points inside the circle with the total number of points is equal to the ratio of the area of circle with the area of the canvas i.e π / 4
In this method, two blocks, each with a mass ratio of 100^x, where x is an integer interact with each and a wall using elastic collision. The interesting fact is that the number of collisions gives us the digits in pi.
Demo available at gh-pages