Skip to content

Instantly share code, notes, and snippets.

@cavedave
Created May 3, 2025 11:13
Show Gist options
  • Save cavedave/348ee93a49098a2f7ff8c9dd0618bd75 to your computer and use it in GitHub Desktop.
Save cavedave/348ee93a49098a2f7ff8c9dd0618bd75 to your computer and use it in GitHub Desktop.
#
# Python script to create USA flag using turtle.
# Author - https://www.pythoncircle.com
# Original Author - https://www.codesters.com/preview/6fe8df0ccc45b1460ab24e5553497c5684987fb5/
import turtle
import time
from PIL import Image
import math
# create a screen
screen = turtle.getscreen()
# set background color of screen
screen.bgcolor("white")
# set tile of screen
screen.title("USA Flag - https://www.pythoncircle.com")
# "Yesterday is history, tomorrow is a mystery,
# but today is a gift. That is why it is called the present.”
# — Oogway to Po, under the peach tree, Kung Fu Panda Movie
oogway = turtle.Turtle()
# set the cursor/turtle speed. Higher value, faster is the turtle
oogway.speed(100)
oogway.penup()
# decide the shape of cursor/turtle
oogway.shape("turtle")
# flag height to width ratio is 1:1.9
flag_height = 250
flag_width = 475
# starting points
# start from the first quardant, half of flag width and half of flag height
start_x = -237
start_y = 125
# For red and white stripes (total 13 stripes in flag), each strip width will be flag_height/13 = 19.2 approx
stripe_height = flag_height/13
stripe_width = flag_width
# length of one arm of star
star_size = 6
def draw_fill_rectangle(x, y, height, width, color):
oogway.goto(x,y)
oogway.pendown()
oogway.color(color)
oogway.begin_fill()
oogway.forward(width)
oogway.right(90)
oogway.forward(height)
oogway.right(90)
oogway.forward(width)
oogway.right(90)
oogway.forward(height)
oogway.right(90)
oogway.end_fill()
oogway.penup()
def draw_star(x, y, color, radius):
# Generate points for a 5-pointed star centered at (x, y)
points = []
for i in range(10):
angle_deg = 90 + i * 36 # Start from top (90°)
angle_rad = math.radians(angle_deg)
r = radius if i % 2 == 0 else radius * 0.382 # inner radius ≈ golden ratio
px = x + r * math.cos(angle_rad)
py = y + r * math.sin(angle_rad)
points.append((px, py))
oogway.penup()
oogway.goto(points[0])
oogway.color(color, color)
oogway.begin_fill()
oogway.pendown()
for px, py in points[1:] + [points[0]]:
oogway.goto(px, py)
oogway.end_fill()
oogway.penup()
# this function is used to create 13 red and white stripes of flag
def draw_stripes():
x = start_x
y = start_y
# we need to draw total 13 stripes, 7 red and 6 white
# so we first create, 6 red and 6 white stripes alternatively
for stripe in range(0,6):
for color in ["red", "white"]:
draw_fill_rectangle(x, y, stripe_height, stripe_width, color)
# decrease value of y by stripe_height
y = y - stripe_height
# create last red stripe
draw_fill_rectangle(x, y, stripe_height, stripe_width, 'red')
y = y - stripe_height
# this function create navy color square
# height = 7/13 of flag_height
# width = 0.76 * flag_height
# check references section for these values
def draw_square():
square_height = (7/13) * flag_height
square_width = (0.76) * flag_height
draw_fill_rectangle(start_x, start_y, square_height, square_width, 'navy')
def draw_six_stars_rows():
gap_between_stars = 30
gap_between_lines = stripe_height + 6
y = 112
# create 5 rows of stars
for row in range(0,5) :
x = -222
# create 6 stars in each row
for star in range (0,6) :
draw_star(x, y, 'white', star_size)
x = x + gap_between_stars
y = y - gap_between_lines
def draw_star_field_51():
union_height = (7/13) * flag_height
union_width = 0.76 * flag_height
#layout = [6, 5, 6, 5, 6, 5, 6, 6, 6] # total 51 stars
#layout = [6, 5, 6, 5, 6, 6, 6, 5, 6]
#layout = [7, 6, 7, 6, 7, 6, 7, 6] #52
layout = [10, 10, 10, 10, 10, 10, 10, 10] #60
rows = len(layout)
max_cols = max(layout)
horiz_gap = union_width / (max_cols + 1)
vert_gap = union_height / (rows + 1)
star_radius = min(horiz_gap, vert_gap) * 0.3
for row_index, stars_in_row in enumerate(layout):
y = start_y - vert_gap * (row_index + 1)
for col in range(stars_in_row):
x = start_x + horiz_gap * (col + 1)
if stars_in_row < max_cols:
x += horiz_gap / 2 # center the shorter rows
draw_star(x, y, 'white', star_radius)
def draw_star_field():
union_height = (7/13) * flag_height
union_width = 0.76 * flag_height
rows = 9
max_cols = 6
# Compute true horizontal and vertical spacing
horiz_gap = union_width / (max_cols + 1)
vert_gap = union_height / (rows + 1)
# Reuse the same radius scaling, now spaced correctly
star_radius = min(horiz_gap, vert_gap) * 0.3
#star_radius = 0.0308 * flag_height # diameter = 0.0616
rows = 9
for row in range(rows):
stars_in_row = 6 if row % 2 == 0 else 5
y = start_y - vert_gap * (row + 1) + (vert_gap / 2)
for col in range(stars_in_row):
x = start_x + horiz_gap * (col + 1)
if stars_in_row == 5:
x += horiz_gap / 2 # center the shorter rows
draw_star(x, y, 'white', star_radius)
def draw_five_stars_rows():
gap_between_stars = 30
gap_between_lines = stripe_height + 6
y = 100
# create 4 rows of stars
for row in range(0,4) :
x = -206
# create 5 stars in each row
for star in range (0,5) :
draw_star(x, y, 'white', star_size)
x = x + gap_between_stars
y = y - gap_between_lines
# start after 5 seconds.
time.sleep(5)
# draw 13 stripes
draw_stripes()
draw_square()
#draw_star_field()
draw_star_field_51()
# draw 30 stars, 6 * 5
#draw_six_stars_rows()
# draw 20 stars, 5 * 4. total 50 stars representing 50 states of USA
#draw_five_stars_rows()
# hide the cursor/turtle
oogway.hideturtle()
# Save the canvas to a PostScript file
ts = turtle.getcanvas()
ts.postscript(file="us_flag.eps")
# Now convert to PNG using Pillow
img = Image.open("us_flag.eps")
img.save("us_flag60.png")
# keep holding the screen until closed manually
screen.mainloop()
@cavedave
Copy link
Author

cavedave commented May 3, 2025

us_flag60

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment