Python drawing - beautiful rose (source code)

    2022-12-26 The article "Python uses the turtle library to draw graphics-beautiful roses" has attracted more attention from netizens, and the feedback is good. Since the code is a screenshot of the source code, many netizens privately sent me to reflect that the code they entered could not Normal operation, some letters ask for the source code, some want to "share the code", so today I directly post the source code to meet the requirements of netizens, and I will not reply to all letters.

    Because the direct code review failed, the following content is repeated.

1. How to draw a rose

    Flower leaves and peduncles are drawn with shapes (arc and straight line) and filled with color, jagged edges (leaf edges) on leaves are drawn with arcs + corners + straight lines, and veins on leaves are drawn with straight lines + arcs. The petal part is relatively complicated. It is necessary to express the flanging of the petals (using the drawing method of the brush stick figure in the previous article), and use the contour line to fill in the edges, but it is difficult to fill in the petals formed by the edges. The solution is to draw in layers, first draw the entire flower (petal part) into a closed shape, then fill in the petal color as the petal background, then draw the edge of the petals on it, and draw them together to form a flower composed of petals.

    In order to facilitate the connection of leaves, pedicels, calyx, and petals, according to the principle that the graphics drawn after the turtle drawing will cover the graphics drawn before, the petal background is drawn first, then the leaves, pedicels, calyx, and petals are drawn. draw.

The principle of drawing the brush stick figure effect is to first draw the outline of the lines with uneven thickness, and then fill in the color, so as to simulate the brush drawing effect.

2. Summary of turtle drawing commands

    Summary of turtle plotting functions. The appearance of "|" in the following means or, such as penup() | pu() | up(), which means that the standard function is penup(), and its abbreviated function pu() or up() can also be used.

1.  Brush control function

    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. Brush movement will not draw graphics, only move, but will not affect coloring

    pendown() | pd() | down(): Drop the brush. Brush movement will draw graphic lines

    pensize(width) | width(width): The width (thickness) of the brush. Width is expressed in pixels, default width=1

    pencolor(color): Display or set the pen color. ①The parameter color defaults to displaying the current brush color; ②A parameter color must be a string or a tuple, which can be a color name string, such as 'tomato', a hexadecimal RGB string, such as '#ff6347', Numerical RGB tuple, such as (255, 99, 71) or (1, 0.39, 0.28); ③ Three parameters, three values ​​respectively represent R, G, B, such as 255, 99, 71 or 1, 0.39, 0.28.

2. Motion control function

    forward(d) | fd(d): Move d pixels forward (relative position) along the current turtle direction, if d is negative, move backward, and the direction of the brush remains unchanged.

    circle(radius, extent=None, steps=None): Draw an arc with radius radius and angle extent° or an inscribed edge with steps. 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.

    goto(x,y): Move to the coordinate (x, y) position (absolute position).

3. Direction control function

    setheading(angle) | seth(angle): Change the heading. If angle is positive, turn to angle° (absolute degree) from the direction of turtle 0° to the positive direction of angle; if angle is negative, turn to angle° (absolute angle) from the direction of turtle 0° to the direction of negative angle. The default horizontal direction is 0° to the right, counterclockwise is positive, and clockwise is negative.

    left(angle) | lt(angle): Turtle turns left (counterclockwise) angle° (relative degree).

    right(angle) | rt(angle): The turtle turns right (clockwise) by angle° (relative degrees).

4. Coloring control function

    begin_fill(): Called before drawing the shape to be filled. Start the coloring mode, the shape area drawn between this statement and end_fill() will be filled with the fill color, even if the pen is lifted.

    end_fill(): Fills the shape drawn after calling begin_fill(). If the shape is not closed, first connect the start point and the end point and then fill in the color, and terminate the coloring mode after filling the color. Lifting the pen does not affect the coloring, but the edge of the area is not drawn after the pen is lifted.

    fillcolor(color): Display or set the fill color. ①The parameter color defaults to display the current fill color; ②A parameter color must be a string or a tuple, which can be a color name string, such as 'tomato', a hexadecimal RGB string, such as '#ff6347', Numerical RGB tuple, such as (255, 99, 71) or (1, 0.39, 0.28); ③ Three parameters, three values ​​respectively represent R, G, B, such as 255, 99, 71 or 1, 0.39, 0.28.

