Draw standard five-star red flag with python turtle brush
Reasons for review Here, the pictures of red flags separated by grids have been deleted from the text. The core is to grid the flags first to facilitate the positioning of the brushes. This can be searched on Baidu to find relevant pictures of red flags with grids.
First divide the flag surface into 4 equally divided rectangles, and then divide the upper left rectangle into 15×10 squares in length and width. The center of the big five-pointed star is located at the upper 5, 5, left 5 and right 10 places of the rectangle. The diameter of the circumcircle of the great five-pointed star is 6 units of length. (The unit length obtained here is used as a reference value for the size ratio in the subsequent program)
The center point of the four small five-pointed stars,
The first one is located on the top 2, the bottom 8, the left 10 and the right 5.
The second one is located on the top 4, the bottom 6, the left 12 and the right 3.
The third one is located in the upper 7, the lower 3, the left 12 and the right 3,
The fourth is located at the top 9 down 1, left 10 right 5.
The diameter of the circumcircle of each small five-pointed star is 2 units of length. Each of the four small five-pointed stars has a pointed point facing the center of the large five-pointed star.
Here, the pictures with red flags separated by a grid are deleted together
Big five-pointed star: the starting point of the upper left corner (2/30, 4/20) [the 2nd grid from the left, the 4th grid from the top] side length (6/30) center point (5/30, 5/20) [5, 5]
Small pentagram:
The first one: center point (10/30, 2/20) [left 10, top 2]
The second one: center point (12/30, 4/20) [left 12, top 4]
The third one: center point (12/30, 7/20) [left 12, top 7]
The fourth one: center point (10/30, 9/20) [left 10, top 9]
The "National Flag Law" also gives five standard specifications for the national flag:
scale | Height (cm) |
1 | 288 × 192 |
2 | 240 × 160 |
3 | 192 × 128 |
4 | 144 × 96 |
5 | 96 × 64 |
- import turtle
- import math
-
- t = turtle.Pen()
- width = 900
- height = 600
- t.speed( 6 ) # Set the drawing speed of the brush to be --> 1 (slow) --> 10 (fast), 0 is the fastest
- t.screen.title( 'Long live the motherland' ) # Set the name of the picture
- t.screen.setup(width, height) # Set the size, width and height of the canvas (exactly equal to the width and height of the five-star red flag)
- t.screen.bgcolor( 'red' ) # Set the color of the canvas, here is set to red as the background color of the five-star red flag
- t.fillcolor( 'yellow' ) # The fill color of the five-pointed star
- t.pencolor( 'yellow' ) # The color of the brush is the same as the color of the five-pointed star
-
- # Calculate the side lengths of five-pointed stars with different diameters circumscribed circles (such as 6) When called, return the result directly (specific side length)
- def side_length ( diameter ): # diameter: diameter
- return math.sin(math.radians(72))*diameter*30
-
- # Divide the canvas into a coordinate reference system with a width of 30 unit squares and a height of 20 unit squares
- # Calculate the actual width of each small square to move the brush and return the result directly when called
- def scale ( diameter ): # scale: The scale diameter represents the number of grids (diameter of the circumcircle of the five-pointed star)
- return width/ 30 *diameter # width/30 represents the actual length of each grid
-
- # Draw five-pointed stars of different sizes. The trajectory of each five-pointed star is the same
- def star ( diameter, angle ): # star: The first parameter of the star represents the diameter of the circumcircle of the pentagram and the second parameter represents the rotation angle of the brush
- t.setheading(angle) # When the brush comes to the center of a pentagram, adjust the direction of the brush so that the corner of the small pentagram points to the center of the big pentagram
- t.forward(scale(diameter)/ 2 ) # The distance of the brush advancing from the center point of the five-pointed star to the starting point of the five-pointed star is half the diameter of the circumscribed circle, so it needs to be divided by 2
- t.setheading(angle- 180 ) # Make the brush point to 180° and reverse it to the left, but now it is adjusted to the right
- t.left( 18 ) # The brush is fine-tuned again by 18 degrees (half of the inner corner of the pentagram), pointing exactly in the direction where the first edge will be drawn
- t.begin_fill() # Prepare color fill to fill the pentagram to be drawn
- for i in range ( 5 ):
- t.forward(side_length(diameter))
- t.right(144)
- t.end_fill() # color fill until end
-
- def spin ( x, y ): # spin: Spin is mainly for 4 small five-pointed stars all have one corner to be aligned with the center of the big five-pointed star
- return math.degrees(math.atan2(x,y))
-
- def national_flag ( x,y,z,h ): # x: move from left to grid y: move up grid z: pentagram diameter h: brush rotation angle
- t.up()
- t.goto(-scale(x), scale(y))
- t.down()
- star(z,h)
- national_flag( 10 , 5 , 6 , 180 - 18 ) # (Position of the center point of the five-pointed star: the distance from the center of the canvas to the left, the number of cells to move up, the size, the rotation angle of the brush)
- national_flag( 5 , 8 , 2 , spin( 3 , 5 )+ 180 ) # The first small pentagram
- national_flag( 3 , 6 , 2 , spin( 1 , 7 )+ 180 ) # Second small pentagram
- national_flag( 3 , 3 , 2 , 180 -spin( 2 , 7 )) # The third small pentagram
- national_flag( 5 , 1 , 2 , 180 -spin( 4 , 5 )) # Fourth small pentagram
-
- t.hideturtle()
- turtle.done()