# List Imports here:
Import Turtle
Import Random
Import Math
# List Constants Here (NO MAGIC numbers!):
Number_darts = 1000
Left_bound =-2
Right_bound = 2
Top_bound =-2
Bottom_bound = 2
Pen_size = 3
Square_width = 2
Square_top = 1
Square_left =-1
Dot_size = 5
# draws square given turtle, width and top left XY position
# param Grapher (Turtle)
# param width (int)
# param TOPLEFTX (int)
# param toplefty (int)
def drawsquare (Grapher, Width, topleftx, toplefty):
Grapher.penup ()
Grapher.setpos (TOPLEFTX, Toplefty)
Grapher.setheading (0)
Grapher.pendown ()
Grapher.forward (width)
Grapher.right (90)
Grapher.forward (width)
Grapher.right (90)
Grapher.forward (width)
Grapher.right (90)
Grapher.forward (width)
Grapher.penup ()
# draws line given turtle and endpoints
# param Grapher (Turtle)
# param xstart (int)
# param Ystart (int)
# param xEnd (int)
# param yend (int)
def drawLine (Grapher, Xstart, Ystart, XEnd, yend):
Grapher.penup ()
Grapher.setpos (Xstart, Ystart)
Grapher.setheading (0)
Angle_rad = Math.atan2 (Yend-ystart, Xend-xstart)
Angle = Angle_rad * 180.0/math.pi
Dist = MATH.SQRT ((yend-ystart) * (Yend-ystart) + (xend-xstart) * (Xend-xstart))
Grapher.pendown ()
Grapher.left (angle)
Grapher.forward (Dist)
Grapher.penup ()
# Draws a dot with given x, y coordinates
# param Grapher (Turtle)
# param x (double)
# param y (double)
# param color (string)
def drawdot (Grapher, x, y, color):
#grapher. PenColor (color)
Grapher.penup ()
Grapher.setpos (x, y)
Grapher.dot (dot_size, color)
Grapher.penup ()
# sets up 4×4 area with X-and y-axis to simulate dartboard
# param Board-window That'll simulate board
# param Grapher-turtle that would do drawing
def setupdartboard (board, Grapher):
# Set up 4×4 area and set Pensize
Board.setworldcoordinates (Left_bound, Top_bound, Right_bound, Bottom_bound)
Grapher.pensize (Pen_size)
# Draw Board
Drawsquare (Grapher, Square_width, Square_left, Square_top)
# Draw X-and y-axes
DrawLine (Grapher, Left_bound, 0, right_bound, 0)
DrawLine (grapher, 0, top_bound, 0, Bottom_bound)
# (predicate Function)
# Determines whether or not Dart falls inside unit circle with center at 0,0
# param Dart (Turtle)
# return True If in circle, False if not
def incircle (x, y):
If X*x + y*y <= 1:
Return True
Else
Return False
# Algorithm for Monte Carlo simulation
# param numberdarts (int)
# param Grapher (Turtle)
# return approximation of Pi (float)
def montepi (Numberdarts, Grapher):
# Initialize Incirclecount counter (this was an accumulator)
Incirclecount = 0
# Loop for Numberdarts
For I in range (0, numberdarts):
# Get random coordinate and position dart
x = Random.random () * 2-1
y = random.random () * 2-1
# Draw Red dot and INCREMENT COUNTER if in circle, blue dot if not
If InCircle (x, y) = = True:
Incirclecount = Incirclecount + 1
Drawdot (Grapher, X, Y, "red")
Else
Drawdot (Grapher, X, Y, "blue")
# return approximation of Pi
return float (incirclecount)/numberdarts * 4
#Performs Monte Carlo Simulation to generate approximation of Pi
def main ():
# Get number of darts for simulation from user
# Note Continuation character <\> so we don ' t go to over columns:
Print ("This was a program, simulates throwing darts at a dartboard\n" \
"In order to approximate pi:the ratio of darts in a unit circle\n" \
"To the total number of darts in a 2x2 square should be\n" \
"Approximately equal to Pi/4")
#Create window, turtle, set up window as Dartboard
window = Turtle. Screen ()
t = Turtle. Turtle ()
Setupdartboard (window, t)
PI = Montepi (number_darts, T)
# Include The following code in order to update animation periodically
# instead of for each throw:
Window.tracer (500)
#Conduct Simulation and print result
Print "PI is approximately equal to" + str (PI)
#Keep the window up until dismissed
Window.exitonclick ()
Main ()
:
Python Turtle,random,math