我們將介紹如何通過Python編程的方式實現李字的繪製。
一、引言
背景:李字是一種很有意思的藝術字體,而且它也能夠很好地展示出計算機繪圖的能力,因此很有必要學習如何用Python實現它的繪製。
設計思路:整個圖形可分為三部分:左半邊、右半邊和中間連接線。左半邊和右半邊均以一個矩形為基礎,每個矩形內部通過一定的數學公式繪製一個圓弧,再通過繪製弧切,得到李字效果。而中間連接線則是通過一些圖形變換實現。
二、繪製左半邊
我們從繪製左半邊開始。
from turtle import *
import math
# 左半邊
def left():
pu()
goto(-50, 100)
pd()
seth(-90)
width(15)
circle(50, 180)
forward(100)
left(90)
circle(150, -90)
left(90)
forward(100)
# 繪製
left()
done()
運行代碼,將得到如下圖形:
三、繪製右半邊
接下來,我們需要繪製右半邊。
from turtle import *
import math
# 左半邊
def left():
pu()
goto(-50, 100)
pd()
seth(-90)
width(15)
circle(50, 180)
forward(100)
left(90)
circle(150, -90)
left(90)
forward(100)
# 右半邊
def right():
pu()
goto(50, 100)
pd()
seth(-90)
width(15)
circle(-50, 180)
forward(100)
right(90)
circle(-150, -90)
right(90)
forward(100)
# 繪製
left()
right()
done()
運行代碼,將得到如下圖形:
四、繪製中間連接線
現在我們需要繪製中間的連接線。這個需要藉助於一些圖形變換來完成。
from turtle import *
import math
# 左半邊
def left():
pu()
goto(-50, 100)
pd()
seth(-90)
width(15)
circle(50, 180)
forward(100)
left(90)
circle(150, -90)
left(90)
forward(100)
# 右半邊
def right():
pu()
goto(50, 100)
pd()
seth(-90)
width(15)
circle(-50, 180)
forward(100)
right(90)
circle(-150, -90)
right(90)
forward(100)
# 中間連接線
def middle():
pu()
goto(0, 50)
pd()
seth(0)
width(15)
fd(100)
# 平移
pu()
left(90)
fd(15)
right(90)
pd()
fd(70)
# 旋轉
pu()
goto(0, 50)
pd()
right(30)
width(5)
fd(40)
left(120)
fd(40)
# 繪製
left()
right()
middle()
done()
運行代碼,將得到如下圖形:
五、完整代碼
以下是完整的Python代碼:
from turtle import *
import math
# 左半邊
def left():
pu()
goto(-50, 100)
pd()
seth(-90)
width(15)
circle(50, 180)
forward(100)
left(90)
circle(150, -90)
left(90)
forward(100)
# 右半邊
def right():
pu()
goto(50, 100)
pd()
seth(-90)
width(15)
circle(-50, 180)
forward(100)
right(90)
circle(-150, -90)
right(90)
forward(100)
# 中間連接線
def middle():
pu()
goto(0, 50)
pd()
seth(0)
width(15)
fd(100)
# 平移
pu()
left(90)
fd(15)
right(90)
pd()
fd(70)
# 旋轉
pu()
goto(0, 50)
pd()
right(30)
width(5)
fd(40)
left(120)
fd(40)
# 繪製
left()
right()
middle()
done()
運行代碼,將得到李字的效果。
原創文章,作者:TQGKC,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/374711.html