标签:
python版本:3.4.3
使用模块:turtle math
代码如下:
#-*- charset:utf-8 -*- import turtle import math #绘制坐标轴,轴心(0,0) #绘制X轴 turtle.color("red") turtle.penup() turtle.goto(-200,0) turtle.pendown() turtle.forward(400) turtle.left(150) turtle.forward(10) turtle.penup() turtle.goto(200,0) turtle.left(60) turtle.pendown() turtle.forward(10) #绘制Y轴 turtle.penup() turtle.goto(0,-60) turtle.pendown() turtle.right(120) turtle.forward(120) turtle.left(150) turtle.forward(10) turtle.penup() turtle.goto(0,60) turtle.pendown() turtle.left(60) turtle.forward(10) turtle.penup() #利用循环实现函数值随着x递增而发生变化,将这些坐标值传入画笔坐标,使其绘图 #注意:在上一步要使penup(),在下面,坐标发生变化后,要使pendown() for x in range(-175,176): turtle.goto(x,50 * math.sin((x / 100) * 2 * math.pi)) turtle.pendown() turtle.penup() turtle.color("blue") for i in range(-175,176): turtle.goto(i,50 * math.cos((i / 100) * 2 * math.pi)) turtle.pendown() #下面两步写入-2π 和 2π turtle.penup() turtle.color("red") turtle.goto(-100,-15) turtle.pendown() turtle.write("-2\u03c0") turtle.penup() turtle.goto(100,-15) turtle.pendown() turtle.write("2\u03c0") turtle.hideturtle() turtle.done()
运行结果:
这个程序比较简单,需要注意的是math.sin(x)中x是弧度
标签:
原文地址:http://my.oschina.net/u/158964/blog/506867