最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python內(nèi)置模塊turtle繪圖詳解

 更新時(shí)間:2017年12月09日 16:52:42   作者:知行流浪  
這篇文章主要介紹了Python內(nèi)置模塊turtle繪圖詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下。

urtle庫是Python語言中一個(gè)很流行的繪制圖像的函數(shù)庫,想象一個(gè)小烏龜,在一個(gè)橫軸為x、縱軸為y的坐標(biāo)系原點(diǎn),(0,0)位置開始,它根據(jù)一組函數(shù)指令的控制,在這個(gè)平面坐標(biāo)系中移動(dòng),從而在它爬行的路徑上繪制了圖形。

turtle繪圖的基礎(chǔ)知識(shí):

1.畫布(canvas)

畫布就是turtle為我們展開用于繪圖區(qū)域,我們可以設(shè)置它的大小和初始位置。

設(shè)置畫布大小

turtle.screensize(canvwidth=None,canvheight=None,bg=None),參數(shù)分別為畫布的寬(單位像素),高,背景顏色。

如:turtle.screensize(800,600,"green")

turtle.screensize()#返回默認(rèn)大小(400,300)

turtle.setup(width=0.5,height=0.75,startx=None,starty=None),參數(shù):width,height:輸入寬和高為整數(shù)時(shí),表示像素;為小數(shù)時(shí),表示占據(jù)電腦屏幕的比例,(startx,starty):這一坐標(biāo)表示矩形窗口左上角頂點(diǎn)的位置,如果為空,則窗口位于屏幕中心。

如:turtle.setup(width=0.6,height=0.6)

turtle.setup(width=800,height=800,startx=100,starty=100)

2.畫筆

2.1畫筆的狀態(tài)

在畫布上,默認(rèn)有一個(gè)坐標(biāo)原點(diǎn)為畫布中心的坐標(biāo)軸,坐標(biāo)原點(diǎn)上有一只面朝x軸正方向小烏龜。這里我們描述小烏龜時(shí)使用了兩個(gè)詞語:坐標(biāo)原點(diǎn)(位置),面朝x軸正方向(方向),turtle繪圖中,就是使用位置方向描述小烏龜(畫筆)的狀態(tài)。

2.2畫筆的屬性

畫筆(畫筆的屬性,顏色、畫線的寬度等)

1)turtle.pensize():設(shè)置畫筆的寬度;

2)turtle.pencolor():沒有參數(shù)傳入,返回當(dāng)前畫筆顏色,傳入?yún)?shù)設(shè)置畫筆顏色,可以是字符串如"green","red",也可以是RGB3元組。

3)turtle.speed(speed):設(shè)置畫筆移動(dòng)速度,畫筆繪制的速度范圍[0,10]整數(shù),數(shù)字越大越快。

2.3繪圖命令

操縱海龜繪圖有著許多的命令,這些命令可以劃分為3種:一種為運(yùn)動(dòng)命令,一種為畫筆控制命令,還有一種是全局控制命令。

(1)畫筆運(yùn)動(dòng)命令

命令

說明

turtle.forward(distance)

向當(dāng)前畫筆方向移動(dòng)distance像素長度

turtle.backward(distance)

向當(dāng)前畫筆相反方向移動(dòng)distance像素長度

turtle.right(degree)

順時(shí)針移動(dòng)degree°

turtle.left(degree)

逆時(shí)針移動(dòng)degree°

turtle.pendown()

移動(dòng)時(shí)繪制圖形,缺省時(shí)也為繪制

turtle.goto(x,y)

將畫筆移動(dòng)到坐標(biāo)為x,y的位置

turtle.penup()

提起筆移動(dòng),不繪制圖形,用于另起一個(gè)地方繪制

turtle.circle()

畫圓,半徑為正(負(fù)),表示圓心在畫筆的左邊(右邊)畫圓

setx( )

將當(dāng)前x軸移動(dòng)到指定位置

sety( )

將當(dāng)前y軸移動(dòng)到指定位置

setheading(angle)

設(shè)置當(dāng)前朝向?yàn)閍ngle角度

home()

設(shè)置當(dāng)前畫筆位置為原點(diǎn),朝向東。

dot(r)

繪制一個(gè)指定直徑和顏色的圓點(diǎn)

(2) 畫筆控制命令

命令

說明

turtle.fillcolor(colorstring)

繪制圖形的填充顏色

turtle.color(color1, color2)

同時(shí)設(shè)置pencolor=color1, fillcolor=color2

turtle.filling()

返回當(dāng)前是否在填充狀態(tài)

turtle.begin_fill()

準(zhǔn)備開始填充圖形

turtle.end_fill()

填充完成

turtle.hideturtle()

隱藏畫筆的turtle形狀

turtle.showturtle()

顯示畫筆的turtle形狀

(3) 全局控制命令

命令

說明

turtle.clear()

清空turtle窗口,但是turtle的位置和狀態(tài)不會(huì)改變

turtle.reset()