5. Color control function

    colormode(cmode=None): Display or set the color mode. If there is no parameter, it returns the color mode; the default mode is 1.0 mode, that is, the RGB range is 0-1.0, also known as the decimal mode; it can be set to 255 mode, that is, the RGB range is 0-255 integer, also known as the integer mode

    color(*args): returns or sets the pen color and fill color. If there is no parameter, return the pen color and fill color; when there is only one parameter colorstring, (r, g, b), then set pencolor() and fillcolor() to this color at the same time; the two parameters colorstring1, colorstring2, (r1, g1, b1), (r2, g2, b2), the previous color is the brush color, and the latter color is the fill color; when the three parameters are r, g, b, set the pencolor and fillcolor at the same time as (r, g, b )color.

6. Turtle LOGO control and text font control functions

    showturtle() |st(): Display the turtle LOGO.

    hideturtle() |ht(): Hide the turtle LOGO.

    write(arg, move=False, align="left", font=("Arial", 8,"normal")): Draw text. Align is the method, and font is the font, size and font. In this example, the font is "Arial", the size is 8 points, and the font style is "Normal".

    Since the drawing statements of Python's turtle library are relatively short, there are more than 570 lines according to the standard program, because comments are added at necessary positions, and multiple statements are combined on the same line in consideration of space, although it does not meet the standard, it also increases reading Difficulty, but the length is greatly compressed, only more than 160 lines after compression, and it does not affect the normal operation of the program.

   Python uses the turtle library to draw graphics - beautiful rose source program (source code):

############################################
# 设计 Zhang Ruilin  创建 2021-10-18 10:43 #
# turtle绘制复杂图案——毛笔画漂亮的玫瑰花 #
############################################
import turtle as tl

tl.speed(0)
tl.setup(600, 650)		# 设置画面尺寸
tl.title('海龟绘制复杂图案——简笔画漂亮的玫瑰花')
tl.up()
tl.goto(-180, 275)		# 坐标移至(-200,290)
# 绘制文字
tl.color('blue')		# 写标题的颜色(蓝色) 
tl.write('漂亮的玫瑰花',align='center',font=('微软雅黑',24))
R = 1/3				# 设置比例因子,用数据的1/3大小绘制
x0, y0 = R*650, R*850		# 设置玫瑰花中心位置偏移量
# 花瓣背景(花瓣主体)
tl.goto(R*1109-x0,y0-R*308)	# 花瓣背景起点坐标
tl.color('red')			# 设置花瓣为红色(画笔、填充都为红色)
tl.seth(-25); tl.down(); tl.begin_fill()
tl.circle(-400*R,38); tl.rt(115); tl.circle(300*R,45); tl.circle(-700*R,20)
tl.circle(700*R,4); tl.circle(-550*R,10); tl.circle(-300*R,27)
tl.circle(50*R,35); tl.circle(-800*R,5); tl.circle(-60*R,70)
tl.circle(900*R,12); tl.circle(100*R,27); tl.circle(-800*R,14)
tl.circle(-30*R,121); tl.circle(-800*R,15); tl.lt(90); tl.circle(400*R,18)
tl.circle(-50*R,140); tl.lt(130); tl.circle(-130*R,110); tl.circle(100*R,30)
tl.circle(-450*R,20); tl.circle(-250*R,60); tl.lt(70); tl.circle(-130*R,60)
tl.circle(500*R,21); tl.circle(-130*R,95); tl.circle(-200*R,20); tl.end_fill()
# 画花叶
tl.color('green')		# 设置花叶为绿色
tl.up(); tl.goto(R*560-x0,y0-R*1230)				# 花叶起点坐标
tl.down(); tl.seth(80); tl.begin_fill()
for i in range(5):		# 画第一片花叶
    tl.circle(410*R,20); tl.lt(120); tl.fd(35*R); tl.rt(120)	# 画叶缘
tl.fd(160*R); tl.lt(150); tl.fd(150*R)
for i in range(4):		# 画叶缘
    tl.rt(130); tl.fd(30*R); tl.lt(120+i*3); tl.circle(600*R,11-i)
