# Tweetcart: Pi Day 2019

For Pi Day (3/14) of 2019, I decided to create a tweetcart showcasing the Monte Carlo method of approximating pi. This method calculates the distance between the origin and a random point from (-r,-r) to (r,r), where r is the radius (in this case, 1). Then, the total number of dots within the circle (or, within a distance of r) is divided by the total number of dots. Multiply that number by 4, and you get pi!

Here, I only render a fourth of the circle; it works the same way. Every frame, the program places 20 random dots on the canvas. If a dot is within 1 unit of the origin, then it becomes blue; else, it becomes red. Pi is 4 times the number of blue dots over the number of total dots. Pico-8 uses a 16-bit fixed-point type for its numbers, though, so it’s not particularly accurate; still, I enjoyed learning about and demonstrating this method!

A tweetcart is a Lua script that can run in Pico-8 and fit within a single Tweet, with 280 characters or fewer.

 ``````--#pico8 #tweetcart cls(15)n=0z=0 ?0,27,92,0 ?1,27,32 ?0,32,99 ?1,94,99 ::_:: for i=1,20 do x=rnd()y=rnd()color(8)d=sqrt(x^2+y^2) if(d<1)color(12)z+=1 pset(32+x*64,96-y*64)n+=1 if(n<0)z/=32767n=1 end rect(32,32,96,96,0)rectfill(32,24,99,28,15) ?'pi~'..4*(z/n),32,24,0 flip()goto _``````