Editor

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.

Union Jack (UK Flag) in Python Turtle: the finished drawing
import math
import turtle
# The flag is 60 units wide and 30 units high, centred on (0, 0)
UNIT = 9
BLUE = "#012169"
RED = "#C8102E"
# 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
screen = turtle.Screen()
screen.title("Union Jack")
screen.bgcolor("#dde6ee")
t = turtle.Turtle()
t.speed(8)
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()
def cross(half_width, color):
"""An upright cross through the middle of the flag."""
polygon([(-half_width, 15), (half_width, 15), (half_width, -15), (-half_width, -15)], color)
polygon([(-30, half_width), (30, half_width), (30, -half_width), (-30, -half_width)], color)
# Blue background
polygon([(-30, 15), (30, 15), (30, -15), (-30, -15)], BLUE)
# White diagonals of St Andrew, from corner to corner
w = WHITE_HALF
polygon([(-30, -15), (-30, -15 + w), (30 - 2 * w, 15), (30, 15), (30, 15 - w), (-30 + 2 * w, -15)], "white")
polygon([(-30, 15), (-30 + 2 * w, 15), (30, -15 + w), (30, -15), (30 - 2 * w, -15), (-30, 15 - w)], "white")
# Red diagonals of St Patrick: in each corner the red sits on a different
# side of the centre line, so the flag looks the same turned 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 cross of St George: white, 10 units wide, with a red cross 6 units wide on top
cross(5, "white")
cross(3, RED)
t.hideturtle()
turtle.done()

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 two cross(...) lines to see the white saltire on blue that is the flag of Scotland.
  • Change UNIT = 9 to UNIT = 6 for a smaller flag, since every point is multiplied by it.
  • Change t.speed(8) to t.speed(2) and watch each red diagonal appear on its own side of the corner line.

What it uses

Every turtle command the site supports is listed on Python turtle online.