tl.rt(130); tl.fd(30*R); tl.lt(125); tl.circle(500*R,30); tl.end_fill()
tl.up(); tl.seth(-35); tl.fd(150*R); tl.lt(185); tl.down()	# 画叶脉
tl.color('darkgreen')		# 叶脉用深绿色
tl.begin_fill(); tl.circle(-470*R,20); tl.circle(750*R,25); tl.fd(200*R)
tl.lt(175); tl.fd(190*R); tl.circle(-750*R,20); tl.circle(500*R,15)
tl.fd(100*R); tl.end_fill(); tl.up(); tl.lt(175); tl.fd(250*R)
tl.rt(45); tl.down(); tl.pensize(3); tl.fd(160*R)
for i in range(2):
    tl.up(); tl.lt(120); tl.fd(110*R); tl.rt(115); tl.down(); tl.fd((150-i*20)*R)
tl.up(); tl.lt(140); tl.fd(150*R); tl.lt(120); tl.down(); tl.fd(100*R)
for i in range(2):
    tl.up(); tl.rt(110); tl.fd((100+i*5)*R); tl.lt(110); tl.down(); tl.fd(140*R)

tl.color('green')		# 设置线条颜色和填充颜色同为绿色
tl.up(); tl.seth(-80)		# 第二片花叶
tl.fd(250*R); tl.pensize(1); tl.down(); tl.seth(125); tl.begin_fill()
for i in range(5):
    tl.circle(330*R,20); tl.lt(120); tl.fd(30*R); tl.rt(120)
tl.fd(130*R); tl.lt(150); tl.fd(120*R)
for i in range(4):
    tl.rt(130); tl.fd(25*R); tl.lt(120+i*3); tl.circle(460*R,11-i)
tl.rt(130); tl.fd(25*R); tl.lt(125); tl.circle(400*R,30); tl.end_fill()
tl.up(); tl.seth(12); tl.fd(120*R); tl.lt(182); tl.down()
tl.color('darkgreen'); tl.begin_fill(); tl.circle(-380*R,20)
tl.circle(600*R,30); tl.fd(130*R); tl.lt(175); tl.fd(120*R)
tl.circle(-600*R,25); tl.circle(400*R,15); tl.fd(80*R); tl.end_fill()
tl.up(); tl.lt(175); tl.fd(200*R); tl.rt(45); tl.down(); tl.pensize(3)
tl.fd(150*R)
for i in range(2):
    tl.up(); tl.lt(120); tl.fd(90*R); tl.rt(115); tl.down(); tl.fd((100-i*15)*R)
tl.up(); tl.lt(140); tl.fd(110*R); tl.lt(120); tl.down(); tl.fd(100*R)
for i in range(2):
    tl.up(); tl.rt(110); tl.fd((80+i*5)*R); tl.lt(110); tl.down(); tl.fd(110*R)
tl.up(); tl.rt(10); tl.fd(500*R); tl.seth(90); tl.color('green'); tl.pensize(1)
tl.begin_fill()			# 画第三片花叶
for i in range(5):
    tl.circle(-410*R,20); tl.rt(120); tl.fd(35*R); tl.lt(120)
tl.fd(160*R); tl.rt(150); tl.fd(150*R)
for i in range(4):
    tl.lt(130); tl.fd(30*R); tl.rt(120+i*3); tl.circle(-600*R,11-i)
tl.lt(130); tl.fd(30*R); tl.rt(125); tl.circle(-500*R,30); tl.end_fill()
tl.up(); tl.lt(40); tl.fd(160*R); tl.lt(180); tl.down(); tl.color('darkgreen')
tl.begin_fill(); tl.circle(470*R,20); tl.circle(-750*R,25); tl.fd(200*R)
tl.rt(175); tl.fd(190*R); tl.circle(750*R,20); tl.circle(-500*R,15)
tl.fd(100*R); tl.end_fill(); tl.up(); tl.rt(175); tl.fd(250*R); tl.lt(45)
tl.down(); tl.pensize(3); tl.fd(160*R)
for i in range(2):
    tl.up(); tl.rt(120); tl.fd(110*R); tl.lt(115); tl.down(); tl.fd((150-i*20)*R)