清空窗口,重置turtle狀態(tài)為起始狀態(tài)

turtle.undo()

撤銷上一個(gè)turtle動(dòng)作

turtle.isvisible()

返回當(dāng)前turtle是否可見

stamp()

復(fù)制當(dāng)前圖形

turtle.write(s [,font=("font-name",font_size,"font_type")])

寫文本,s為文本內(nèi)容,font是字體的參數(shù),分別為字體名稱,大小和類型;font為可選項(xiàng),font參數(shù)也是可選項(xiàng)

(4) 其他命令

命令

說明

turtle.mainloop()或turtle.done()

啟動(dòng)事件循環(huán) -調(diào)用Tkinter的mainloop函數(shù)。

必須是烏龜圖形程序中的最后一個(gè)語句。

turtle.mode(mode=None)

設(shè)置烏龜模式(“standard”,“l(fā)ogo”或“world”)并執(zhí)行重置。如果沒有給出模式,則返回當(dāng)前模式。

模式

初始龜標(biāo)題

正角度

standard

向右(東)

逆時(shí)針

logo

向上(北)

順時(shí)針

turtle.delay(delay=None)

設(shè)置或返回以毫秒為單位的繪圖延遲。

turtle.begin_poly()

開始記錄多邊形的頂點(diǎn)。當(dāng)前的烏龜位置是多邊形的第一個(gè)頂點(diǎn)。

turtle.end_poly()

停止記錄多邊形的頂點(diǎn)。當(dāng)前的烏龜位置是多邊形的最后一個(gè)頂點(diǎn)。將與第一個(gè)頂點(diǎn)相連。

turtle.get_poly()

返回最后記錄的多邊形。

3.命令詳解

3.1turtle.circle(radius,extent=None,steps=None)

描述:以給定半徑畫圓

參數(shù):

radius(半徑):半徑為正(負(fù)),表示圓心在畫筆的左邊(右邊)畫圓;

extent(弧度)(optional);

steps(optional)(做半徑為radius的圓的內(nèi)切正多邊形,多邊形邊數(shù)為steps)。

舉例:

circle(50)#整圓;

circle(50,steps=3)#三角形;

circle(120,180)#半圓

4.實(shí)例

1、太陽花

# coding=utf-8 
import turtle 
import time 
 
# 同時(shí)設(shè)置pencolor=color1, fillcolor=color2 
turtle.color("red", "yellow")  
turtle.begin_fill() 
for _ in range(50): 
turtle.forward(200) 
turtle.left(170) 
turtle.end_fill() 
turtle.mainloop() 

2、五角星

# coding=utf-8 
import turtle 
import time 
 
turtle.pensize(5) 
turtle.pencolor("yellow") 
turtle.fillcolor("red") 
 
turtle.begin_fill() 
for _ in range(5): 
 turtle.forward(200) 
 turtle.right(144) 
turtle.end_fill() 
time.sleep(2) 
  
turtle.penup() 
turtle.goto(-150,-120) 
turtle.color("violet") 
turtle.write("Done", font=('Arial', 40, 'normal')) 
 
turtle.mainloop() 

3、時(shí)鐘程序

# coding=utf-8 
import turtle 
from datetime import * 
# 抬起畫筆,向前運(yùn)動(dòng)一段距離放下 
def Skip(step): 
  turtle.penup() 
  turtle.forward(step) 
  turtle.pendown() 
def mkHand(name, length): 
  # 注冊(cè)Turtle形狀,建立表針Turtle 
  turtle.reset() 
  Skip(-length * 0.1) 
  # 開始記錄多邊形的頂點(diǎn)。當(dāng)前的烏龜位置是多邊形的第一個(gè)頂點(diǎn)。 
  turtle.begin_poly() 
  turtle.forward(length * 1.1) 
  # 停止記錄多邊形的頂點(diǎn)。當(dāng)前的烏龜位置是多邊形的最后一個(gè)頂點(diǎn)。將與第一個(gè)頂點(diǎn)相連。 
  turtle.end_poly() 
  # 返回最后記錄的多邊形。 
  handForm = turtle.get_poly() 
  turtle.register_shape(name, handForm) 
def Init(): 
  global secHand, minHand, hurHand, printer 
  # 重置Turtle指向北 
  turtle.mode("logo") 
  # 建立三個(gè)表針Turtle并初始化 
  mkHand("secHand", 135) 
  mkHand("minHand", 125) 
  mkHand("hurHand", 90) 
  secHand = turtle.Turtle() 
  secHand.shape("secHand") 
  minHand = turtle.Turtle() 
  minHand.shape("minHand") 
  hurHand = turtle.Turtle() 
  hurHand.shape("hurHand") 
  for hand in secHand, minHand, hurHand: 
    hand.shapesize(1, 1, 3) 
    hand.speed(0) 
  # 建立輸出文字Turtle 
  printer = turtle.Turtle() 
  # 隱藏畫筆的turtle形狀 
  printer.hideturtle() 
  printer.penup() 
