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.

The method of drawing moon cakes is also shared with you
Python draws mid-autumn moon cakes, and uses turtle library to draw mid-autumn moon cakes - Programmer SoughtThe rendering code of the mid-autumn moon cake in turtle painting is encapsulated in the function. If you want to change that part, cancel the original function call, and then freely add a new function to code import turtlet = turtle.Pen() # Brush one is used for Drawing t.speed(0)# Pattern color F29407# Pie body color F8B41A# Drawing pie body part def outfill_flower(flower_num: "Number of petals", flower_color: "Color of petals"): for i in range(flower_num):http://www.ifindbug.com/doc/id-45828/name-python-draws-mid-autumn-moon-cakes-and-uses-turtle-library-to-draw-mid-autumn-moon-cakes.html

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

  1. import turtle
  2. import math
  3. t = turtle.Pen()
  4. width = 900
  5. height = 600
  6. t.speed( 6 ) # Set the drawing speed of the brush to be --> 1 (slow) --> 10 (fast), 0 is the fastest
  7. t.screen.title( 'Long live the motherland' ) # Set the name of the picture
  8. 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)
  9. 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
  10. t.fillcolor( 'yellow' ) # The fill color of the five-pointed star
  11. t.pencolor( 'yellow' ) # The color of the brush is the same as the color of the five-pointed star
  12. # 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)
  13. def side_length ( diameter ): # diameter: diameter
  14. return math.sin(math.radians(72))*diameter*30
  15. # Divide the canvas into a coordinate reference system with a width of 30 unit squares and a height of 20 unit squares
  16. # Calculate the actual width of each small square to move the brush and return the result directly when called
  17. def scale ( diameter ): # scale: The scale diameter represents the number of grids (diameter of the circumcircle of the five-pointed star)
  18. return width/ 30 *diameter # width/30 represents the actual length of each grid
  19. # Draw five-pointed stars of different sizes. The trajectory of each five-pointed star is the same
  20. 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
  21. 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
  22. 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
  23. t.setheading(angle- 180 ) # Make the brush point to 180° and reverse it to the left, but now it is adjusted to the right
  24. 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
  25. t.begin_fill() # Prepare color fill to fill the pentagram to be drawn
  26. for i in range ( 5 ):
  27. t.forward(side_length(diameter))
  28. t.right(144)
  29. t.end_fill() # color fill until end
  30. 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
  31. return math.degrees(math.atan2(x,y))
  32. def national_flag ( x,y,z,h ): # x: move from left to grid y: move up grid z: pentagram diameter h: brush rotation angle
  33. t.up()
  34. t.goto(-scale(x), scale(y))
  35. t.down()
  36. star(z,h)
  37. 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)
  38. national_flag( 5 , 8 , 2 , spin( 3 , 5 )+ 180 ) # The first small pentagram
  39. national_flag( 3 , 6 , 2 , spin( 1 , 7 )+ 180 ) # Second small pentagram
  40. national_flag( 3 , 3 , 2 , 180 -spin( 2 , 7 )) # The third small pentagram
  41. national_flag( 5 , 1 , 2 , 180 -spin( 4 , 5 )) # Fourth small pentagram
  42. t.hideturtle()
  43. turtle.done()

Tags: Draw standard five-star red flag with python turtle brush

Code articles python Development language

Related: Draw standard five-star red flag with python turtle brush