python turtle,random,math,,#Listimpor


#Listimportshere:

importturtle

importrandom

importmath

#Listconstantshere(NOMAGICNUMBERS!):

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

#Drawssquaregiventurtle,widthandtopleftXYposition

#paramgrapher(Turtle)

#paramwidth(int)

#paramtopLeftX(int)

#paramtopLeftY(int)

defdrawSquare(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()

#Drawslinegiventurtleandendpoints

#paramgrapher(Turtle)

#paramxStart(int)

#paramyStart(int)

#paramxEnd(int)

#paramyEnd(int)

defdrawLine(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()

#Drawsadotwithgivenx,ycoordinates

#paramgrapher(Turtle)

#paramx(double)

#paramy(double)

#paramcolor(string)

defdrawDot(grapher,x,y,color):

#grapher.pencolor(color)

grapher.penup()

grapher.setpos(x,y)

grapher.dot(DOT_SIZE,color)

grapher.penup()

#Setsup4X4areawithx-andy-axistosimulatedartboard

#paramboard-windowthatwillsimulateboard

#paramgrapher-turtlethatwilldodrawing

defsetUpDartboard(board,grapher):

#setup4X4areaandsetpensize

board.setworldcoordinates(LEFT_BOUND,TOP_BOUND,RIGHT_BOUND,BOTTOM_BOUND)

grapher.pensize(PEN_SIZE)

#Drawboard

drawSquare(grapher,SQUARE_WIDTH,SQUARE_LEFT,SQUARE_TOP)

#Drawx-andy-axes

drawLine(grapher,LEFT_BOUND,0,RIGHT_BOUND,0)

drawLine(grapher,0,TOP_BOUND,0,BOTTOM_BOUND)

#(PredicateFunction)

#Determineswhetherornotdartfallsinsideunitcirclewithcenterat0,0

#paramdart(Turtle)

#returnTrueifincircle,Falseifnot

definCircle(x,y):

ifx*x+y*y<=1:

returnTrue

else:

returnFalse

#AlgorithmforMonteCarlosimulation

#paramnumberDarts(int)

#paramgrapher(Turtle)

#returnapproximationofpi(float)

defmontePi(numberDarts,grapher):

#InitializeinCircleCountcounter(THISISANACCUMULATOR)

inCircleCount=0

#LoopfornumberDarts

foriinrange(0,numberDarts):

#Getrandomcoordinateandpositiondart

x=random.random()*2-1

y=random.random()*2-1

#DrawreddotANDINCREMENTCOUNTERifincircle,bluedotifnot

ifinCircle(x,y)==True:

inCircleCount=inCircleCount+1

drawDot(grapher,x,y,"red")

else:

drawDot(grapher,x,y,"blue")

#returnapproximationofpi

returnfloat(inCircleCount)/numberDarts*4

#PerformsMonteCarlosimulationtogenerateapproximationofPi

defmain():

#Getnumberofdartsforsimulationfromuser

#Notecontinuationcharacter<\>sowedon‘tgoover78columns:

print("Thisisaprogramthatsimulatesthrowingdartsatadartboard\n"\

"inordertoapproximatepi:Theratioofdartsinaunitcircle\n"\

"tothetotalnumberofdartsina2X2squareshouldbe\n"\

"approximatelyequaltopi/4")

#Createwindow,turtle,setupwindowasdartboard

window=turtle.Screen()

t=turtle.Turtle()

setUpDartboard(window,t)

PI=montePi(NUMBER_DARTS,t)

#Includethefollowingcodeinordertoupdateanimationperiodically

#insteadofforeachthrow:

window.tracer(500)

#Conductsimulationandprintresult

print"Piisapproximatelyequalto"+str(PI)

#Keepthewindowupuntildismissed

window.exitonclick()

main()

如图:bubuko.com,布布扣

python turtle,random,math

评论关闭