Python turtle library graphics drawing - brush strokes dove of peace

1. How to draw brush strokes

Brush stick figures are obviously different from ordinary stick figures. The thickness of the lines drawn in common stick figures is the same, while the thickness of lines drawn with a brush varies, as shown in Figure 1.

 

Fig. 1 Part of brush stick figure (line thickness change)

Let's take a look at the curved Chinese character library first. In fact, Chinese characters are also similar, consisting of strokes of various shapes, as shown in Figure 2, the Chinese character "Wei" in Song Dynasty. Both horizontal and vertical strokes are with strokes. Currently, the curvilinear fonts are basically used, and only the characters are stored in the fonts. Contours, including horizontal lines, vertical lines, curves, etc., where the curves are fitted with a small number of coordinate points and then quadratic B-spline curves or cubic B-spline curves, which can be transformed steplessly, such as zooming in, zooming out, rotating, and deforming. The left side of Figure 2 is the outline of each stroke, and the right side of Figure 2 is the corresponding Chinese character formed by filling in each stroke.

 

Figure 2 Song type Chinese character "微"

As shown in Figure 3, the Chinese character "Rui" in italic script is written horizontally and vertically. The corresponding Chinese characters.

 

Figure 3 Italicized Chinese character "Rui"

Looking at Figure 1 again, it can be outlined as Figure 4, and then filled in. Drawing with turtle cannot be drawn with B-spline curves, and can only be approximated by arcs and straight lines with different radii and corners.

 

Figure 4 outlines Figure 1

Figure 5 is a stick figure from "Baidu Pictures". In order to make the turtle drawing more correct to restore this picture, you can first draw some arcs or straight lines on the important positions of the original picture (see Figure 6) for measurement The radius and angle of the arc can be estimated first, and then corrected (adjusted) according to the error when drawing, and other line segments can be compared with the marked arc radius for radius estimation.

 

Figure 5 Simple brush strokes - dove of peace

 

Figure 6 Draw some arcs or straight lines on the original painting, which is convenient for measuring the arc radius and estimating the angle

2. Simple strokes with a brush - the drawing of a dove of peace

The turtle library is a built-in graphical module of Python and one of the Python standard libraries. Common methods are as follows:

1. Brush control method

The "|" appears below means or, such as penup() | pu() | up(), the standard method is penup(), and its abbreviated method pu() or up() can also be used.

penup() | pu() | up()——lift up the brush (only move the brush after lifting the pen, no lines are drawn)

pendown() | pd() | down()——drop the brush (after the pen is moved, the line will be drawn)

pensize(width) | width(width)——pen width (width is expressed in pixels, default width=1)

pencolor(color)——the color of the brush (color can be a string color name, such as 'red'; it can also be an RGB color in the form of r, g, b, such as 255, 0, 0, etc.)

2. Motion control method

forward(d) | fd(d)——Move d pixels forward along the current turtle direction (if d is negative, move backward)

circle(radius, extent=None, steps=None)——draws an arc or steps inscribed with a radius of radius and an angle of extent°. If the extent and steps are omitted, a circle is drawn, the radius is positive, and the center of the circle is on the left side of the direction of the turtle; if the radius is negative, the center of the circle is on the right side of the direction of the turtle; if extent is specified, an arc is drawn at an angle °, the radius is positive, and the center of the circle is If the radius is negative on the left side of the turtle direction, the center of the circle is on the right side of the turtle direction; if only steps are specified, the inscribed positive steps of the circle will be drawn; if extent and steps are specified, the inscribed positive steps of the specified arc of the circle will be drawn Edge polyline.

3. Direction control method

setheading(angle) | seth(angle)——change the forward direction (if angle is positive, turn from the turtle 0° direction to the angle positive direction to angle° (absolute degree); if angle is negative, turn from the turtle 0° direction to the angle The negative direction turns to angle° (absolute angle). The default horizontal direction is 0° to the right, the counterclockwise direction is positive, and the clockwise direction is negative)

left(angle) | lt(angle)——the turtle turns left by angle° (relative degree)

right(angle) | rt(angle)——the turtle turns right by angle°(relative degrees)

4. Coloring control method

begin_fill() - Called before drawing the shape to be filled. Start coloring mode

