/* Demonstration of Buffon's needle */ int lineLength = 50; int gridWidth = 50; int numTries = 10000; color lineColor = #000000; color needleColor = #5555FF; void setup() { background(#FFFFFF); stroke(lineColor); size(325,325); for(int i = gridWidth; i < width; i = i + gridWidth) { line(i,0,i,height); } } int onLine = 0; int offLine = 0; void draw() { int x = (int)random(width); int y = (int)random(height); float theta = radians(random(360)); float opposite = sin(theta) * lineLength; float adjacent = sqrt(lineLength*lineLength-opposite*opposite); if(((x%gridWidth)+adjacent) > gridWidth) { stroke(#FF0000); onLine++; } else { offLine++; stroke(150); } line(x,y,x+adjacent,y+opposite); stroke(200); fill(#DDDDDD); PFont font = loadFont("Andalus-20.vlw"); textFont(font, 20); float ratio = float(onLine)/float(offLine) * 2.0; rect(20,20,140,75); fill(0); text(("Off lines: " + int(offLine) + "\nOn lines: " + int(onLine) + "\nRatio: " + nf(ratio,1,2)), 25, 25, 500,80); }