def SetupClock(radius): 
  # 建立表的外框 
  turtle.reset() 
  turtle.pensize(7) 
  for i in range(60): 
    Skip(radius) 
    if i % 5 == 0: 
      turtle.forward(20) 
      Skip(-radius - 20) 
       
      Skip(radius + 20) 
      if i == 0: 
        turtle.write(int(12), align="center", font=("Courier", 14, "bold")) 
      elif i == 30: 
        Skip(25) 
        turtle.write(int(i/5), align="center", font=("Courier", 14, "bold")) 
        Skip(-25) 
      elif (i == 25 or i == 35): 
        Skip(20) 
        turtle.write(int(i/5), align="center", font=("Courier", 14, "bold")) 
        Skip(-20) 
      else: 
        turtle.write(int(i/5), align="center", font=("Courier", 14, "bold")) 
      Skip(-radius - 20) 
    else: 
      turtle.dot(5) 
      Skip(-radius) 
    turtle.right(6)    
def Week(t):   
  week = ["星期一", "星期二", "星期三", 
      "星期四", "星期五", "星期六", "星期日"] 
  return week[t.weekday()]  
def Date(t): 
  y = t.year 
  m = t.month 
  d = t.day 
  return "%s %d%d" % (y, m, d)   
def Tick(): 
  # 繪制表針的動(dòng)態(tài)顯示 
  t = datetime.today() 
  second = t.second + t.microsecond * 0.000001 
  minute = t.minute + second / 60.0 
  hour = t.hour + minute / 60.0 
  secHand.setheading(6 * second) 
  minHand.setheading(6 * minute) 
  hurHand.setheading(30 * hour)    
  turtle.tracer(False)  
  printer.forward(65) 
  printer.write(Week(t), align="center", 
         font=("Courier", 14, "bold")) 
  printer.back(130) 
  printer.write(Date(t), align="center", 
         font=("Courier", 14, "bold")) 
  printer.home() 
  turtle.tracer(True)   
  # 100ms后繼續(xù)調(diào)用tick 
  turtle.ontimer(Tick, 100) 
def main(): 
  # 打開/關(guān)閉龜動(dòng)畫,并為更新圖紙?jiān)O(shè)置延遲。 
  turtle.tracer(False) 
  Init() 
  SetupClock(160) 
  turtle.tracer(True) 
  Tick() 
  turtle.mainloop() 
  
if __name__ == "__main__": 
  main() 

畫一個(gè)邊長為60的正方形,并填充為紅色,邊框?yàn)樗{(lán)色

#-*- coding: utf-8 -*-
import turtle
turtle.reset()
a= 60
turtle.fillcolor("red")
turtle.pencolor("blue")
turtle.pensize(10)
turtle.fill(True)
turtle.left(90)
turtle.forward(a)
turtle.left(90)
turtle.forward(a)
turtle.left(90)
turtle.forward(a)
turtle.left(90)
turtle.forward(a)
turtle.fill(False)

最后一個(gè)實(shí)例:

from numpy import *
from random import random
import turtle
turtle.reset()
x = array([[.5],[.5]])
p = [0.85,0.92,0.99,1.00]
A1 = array([[.85, 0.04],
      [-0.04,.85]])
b1 = array([[0],[1.6]])
A2 = array([[0.20,-0.26],
      [0.23,0.22]])
b2 = array([[0],[1.6]])
A3 = array([[-0.15,0.28],
      [0.26,0.24]])
b3 = array([[0],[0.44]])

A4 = array([[0,0],
      [0,0.16]])

turtle.color("blue")
cnt = 1
while True:
  cnt += 1
  if cnt == 2000:
    break
  r = random()
  if r < p[0]:
    x = dot(A1 , x) + b1
  elif r < p[1]:
    x = dot(A2 , x) + b2
  elif r < p[2]:
    x = dot(A3 , x) + b3
  else:
    x = dot(A4 , x)
  #print x[1]
  turtle.up()
  turtle.goto(x[0][0] * 50,x[1][0] * 40 - 240)
  turtle.down()
  turtle.dot()

總結(jié)

以上就是本文關(guān)于Python內(nèi)置模塊turtle繪圖詳解的全部內(nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:

Python中turtle作圖示例

使用Python的turtle模塊畫圖的方法

如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

相關(guān)文章

最新評(píng)論

宜兴市| 辽宁省| 泌阳县| 五台县| 界首市| 鹿泉市| 寻甸| 宜宾市| 咸宁市| 临湘市| 新营市| 枣强县| 西城区| 光泽县| 福州市| 喜德县| 吉木乃县| 宜良县| 乐清市| 正阳县| 商丘市| 静宁县| 普兰店市| 樟树市| 泰顺县| 平阳县| 轮台县| 娱乐| 天长市| 沐川县| 桦甸市| 龙门县| 佳木斯市| 綦江县| 府谷县| 高雄县| 四子王旗| 绥宁县| 登封市| 印江| 南京市|