end_fill() - fills the shape drawn after calling begin_fill(). If the shape is not closed, first connect the start point and end point and then fill in the color, and terminate the coloring mode after filling in the color

The complete source code is as follows:

###############################################
# 设计 Zhang Ruilin     创建 2021-10-06 23:18 #
#                       修订 2022-07-31 07:54 #
# turtle 绘简笔画——毛笔风格衔橄榄枝的和平鸽 #
###############################################
import turtle as tl
tl.speed(0)			# 设置最快绘图速度
tl.setup(780, 650)		# 设置画面尺寸
tl.title('海龟绘图——毛笔风格衔橄榄枝的和平鸽简笔画')
tl.pu()
R = 1/3				# 设置比例因子, 用数据的1/3大小绘制
x0, y0 = R*1250, R*1000		# 设置位置偏移量, 使图像处于窗口中间
draw_color='green'		# 定义线条颜色(绿色)
fill_color=('green')		# 定义填充颜色(绿色), 即橄榄枝颜色
tl.color(draw_color, fill_color)# 设置线条颜色和填充颜色
tl.goto(R*1030-x0, y0-R*1040)	# 移动至橄榄枝柄起点坐标
tl.pd()
tl.lt(90)
tl.begin_fill()			# 橄榄枝填色开始
tl.circle(200*R, 50)		# 画橄榄枝柄起笔
tl.circle(345*R, 25)
tl.circle(-520*R, 35)
tl.circle(-330*R, 40)
tl.rt(162)
tl.circle(550*R, 55)
tl.circle(-450*R, 30)
tl.circle(-150*R, 25)
tl.circle(-60*R, 95)
tl.end_fill()			# 橄榄枝填色, 并终止填色模式
tl.seth(100)
tl.pu()
tl.fd(500*R)			# 移动到第一片橄榄枝叶
tl.lt(110)
tl.pd()
tl.begin_fill()			# 第一片橄榄叶填色开始
tl.circle(170*R, 95)		# 第一片橄榄枝叶
tl.lt(100)
tl.circle(170*R, 55)
tl.end_fill()			# 第一片橄榄叶填色, 并终止填色模式
tl.seth(170)
tl.pu()
tl.fd(100*R)			# 移动到第二片橄榄枝叶
tl.lt(120)
tl.pd()
tl.begin_fill()
tl.circle(-100*R, 95)		# 第二片橄榄枝叶
tl.rt(75)
tl.circle(-180*R, 95)
tl.end_fill()
tl.seth(170)
tl.pu()
tl.fd(160*R)
tl.lt(40)
tl.pd()
tl.begin_fill()
tl.circle(100*R, 50)		# 第三片橄榄枝叶
tl.circle(170*R, 60)
tl.lt(85)
tl.circle(100*R, 60)
tl.fd(110*R)
tl.circle(-90*R, 20)
tl.end_fill()
tl.seth(120)
tl.pu()
tl.fd(110*R)
tl.pd()
tl.begin_fill()
tl.circle(10*R, 110)		# 第四片橄榄枝叶
tl.fd(100*R)
tl.circle(80*R, 90)
tl.fd(20*R)
tl.lt(80)
tl.circle(175*R, 40)
tl.end_fill()
tl.seth(160)
tl.pu()
tl.fd(175*R)
tl.rt(150)
tl.pd()
tl.begin_fill()
tl.circle(90*R, 85)		# 顶尖橄榄枝叶
tl.circle(170*R, 43)
tl.lt(115)
tl.fd(100*R)
tl.circle(160*R, 35)
tl.end_fill()
tl.seth(200)
tl.pu()
tl.fd(155*R)
tl.lt(140)
tl.pd()
tl.begin_fill()
tl.fd(90*R)
tl.circle(-140*R, 55)		# 下第一片橄榄枝叶
tl.rt(90)
tl.circle(-150*R, 100)
tl.end_fill()
tl.seth(-70)
tl.pu()
tl.fd(210*R)
tl.lt(65)
tl.pd()
tl.begin_fill()
tl.fd(150*R)			# 下第二片橄榄枝叶
tl.circle(-90*R, 70)
tl.rt(100)
tl.fd(120*R)
tl.circle(-90*R, 90)
tl.end_fill()
tl.seth(-22)
tl.pu()
tl.fd(360*R)
tl.rt(110)
tl.pd()
tl.begin_fill()
tl.circle(-180*R, 80)		# 下第三片橄榄枝叶
tl.rt(120)
tl.fd(90*R)
tl.circle(-140*R, 60)
tl.end_fill()
tl.lt(10)
tl.pu()
tl.fd(150*R)
tl.rt(100)
tl.pd()
tl.begin_fill()
tl.fd(60*R)
tl.circle(-130*R, 80)		# 下第四片橄榄枝叶
tl.rt(110)
tl.fd(90*R)
tl.circle(-140*R, 60)
tl.end_fill()
tl.pu()
tl.fd(90*R)
tl.seth(90)
tl.pd()
tl.color('black', 'black')	# 设置线条颜色和填充颜色均为黑色
tl.begin_fill()
tl.fd(10*R)			# 鸽头
tl.circle(-25*R, 45)
tl.fd(20*R)
tl.circle(100*R, 45)
tl.circle(-120*R, 70)
tl.circle(-160*R, 40)
tl.circle(-360*R, 20)
tl.fd(150*R)
tl.rt(105)
tl.circle(100*R, 25)
tl.rt(105)
tl.fd(135*R)
tl.circle(300*R, 30)
tl.circle(120*R, 60)
tl.circle(110*R, 60)
tl.rt(60)
tl.fd(70*R)
tl.lt(165)
tl.fd(80*R)
tl.lt(170)
tl.end_fill()
tl.seth(60)
tl.pu()
tl.fd(70*R)
tl.lt(10)
tl.color('red')			# 设置鸽眼颜色为红色
tl.pd()
tl.begin_fill()
tl.fd(20*R)			# 眼睛
tl.circle(-35*R, 90)
tl.rt(65)
tl.fd(20*R)
tl.end_fill()
tl.seth(223)
tl.pu()
tl.fd(200*R)
tl.color('black')
tl.pd()
tl.begin_fill()
tl.lt(165)
tl.fd(30*R)			# 腹部
tl.circle(-60*R, 110)
tl.fd(200*R)
tl.circle(715*R, 20)
tl.circle(380*R, 40)
tl.lt(120)
tl.fd(50*R)
tl.lt(58)
tl.circle(-380*R, 40)
tl.circle(-710*R, 20)
tl.fd(150*R)
tl.circle(65*R, 110)
tl.fd(50*R)
tl.end_fill()
tl.seth(-53)
tl.pu()
tl.fd(700*R)
tl.pd()
tl.begin_fill()
tl.rt(35)
tl.circle(700*R, 20)		# 尾巴
tl.circle(210*R, 40)
tl.circle(38*R, 90)
tl.rt(130)
tl.fd(70*R)
tl.circle(80*R, 120)
tl.rt(100)
tl.fd(40*R)
tl.circle(40*R, 150)
tl.rt(135)
tl.circle(150*R, 80)
tl.lt(68)
tl.circle(90*R, 45)
tl.circle(-700*R, 64)
tl.lt(45)
tl.fd(50*R)
tl.lt(131)
tl.circle(700*R, 73)
tl.rt(140)
tl.circle(-250*R, 40)
tl.lt(90)
tl.fd(20*R)
tl.circle(-30*R, 130)
tl.fd(50*R)
tl.lt(110)
tl.circle(-70*R, 120)
tl.fd(90*R)
tl.lt(120)
tl.circle(-50*R, 105)
tl.circle(-400*R, 20)
tl.end_fill()
tl.seth(93.5)
tl.pu()
tl.fd(590*R)
tl.lt(85)
tl.pd()
tl.begin_fill()
tl.circle(-56*R, 90)		# 前翅膀
tl.circle(-160*R, 50)
tl.circle(-700*R, 20)
tl.circle(900*R, 45)
tl.circle(100*R, 20)
tl.circle(-100*R, 30)
tl.rt(90)
tl.circle(-100*R, 51)
tl.circle(-470*R, 20)
tl.lt(150)
tl.circle(-50*R, 150)
tl.circle(-400*R, 12)
tl.circle(-800*R, 10)
tl.lt(150)
tl.circle(-170*R, 15)
tl.rt(100)
tl.circle(-150*R, 35)
tl.fd(90*R)
tl.lt(120)
tl.circle(-60*R, 82)
tl.circle(-150*R, 20)
tl.circle(-200*R, 35)
tl.lt(125)
tl.circle(-50*R, 120)
tl.circle(-300*R, 20)
tl.lt(150)
tl.circle(-40*R, 120)
tl.circle(-150*R, 22)
tl.circle(-300*R, 23)
tl.circle(-600*R, 36)
tl.rt(90)
tl.fd(20*R)
tl.rt(90)
tl.circle(580*R, 36)
tl.circle(270*R, 20)
tl.circle(90*R, 40)
tl.circle(35*R, 115)
tl.rt(150)
tl.fd(90*R)
tl.circle(100*R, 25)
tl.circle(35*R, 115)
tl.rt(115)
tl.fd(105*R)
tl.circle(100*R, 25)
tl.circle(60*R, 110)
tl.rt(145)
tl.fd(105*R)
tl.circle(90*R, 30)
tl.circle(35*R, 100)
tl.rt(120)
tl.fd(70*R)
tl.circle(450*R, 20)
tl.circle(30*R, 110)
tl.circle(300*R, 10)
tl.rt(121)
tl.circle(450*R, 25)
tl.lt(130)
tl.circle(150*R, 8)
tl.circle(700*R, 8)
tl.circle(-700*R, 40)
tl.fd(220*R)
tl.circle(510*R, 20)
tl.circle(115*R, 80)
tl.end_fill()
tl.seth(103)
tl.pu()
tl.fd(260*R)
tl.rt(50)
tl.pd()
tl.begin_fill()
tl.lt(3)
tl.circle(-780*R, 18)		# 后翅膀
tl.fd(150*R)
tl.circle(750*R, 30)
tl.circle(-200*R, 30)
tl.rt(95)
tl.circle(-150*R, 30)
tl.circle(-250*R, 30)
tl.lt(120)
tl.fd(50*R)
tl.circle(-30*R, 105)
tl.circle(-200*R, 20)
tl.circle(-400*R, 10)
tl.lt(140)
tl.circle(-270*R, 10)
tl.rt(100)
tl.circle(-150*R, 50)
tl.circle(-4*R, 150)
tl.circle(-200*R, 10)
tl.circle(25*R, 120)
tl.circle(200*R, 5)
tl.rt(125)
tl.circle(800*R, 5)
tl.circle(400*R, 7)
tl.circle(200*R, 15)
tl.lt(90)
tl.circle(200*R, 25)
tl.rt(125)
tl.circle(400*R, 20)
tl.circle(150*R, 22)
tl.lt(107)
tl.circle(80*R, 35)
tl.circle(-790*R, 35)
tl.circle(780*R, 24.5)
tl.end_fill()
tl.pu()
# 绘制文字
tl.goto(-200, -80)		# 坐标移至(-200, 250)文字中心处
tl.write('衔\n着', align='center', font=('汉仪行楷繁', 30))
tl.goto(-240, -160)		# 坐标移至(-200, 250)文字中心处
tl.write('橄\n榄\n枝\n的', align='center', font=('汉仪行楷繁', 30))
tl.goto(-280, -160)		# 坐标移至(-200, 250)文字中心处
tl.write('和\n平\n鸽', align='center', font=('汉仪行楷繁', 30))
tl.goto(-340,-270)
tl.write('任\n寅\n兰\n月\n瑞\n林\n制\n作\n于\n杭\n州',font=('华文行楷',16,'normal'))
tl.pensize(1)			# 绘印章
tl.seth(-45)
tl.goto(-345,-300)
tl.pd()
tl.color('red')			# 写标题的颜色(蓝色)
tl.circle(20,360,4)		# 绘外框
tl.lt(45)
tl.fd(3)
tl.write('瑞\n林',font=('印章篆体',10,'normal'))
tl.fd(13)
tl.write('张\n印',font=('印章篆体',10,'normal'))

tl.ht()				# 隐藏turtle形状
tl.done()

Execution result: (see Figure 7)

 

Figure 7 "Peace dove holding an olive branch" drawn by the turtle library

Guess you like

Origin blog.csdn.net/hz_zhangrl/article/details/128528945