Estimate Pi by simulating dropping needles onto evenly spaced lines

A quick explaination:
matches on a piece of paper
The explaination is in the text below the image.

Wikipedia’s entry on Buffon’s needle problem can be found here

My code is on my github, in my Panda3D-snippets
Do also copy the assets folder, otherwise it will panic about not finding the font.

The Panda3D code is fairly simple. A lot of it is just interface stuff for the onscreen text and slidey bars in the menu.

At it’s core the actual estimating of Pi consists of placing the lines (Collision Polyons) and then cycling through the same three tasks.

A task to randomly drop the needles. The important part here is to take the distance beween the lines into account, you cant just put them all in [-3, 3]. You need to do something like [-distance, distance]. And dont go past the CollisionPolygons that form the lines.

A task to count the needles that touch a line. This is achieved with a traverse(render) and count of the number of collision entries.

And finally, a task that waits a bit and then cleans up so the framerate stays good. The needles are removed and clearColliders() is called on the traverser. The wait time is just so you get to see the needles before they are removed. You could simulate faster without the visuals but then it’s quite dull to look at.

edit:
adding an explaination of how it works for those who dont want to open another site.
Buffon was a mathematician with a floor with evenly sized planks of wood. One day he wondered what the odds would be for a dropped needle to touch one of those evenly spaced lines. That problem was later solved and it’s formula, for needles with a length equal or shorter than the distance between the lines uses Pi.
Math being math, this means that if you rearrange that formula you now got a method to estimate Pi by dropping needles (or matches or spoons) onto evenly spaced lines and I think that’s quite neat.

I decided to make it in Panda3D mostly because it seemed like a good exercise.

1 Like

Of course, I love computational science, but this is just fictional research by scientists, although someone may have made a number of important discoveries. But as for this example, has it been investigated with all the physical factors? For example, the gas medium, the force of attraction, the method of throwing, the height of the initial position, and so on.