tl.up(); tl.rt(140); tl.fd(150*R); tl.rt(120); tl.down(); tl.fd(100*R)
for i in range(2):
    tl.up(); tl.lt(110); tl.fd((100+i*5)*R); tl.rt(110); tl.down(); tl.fd(140*R)
tl.lt(45); tl.fd(230*R); tl.lt(45)
tl.color('saddlebrown','#976123')				# 花梗色
tl.begin_fill()			# 画花梗
tl.fd(460*R); tl.circle(250*R,15); tl.rt(120); tl.circle(80*R,45); tl.rt(100)
tl.circle(-100*R,35); tl.circle(700*R,15); tl.fd(360*R); tl.circle(300*R,10)
tl.fd(220*R); tl.rt(90); tl.fd(35*R); tl.rt(90); tl.fd(220*R)
tl.circle(-320*R,10); tl.fd(70*R); tl.end_fill()
tl.up()				# 画花萼
tl.rt(160); tl.fd(400*R); tl.lt(80); tl.pensize(1); tl.color('darkgreen','green')
tl.down(); tl.begin_fill(); tl.circle(520*R,10); tl.circle(100*R,75)
tl.circle(-100*R,60); tl.lt(150); tl.circle(220*R,85); tl.rt(140)
tl.circle(240*R,30); tl.circle(-240*R,15); tl.lt(140); tl.circle(-240*R,20)
tl.circle(240*R,25); tl.rt(120); tl.circle(180*R,80); tl.circle(100*R,40)
tl.lt(100); tl.circle(-310*R,23); tl.lt(57); tl.circle(-520*R,35); tl.end_fill()
tl.up(); tl.seth(90)		# 画花芯花瓣(蕃茄红色)
tl.fd(595*R); tl.lt(90); tl.fd(60*R); tl.rt(120); tl.down(); tl.color('tomato')
tl.begin_fill(); tl.circle(-80*R,45); tl.circle(-30*R,120); tl.circle(-10*R,50)
tl.circle(-250*R,30); tl.circle(-20*R,60); tl.circle(-80*R,100)
tl.circle(-290*R,40); tl.circle(-110*R,90); tl.circle(110*R,70)
tl.circle(-110*R,80); tl.circle(-220*R,25); tl.rt(70); tl.fd(25*R); tl.rt(110)
tl.circle(220*R,30); tl.circle(80*R,70); tl.circle(-120*R,30); tl.lt(125)
tl.circle(200*R,20); tl.rt(45); tl.circle(220*R,50); tl.rt(80); tl.fd(21*R)
tl.rt(105); tl.circle(-400*R,33); tl.lt(50); tl.circle(-240*R,15)
tl.circle(850*R,5); tl.circle(90*R,87); tl.circle(270*R,40); tl.circle(60*R,100)
tl.circle(10*R,63); tl.circle(250*R,25); tl.lt(90); tl.circle(28*R,100)
tl.end_fill(); tl.up(); tl.rt(150); tl.fd(95*R); tl.rt(30); tl.begin_fill()
tl.down(); tl.circle(-160*R,30); tl.circle(-270*R,30); tl.circle(-100*R,50)
tl.rt(150); tl.circle(250*R,50); tl.circle(150*R,20); tl.end_fill(); tl.up()
tl.lt(130); tl.fd(280*R); tl.rt(65); tl.begin_fill(); tl.down()
tl.circle(-350*R,20); tl.circle(-100*R,10); tl.circle(-470*R,10); tl.rt(110)
tl.fd(20*R); tl.rt(60); tl.circle(-470*R,8); tl.circle(150*R,25)
tl.circle(470*R,8); tl.circle(-30*R,50); tl.end_fill(); tl.up(); tl.lt(186,)
tl.fd(480*R); tl.lt(195); tl.down(); tl.begin_fill(); tl.circle(-500*R,22)
tl.circle(280*R,20); tl.circle(130*R,90); tl.circle(15*R,175); tl.fd(20*R)
tl.rt(105); tl.circle(-300*R,70); tl.rt(120); tl.circle(-800*R,3); tl.rt(70)
tl.circle(100*R,30); tl.lt(160); tl.circle(-50*R,75); tl.circle(-100*R,100)
tl.circle(200*R,20); tl.lt(30); tl.fd(25*R); tl.lt(135); tl.fd(35*R); tl.rt(95)
tl.circle(-100*R,80); tl.circle(800*R,15); tl.circle(-100*R,100)
tl.circle(-200*R,30); tl.circle(800*R,20); tl.circle(10*R,180)
tl.circle(-800*R,20); tl.circle(220*R,22); tl.circle(120*R,105)
tl.circle(-800*R,15); tl.circle(110*R,90); tl.rt(85); tl.circle(-400*R,5)
tl.circle(130*R,100); tl.rt(60); tl.circle(600*R,5); tl.rt(50)
tl.circle(600*R,5); tl.circle(40*R,120); tl.circle(600*R,15); tl.lt(50)
tl.circle(-600*R,15); tl.lt(130); tl.circle(170*R,55); tl.lt(165)
tl.circle(-160*R,40); tl.rt(120); tl.fd(90*R); tl.rt(45)
tl.circle(-600*R,14); tl.circle(-30*R,130); tl.circle(500*R,20)
tl.circle(300*R,50); tl.rt(95); tl.circle(-120*R,85); tl.circle(700*R,19)
tl.end_fill(); tl.up(); tl.rt(66); tl.fd(150*R); tl.rt(57); tl.down()
tl.begin_fill(); tl.circle(50*R,50); tl.fd(150*R); tl.circle(600*R,8)
tl.circle(-200*R,10); tl.circle(-30*R,145); tl.circle(-300*R,45)
tl.circle(-350*R,20); tl.circle(500*R,40); tl.circle(-200*R,20)
tl.circle(-50*R,100); tl.circle(-200*R,20); tl.circle(-500*R,20)
tl.circle(-250*R,40); tl.circle(-500*R,20); tl.circle(-700*R,20)
tl.circle(100*R,45); tl.fd(30*R); tl.rt(180); tl.circle(-120*R,45)
tl.circle(800*R,15); tl.circle(500*R,25); tl.circle(275*R,38)
tl.circle(500*R,27); tl.circle(70*R,110); tl.circle(220*R,25)
tl.circle(-420*R,43); tl.circle(320*R,65); tl.circle(55*R,141)
tl.circle(100*R,10); tl.fd(280*R); tl.end_fill(); tl.up(); tl.rt(165)
tl.fd(210*R); tl.lt(20); tl.down(); tl.begin_fill(); tl.circle(700*R,20)
tl.circle(30*R,120); tl.circle(800*R,15); tl.circle(-80*R,30)
tl.circle(-800*R,12); tl.circle(80*R,70); tl.circle(500*R,10); tl.lt(135)
tl.fd(20*R); tl.lt(42); tl.circle(-480*R,8); tl.circle(-70*R,70)
tl.circle(800*R,13); tl.circle(70*R,30); tl.circle(-800*R,13)
tl.circle(-25*R,123); tl.circle(-680*R,17); tl.end_fill(); tl.up(); tl.lt(80)
tl.fd(200*R); tl.lt(20); tl.down(); tl.begin_fill(); tl.circle(-130*R,100)
tl.circle(250*R,20); tl.circle(-400*R,30); tl.circle(-250*R,20)
tl.circle(-200*R,35); tl.rt(90); tl.fd(20*R); tl.rt(90); tl.circle(200*R,35)
tl.circle(230*R,20); tl.circle(380*R,30); tl.circle(-260*R,20)
tl.circle(115*R,105); tl.end_fill(); tl.up(); tl.lt(55); tl.fd(870*R)
tl.rt(25); tl.down(); tl.begin_fill(); tl.circle(-400*R,40); tl.rt(115)
tl.circle(300*R,45); tl.circle(-700*R,19); tl.rt(135); tl.fd(25*R); tl.rt(43)
tl.circle(700*R,18); tl.circle(-300*R,39); tl.lt(113); tl.circle(400*R,35)
tl.end_fill(); tl.lt(20); tl.up(); tl.fd(300*R); tl.lt(25); tl.down()
tl.begin_fill(); tl.circle(-400*R,50); tl.circle(300*R,5); tl.lt(170)
tl.circle(-300*R,6); tl.circle(250*R,25); tl.circle(400*R,36)
tl.circle(15*R,180); tl.end_fill()

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

    operation result:

Figure 1 A beautiful rose drawn with the above Python program code

Guess you like

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