天天看点

cp9_2_1.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : cp9_2_1.py
# @Author: WRH
# @Date  : 2021/6/7
# @Edition:Python3.8.6

# 9.2  turtle库的图形绘制方法
# turtle也是内置库Python图形绘制库,其绘制方法更为简单,原理如同控制一只“小龟”以不同的方向和速度进行位移而得到其运动轨迹
# 9.2.1  turtle绘图的基本方法
'''
1.坐标位置和方向
setup()方法用于初始化画布窗口大小和位置,参数包括画布窗口宽、画布窗口高、窗口在屏幕的水平起始位置和窗口在屏幕的垂直起始位置
用turtle创建的画布与Canvas不同,其原点(0,0)在画布的中心,坐标方向与数学定义一致,向右、向上为正。
2.画笔
方法color()用于设置或返回画笔颜色
方法pensize()或width()用于设置笔触粗细
3.画笔控制和运动
方法penup()、pu()或up()为抬笔,当笔触移动时不留墨迹;方法pendown(),pd()或down()为落笔,当笔触移动时会留下墨迹。
画笔的移动方法有:向箭头所指方向前进forward()、fd();逆箭头所指方向后退backward(),bk()或back()。
画笔的原地转角方法有:箭头方向左转left()或lt();箭头方向右转right()或rt()。
位移至某点的方法:goto(),setpos()或setposition();画圆的方法:circle();返回原点的方法:home()。
位移速度方法speed(),其取值范围从慢到快为1~10。注意:取0为最快(无移动过程,直接显示目标结果)。
绘图完毕通常用方法done()结束进程。
4.文字
输出文字标签用write()方法,默认参数为输出文本,可选参数有:对齐方式align(left,center,right),
font元组型字体设置(字体、字号、字形)
'''

# 1.绘制基本图形
# 例9-8
'''
从原点出发至坐标点(-100,100),改为红色,沿光标指向(默认方向为水平向右)前进200像素,改为蓝色,后退100像素,
以动画模式输出文字(黑体,36磅,斜体)。
'''
import turtle
turtle.setup(640,480,300,300) # 设置绘图窗体,四个参数一次是宽、高、窗体左侧相对于屏幕左侧的像素距离,窗体顶部与屏幕顶部的像素距离
turtle.reset() # turtle重置,回到画布初始状态
turtle.pensize(5)
turtle.goto(-100,100)
turtle.color('red')
turtle.fd(200)
turtle.color('blue')
turtle.bk(100)
turtle.write('turtle绘图',move=True,font=('黑体',36,'italic')) # italic斜体
turtle.done()

# 例9-9
'''
以5像素笔触重复执行“前进100像素,右转60度”的操作共6次,绘制红色正六边形;再用circle()
方法画半径为60像素的红色圆内接正六边形;然后抬笔移动至(-50, 200)点落笔,
重复执行“右转144度,前进400像素”的操作共5次,绘制五角星。
'''
from turtle import *
reset()
pensize(5)
# 画正六边形,每步右转60度
for i in range(6):
    fd(100)
    right(60)
# 用circle方法画正六边形(半径为60像素的圆内接正六边形)
color('red')
circle(60,steps=6)
# 抬笔移动位置
up()
goto(-50, 200)
down()
# 画五角星,每步右转144度
for i in range(5):
    right(144)
    fd(400)
done()

# 例9-10 螺纹形状绘制
import turtle as tt
tt.speed(50)
tt.pencolor('blue')
for i in range(100):
    tt.circle(2*i)
    tt.right(4.5)

# 例9-11 绘制小猪佩奇
import turtle
t = turtle.Turtle()
t.pensize(4) #设置画笔粗
t.color("pink")
turtle.setup(400,400) #设置主窗口的大小
t.speed(20) #设置画笔速度
#画鼻子
t.pu() #提笔
t.goto(-100,0) #画笔移至坐标(-100,100)
t.pd() # 落笔
t.seth(-30) #沿角度-30°
lengh=0.4
for i in range(120):
   if 0<=i<30 or 60<=i<90:
       lengh=lengh+0.08
       t.lt(3) #向左转3度
       t.fd(lengh) #向前移动lengh
   else:
       lengh=lengh-0.08
       t.lt(3)
       t.fd(lengh)
