A simple approximation of the circle constant pi using the "monte carlo" method.
My implementaion is simple I use an modified verion of rand()
,just that I get an double within an range, to drop my points.
My calculation in approximatePI()
is:
(pointsInCircle * pointsTotal) / 4
It's simple we throw dots on an imaginary square with an cirlce in it (with a = r^2
) and count how many dots are in the circle and how many we have in total. Than we do the calculation from above.
The determination rater the dot is in the circle or not is done by using Pythagoras rule a^2 + b^2 = c^2 to calculate the distance between the centre and the point. If the result is greater than the radius its not in the circle.
The fRand()
function is from stackoverflow user Frank posted in https://stackoverflow.com/questions/2704521/generate-random-double-numbers-in-c
I use the BigFloat - Arbitrary-Precision Float Arithmetic Library by Gianfranco Mariotti
I got the idea to do an c++ implementation of an aproximation of pi because of an video by CodingTrain. I wrote some of the code simular to the one in video, so that is also not quite my stuff.