python turtle,random,math
# 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 4X4 area with x- and y-axis to simulate dartboard
# param board - window that will simulate board
# param grapher - turtle that will do drawing
def setUpDartboard( board, grapher ):
# set up 4X4 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 IS 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 over 78 columns:
print("This is a program that 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的更多相关文章
- 【转载】python 模块 - random生成随机数模块
随机数种子 要每次产生随机数相同就要设置种子,相同种子数的Random对象,相同次数生成的随机数字是完全相同的: random.seed(1) 这样random.randint(0,6, (4,5)) ...
- python使用random函数生成随机数
python使用random函数来生成随机数,常用的方法有: import random #生成0-1之间的数(不包括0和1) random.random() #生成1-5之间的随机整数(包括1和5) ...
- python+turtle 笔记
用Python+turtle绘制佩琪: from turtle import * def nose(x,y):#鼻子 penup()#提起笔 goto(x,y)#定位 pendown()#落笔,开始画 ...
- 从Scratch到Python——python turtle 一种比pygame更加简洁的实现
从Scratch到Python--python turtle 一种比pygame更加简洁的实现 现在很多学校都开设了Scratch课程,学生可以利用Scratch创作丰富的作品,然而Scratch之后 ...
- 让小乌龟可以唱歌——对Python turtle进行拓展
在Scratch中,小猫是可以唱歌的,而且Scratch的声音木块有着丰富的功能,在这方面Python turtle略有欠缺,今天我们就来完善一下. Python声音模块 Python处理声音的模块很 ...
- Python turtle绘制阴阳太极图代码解析
本文详细分析如何使用Python turtle绘制阴阳太极图,先来分解这个图形,图片中有四种颜色,每条曲线上的箭头表示乌龟移动的方向,首先从中心画一个半圆(红线),以红线所示圆的直径作半径画一个校园, ...
- 【转】python之random模块分析(一)
[转]python之random模块分析(一) random是python产生伪随机数的模块,随机种子默认为系统时钟.下面分析模块中的方法: 1.random.randint(start,stop): ...
- python的random()函数
python 的random函数需要调用 #!/usr/bin/python # -*- coding: UTF-8 -*- import random print( random.randint(1 ...
- Note of Python Turtle
Note of Python Turtle Turtle 库函数是 Python语言中一个流行的绘图函数库.Turtle 意思是海龟,在Python中显示为一个小箭头,通过它的移动而留 ...
随机推荐
- [置顶] ARM指令集和常用寄存器
1) ARM指令集 32位的 ARM指令和 16位 的Thumb指令 1,寄存器寻址 MOV R1, R2 //将寄存器R2的值传给寄存器R1 2,立即寻址 MOV R0, #0XFF00 //数据 ...
- C语言 对数组名取地址
作者 : 卿笃军 你有没有想过,对一个一维数组名取地址,然后用这个地址进行加减运算.这会出现什么样的结果呢? 演示样例: int a[5] = {1,2,3,4,5}; int *p = (int * ...
- mysql数据库的高可用方法总结
高可用架构对于互联网服务基本是标配,无论是应用服务还是数据库服务都需要做到高可用.虽然互联网服务号称7*24小时不间断服务,但多多少少有一 些时候服务不可用,比如某些时候网页打不开,百度不能搜索或者无 ...
- python模块基础之getpass模块
getpass模块提供了可移植的密码输入,一共包括下面两个函数: 1. getpass.getpass() 2. getpass.getuser() getpass.getpass([prompt[, ...
- Java初转型-jdk安装和配置
Java 开发环境配置 > * 下载JDK> * 配置环境变量> * 测试JDK是否安装成功> * 使用 Eclipse 运行第一个 Java 程序 下载JDK 首先我们需要下 ...
- Function.prototype.bind
解析Function.prototype.bind 简介 对于一个给定的函数,创造一个绑定对象的新函数,这个函数和之前的函数功能一样,this值是它的第一个参数,其它参数,作为新的函数的给定参数. b ...
- (转)使用 .NET 的 RNGCryptoServiceProvider 生成随机数
1. [代码]一个简单的方法,但不够可靠 跳至 [1] [2] [全屏预览] ? 1 2 3 4 5 6 7 8 9 10 11 static void Main(string[] args) ...
- 深入理解REST与Servlet架构的区别
本身这个比较是个伪命题,因为 RESTful Service是一个软件架构“风格”, 而servlet是java 服务端的一种技术 之所以把它们拿出来比较,是由于它们代表了两个时代的技术风格与架构.下 ...
- seajs原理解析
一: 1.本文是基于seajs2.2.1编写的,之后版本应该大同小异 2.本文仅代表个人观点,如有理解错误,敬请指出,大家一起学习 二: 1.首先放一张我画的流程图 这是我理解的seajs的基本的所有 ...
- 第1章 网络编程基础(2)——Socket编程原理
Socket编程原理 Socket是网络通信端点的一种抽象,它提供了一种发送和接收数据的机制. 流socket(SOCK_STREAM):双向.有序.无重复.并且无记录边界 数据报Socket(SOC ...