Union Jack (UK Flag) in Python Turtle
Advanced · Flags · 63 lines · draws in about 12 seconds
This program builds the Union Jack from filled polygons measured in 60 by 30 flag units, with the red diagonals shifted to the correct side in each corner.

How the code works
Working in flag units
The official design is 60 units by 30, so every point is written in those units and polygon() multiplies it by UNIT. The diagonals rise 1 for every 2 across, so a band 1 unit wide covers sqrt(5) / 2 units of height.
# The diagonals rise 1 unit for every 2 across, so a band 1 unit wide # (measured straight across it) is sqrt(5) / 2 units tall TALL = math.sqrt(5) / 2 WHITE_HALF = 3 * TALL # white diagonals: 3 units each side of the centre line RED_WIDTH = 2 * TALL # red diagonals: 2 units, on one side of the line
One function for every shape
polygon() goes to the first point, visits the others in order and comes back to the start while the fill is on. The background, the diagonals and the crosses are all polygons.
def polygon(points, color): """Fill the shape through a list of (x, y) points given in flag units.""" t.penup() t.goto(points[0][0] * UNIT, points[0][1] * UNIT) t.pendown() t.color(color) t.begin_fill() for x, y in points[1:]: t.goto(x * UNIT, y * UNIT) t.goto(points[0][0] * UNIT, points[0][1] * UNIT) t.end_fill()
Red diagonals on the correct side
The red diagonals are not in the middle of the white ones. Going out from the centre, each red band lies on the left of its corner line, so the top-left band is below the line, the top-right band is above it, and the flag looks the same upside down.
r = RED_WIDTH polygon([(0, 0), (30, 15), (30 - 2 * r, 15), (0, r)], RED) # top right: above the line polygon([(0, 0), (-30, 15), (-30, 15 - r), (0, -r)], RED) # top left: below the line polygon([(0, 0), (-30, -15), (-30 + 2 * r, -15), (0, -r)], RED) # bottom left: below the line polygon([(0, 0), (30, -15), (30, -15 + r), (0, r)], RED) # bottom right: above the line
The crosses go on top
The cross of St George is drawn last: a white cross 10 units wide, then a red cross 6 units wide. cross() draws the upright bar and the sideways bar with two calls to polygon().
cross(5, "white") cross(3, RED)
Make it yours
Change the code above and press Run. Some ideas:
- Put a
#in front of the four red diagonal lines and the twocross(...)lines to see the white saltire on blue that is the flag of Scotland. - Change
UNIT = 9toUNIT = 6for a smaller flag, since every point is multiplied by it. - Change
t.speed(8)tot.speed(2)and watch each red diagonal appear on its own side of the corner line.
What it uses
- lists of points
- functions with parameters
- coordinates and goto
- math.sqrt
Every turtle command the site supports is listed on Python turtle online.