t.pu() # 提笔
t.seth(90) #沿角度90度
t.fd(25) # 向前移动25
t.seth(0) #沿角度0
t.fd(10)
t.pd()
t.pencolor("pink")
t.seth(10)
t.circle(5) # 画一个半径为5的圆
t.pu()
t.seth(0)
t.fd(20)
t.pd()
t.pencolor("pink")
t.seth(10)
t.circle(5)
#画头
t.color("pink")
t.pu()
t.seth(90)
t.fd(41)
t.seth(0)
t.pd()
t.seth(180)
t.circle(300,-30) #画一个半径为300,圆心角为-30°的弧
t.circle(100,-60)
t.circle(80,-100)
t.circle(150,-20)
t.circle(60,-95)
t.seth(161)
t.circle(-300,15)
t.pu()
t.goto(-80,70)
#画耳朵
t.color("pink")
t.pu()
t.seth(90)
t.fd(-7)
t.seth(0)
t.fd(70)
t.pd()
t.seth(100)
t.circle(-50,50)
t.circle(-10,120)
t.circle(-50,54)
t.pu()
t.seth(90)
t.fd(-12)
t.seth(0)
t.fd(30)
t.pd()
t.seth(100)
t.circle(-50,50)
t.circle(-10,120)
t.circle(-50,56)
#画眼睛
t.color("pink")
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-95)
t.pd()
t.circle(15)
t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.circle(3)
t.color("pink")
t.pu()
t.seth(90)
t.fd(-25)
t.seth(0)
t.fd(40)
t.pd()
t.circle(15)
t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()
#画腮红
t.color("pink")
t.pu()
t.seth(90)
t.fd(-95)
t.seth(0)
t.fd(65)
t.pd()
t.begin_fill()
t.circle(30)
t.end_fill()
#画嘴
t.color("pink")
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(-100)
t.pd()
t.ht()  #不显示箭头
t.seth(-80)
t.circle(30,40)
t.circle(40,80)

# 2.绘制函数图形
# 例9-12
'''
创建800×800的turtle画布,以画布中心为原点画出坐标轴,并按以下公式绘制函数曲线:
x=(w0/4)×(-2sint+sin2t)
y=(h0/4)×(2cost-cos2t)
式中,w0是画布宽的一半,h0是画布高的一半。t的取值范围为0~2,步长为0.01。
'''
import math
import turtle
# 自定义从(x1, y1)到(x2, y2)的画直线函数
def drawLine (ttl, x1, y1, x2, y2):
    ttl.penup()
    ttl.goto (x1, y1)
    ttl.pendown()
    ttl.goto (x2, y2)
    ttl.penup()
# 逐点计算函数坐标,并按此移动
def drawFunc (ttl, begin, end, step, w0, h0):
    t=begin
    while t < end:
        if t>begin:
            ttl.pendown()
        x = (w0/4)*(-2*math.sin(t)+math.sin(2*t))
        y = (h0/4)*(2*math.cos(t)-math.cos(2*t))
        ttl.goto (x, y)
        t +=  step
    ttl.penup()
def main():
    # 设置画布窗口大小
    turtle.setup (800, 800, 0, 0)
    # 创建turtle对象
    ttl = turtle.Turtle()
    # 画坐标轴
    drawLine (ttl, -400, 0, 400, 0)
    drawLine (ttl, 0, 400, 0, -400)
    # 画函数曲线
    ttl.pencolor ('red')
    ttl.pensize(5)
    drawFunc (ttl, 0, 2*math.pi, 0.01,400,400)
    # 对象,起点,终点,步长,半宽,半高
    # 绘图完毕
    turtle.done()
if __name__ == "__main__":
    main()

# 3.数据可视化
# 例9-13 读取“ecgdata.txt”的心电图数据,绘制心电图。
import turtle
turtle.setup(500,600) #设置主窗口的大小
turtle.speed(20) #设置画笔速度
turtle.pu() #提笔
x=-300
turtle.goto(x,0) #画笔移至坐标
turtle.pd() # 落笔
ecg=list(open('ecgdata.txt','r'))
t=0
while t<len(ecg)-1:
    turtle.goto(t-300,int(ecg[t]))
    t+=1