python 畫任意多邊形


urtle是Python內置的圖形庫,在這里,我們需要知道正多邊形內角計算公式:內角=(邊數-2)*180/邊數
可選項:提示輸入一種顏色,進行填充。本程序運行環境是Python3的IDLE。
import turtle

##python中的圖形庫

colors=["red","yellow","purple","blue","black","green"]
turtle.pensize(2)

turtle.setup(width=800, height=800, startx=400, starty=0)
turtle.pencolor("white")
#默認中心點開始,gisoracle
turtle.goto(0,300)

for num in range(3,20):
    turtle.pencolor(colors[num%6])

    angle = 180 - (num - 2) * 180 / num

    for i in range(num):
        turtle.begin_fill()
        turtle.forward(100)
        turtle.right(angle)
        turtle.end_fill()
turtle.done()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM