buffon.py

Created by mobluse

Created on September 15, 2024

2.43 KB

Buffon’s needle problem to estimate pi from “CE Python Turtle Getting Started” by Texas Instruments for TI-84 Plus CE modified to run on NumWorks.


# Buffon's needle problem to estimate pi
# from "CE Python Turtle Getting Started"
# modified to work on NumWorks.
#from ti_system import *
from random import *
from math import pi
from time import sleep

# use shell before entering turtle environment
#disp_clr()
length=int(input("Length of needle? (50) ") or '50')
spacing=int(input("Spacing among lines? (50) ") or '50')
needles=int(input("How many needles? (21) ") or '21')
the_pies=[]
#the_count=[]
# set up turtle environment
import turtle as t;#t=Turtle()
#t.hidegrid()
t.hideturtle()
t.pensize(2)
t.speed(0)
w,h=320,210

the_lines=[] # list of all x-coordinate of all lines
n_lines=int((w/spacing)/2)+2 # number of lines
crossing=0 # needles crossing a line
estimate=0 # calculated estimate of pi
hundreds=100
# draw all of the lines and append the_lines
for x in range(-n_lines*spacing,(n_lines+1)*spacing,spacing):
  the_lines+=[x] # Used .append()
  t.penup()
  t.goto(x,-h/2)
  t.pendown()
  t.goto(x,h/2)

print("len(the_lines)=%d"%len(the_lines))
# draw the needles
for i in range(needles+1):
  x1=randint(0,w)-w//2
  y1=randint(0,h)-h//2
  angle=random()*360
  t.penup()
  t.goto(x1,y1)
  t.setheading(angle)
  t.forward(length)
  #x2=t.xcor()
  #y2=t.ycor()
  x2,y2=t.position()
  t.pencolor(255,0,0)
# check if needle touches or crosses a line
  for n in range(len(the_lines)):
    if((x1<=the_lines[n]<=x2) or \
       (x2<=the_lines[n]<=x1)):
      t.pencolor(0,255,0)
      crossing+=1
  t.pendown()
  t.goto(x1,y1)
# Buffon's formula
  try: 
    estimate=(2*length*i)/(crossing*spacing)
  except:
# all fun and games until somone divides by zero
    pass
  the_pies+=[estimate] # Used .append()
#  the_count+=[i] # Used .append()
  if i>=hundreds:
    print("(%d,%.5G),"%(i,estimate))
    hundreds+=100
#store_list("1",the_count)
#store_list("2",the_pies)
error=(pi-estimate)*100/pi
sleep(2)

#disp_at(7,"    Press [clear] to continue","left")
#disp_wait()
#disp_clr()
t.penup()
t.goto(-w//2+10,-h//2)
t.pencolor("blue")
t.write("Press Back/Esc to continue!")
print("\nEstimate of \n  pi =",estimate,"\n  Error =",error,"%")
print("\nCopy the values of the\nfollowing list to the" \
      "\nCalculation, Regression or Grapher apps:")
print("the_pies")
print(end="{")
for i in range(len(the_pies)-1):
  print(end="%.5G,"%the_pies[i])
print("%.5G}"%the_pies[-1])
print(end="{")
for i in range(len(the_pies)-1):
  print(end="(%d,%.5G),"%(i,the_pies[i]))
print("(%d,%.5G)}"%(len(the_pies)-1,the_pies[-1]))

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.