Use python's turtle library to draw the five-star red flag

Table of contents

 

1. General idea

2. Code display

3. Code running effect


1. General idea

        Find the description of the standard five-star red flag, as shown in the first picture below (source: Baidu Encyclopedia). Find the five-star red flag ink line diagram in the second picture below.

2d9e80f82a604477a54aafa1f26ac116.png

f479ef38a46143f9be2b30da14f5c823.png

         We first define a small square of 10 pixels, draw five five-pointed stars in the second quadrant, and the origin is the center of the flag.

        The coordinates of the upper left corner of the five-star red flag are (-150, 100). When drawing the five-star red flag, the turtle is oriented to the right by default. Go forward 300 pixels and then turn right 90 degrees. Then move forward 200 pixels until the background of the five-star red flag is finished, and then fill it with red. This color value can be obtained through the color picker or directly using the "red" parameter.

        Drawing five five-pointed stars, we can find what the five five-pointed stars have in common. The four small five-pointed stars and the large five-pointed star are only different in size, orientation and coordinates. According to the five-star red flag ink line diagram, we can know that the coordinates of the big five-pointed star and the four small five-pointed stars (from top to bottom) are (-100,50), (-50,80), (-30,60), (- 30,30), (-50,10). The radius of the circumscribed circles of the large five-pointed star and the small five-pointed star are 30 pixels and 10 pixels respectively. Then the length of the five-pointed star can be calculated based on the radius of the circumscribed circle and the Pythagorean theorem (the length of the five-pointed star that requires five strokes).

        Then we design a function to draw these five five-pointed stars. Because they are similar, they can be drawn through a function. When drawing, we need to unify a drawing angle (the process of drawing five-pointed stars is similar. If you want to draw the desired To achieve the desired effect, you need to set an angle reference). Based on the orientation of the big five-pointed star, set its rotation angle to 0°. The angle parameter passed in is the angle relative to the big five-pointed star (how many degrees it is rotated counterclockwise). .

        The rotation angle of the small five-pointed star can be obtained through the ink line diagram combined with the inverse trigonometric function. Here is an example, which is the orientation of the top small five-pointed star. We can know from the ink line diagram that it rotates counterclockwise relative to the big five-pointed star ( 90+arctan(3/5)*180/Π)°. The arctangent function calculates a radian value. In order to convert it to an angle value, it needs to be multiplied by 180/Π. In the Python math library, the arctangent function is math.atan(), and Π is represented by math.pi.

        Then we can define the function for drawing a five-pointed star. When we finish drawing a five-pointed star, we will adjust the direction to upward. Next, take the big five-pointed star as an example.

        The initial coordinates of the brush are the center of the circumcircle of the big five-pointed star, which is the center of the big five-pointed star. We need to first go to the top vertex of the big five-pointed star. After painting the red background of the five-star red flag, the brush direction is to the right. We need to adjust the brush direction to upward, so before painting, we need to rotate the brush 90° to the left (rotate 90° counterclockwise). After that, the brush is still at the center of the circle, but the direction has changed. We have to start drawing from the vertex, first advance forward by a radius distance (30 pixels), and now the direction and position are the arrows pointing upward at the top vertex in the picture below (drawn through PS). After that, we need to rotate 162° to the right (162° clockwise), the direction is shown by another arrow at the top vertex of the figure.

        After that, you need to walk forward the length of the five-pointed star to reach the right arrow in the lower right corner. The length of the five-pointed star is calculated through the Pythagorean theorem combined with the ink line diagram. One-half the length is the radius * one-third of the root of eight (one-half of the length of the five-pointed star: radius of the circumscribed circle = root of eight: three), and then Multiply by 2 to get the length of the five-pointed star (that is, the distance to move forward), then rotate it 162° (the direction is shown in the other direction in the lower right corner of the picture below (approximately northwest)), and then loop 5 times to reach the non-upward point of the top vertex of the big five-pointed star in the picture below. The direction of the arrow.

1f83c35df4a54a4cbea2d99ce78352df.png

         In order to reuse the function of drawing a five-pointed star, we need to point the final orientation to the standard direction (that is, upward minus the direction of the counterclockwise angle passed in, and the big five-pointed star passes in the parameter 0, which is upward). At this time, we need to change the orientation 162° to the left.

        The differences when drawing a small five-pointed star are the coordinates of the center of the circle, the radius of the circumscribed circle, and the initial orientation. It should be noted that after modifying the initial orientation, the final position of the five-pointed star after drawing is not upward but has a deviation from the initial orientation angle. When the function is defined, in addition to the adjustment of the initial orientation at the beginning, it must also rotate in the opposite direction at the end. For example, if you At the beginning, it is rotated 30° counterclockwise, and finally it needs to be rotated 30° clockwise, so that the orientation will be the standard orientation (upward, convenient for drawing other small five-pointed stars) after the function is executed.

2. Code display

import turtle
import math

turtle.pensize(3)
turtle.colormode(255)

# 画背景
turtle.pencolor("#ed120c")
turtle.fillcolor("#ed120c")
turtle.begin_fill()
turtle.penup()
turtle.goto(-150, 100)
turtle.pendown()

for i in [300, 200, 300, 200]:
    turtle.forward(i)
    turtle.right(90)
turtle.left(90)
turtle.end_fill()
turtle.penup()


# 画五角星函数,要求传入五角星中心坐标,五角星外接圆半径,五角星逆时针旋转角度(默认角度0度为向上,即大五角星方向)
def Pentagram(x, y, radius, degree):
    length = (radius * math.sqrt(8) // 3) * 2  # 根据五角星外接圆半径计算五角星长度

    turtle.goto(x, y)  # 五角星外接圆中心坐标(即五角星中心坐标)
    turtle.left(degree)  # 五角星逆时针旋转多少度
    turtle.forward(radius)  # 前进外接圆半径距离,到达五角星顶点
    turtle.pendown()

    # 以下为标准画五角星代码,画笔颜色及填充颜色可以通过取色器取
    turtle.right(162)
    turtle.pencolor("#f4ec20")
    turtle.fillcolor("#f4ec20")
    turtle.begin_fill()
    for _ in range(5):
        turtle.forward(length)
        turtle.right(144)
    turtle.end_fill()
    turtle.left(162)

    turtle.right(degree)
    turtle.penup()


# 画大五角星
Pentagram(-100, 50, 30, 0)

# 画第一个小五角星
degree1 = 90 + math.atan(3 / 5) * 180 / math.pi  # 计算第一个小五角星相对于大五角星的逆时针旋转角度,atan计算弧度需要转化为角度
Pentagram(-50, 80, 10, degree1)

# 画第二个小五角星
degree2 = 90 + math.atan(1 / 7) * 180 / math.pi
Pentagram(-30, 60, 10, degree2)

# 画第三个小五角星
degree3 = 90 - math.atan(2 / 7) * 180 / math.pi
Pentagram(-30, 30, 10, degree3)

# 画第四个小五角星
degree4 = 90 - math.atan(4 / 5) * 180 / math.pi
Pentagram(-50, 10, 10, degree4)

turtle.hideturtle()
turtle.done()

3. Code running effect

        Since the presence of the national flag is considered a violation, you can run it on your own computer and the effect will be the effect of the five-star red flag.

54d8a01e532e44299c0b6138c0a9492c.png

 

 

 

Guess you like

Origin blog.csdn.net/qq_59744114/article/details/129207568