天天看點

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