Editor

Rangoli in Python Turtle

Intermediate · Festivals · 71 lines · draws in about 10 seconds

This program draws a symmetric rangoli by repeating one petal function around the centre, layer by layer, with white chalk lines and dots.

Rangoli in Python Turtle: the finished drawing
import turtle
screen = turtle.Screen()
screen.title("Rangoli")
screen.bgcolor("#1e1030")
t = turtle.Turtle()
t.speed(10)
t.pensize(2)
def petal(distance, angle, length, color):
"""A leaf-shaped petal that starts `distance` from the centre and points at `angle`."""
t.penup()
t.goto(0, 0)
t.setheading(angle)
t.forward(distance)
t.right(45)
t.pendown()
t.color("white", color)
t.begin_fill()
radius = length / 1.414 # two quarter circles make a petal this long
t.circle(radius, 90, steps=15)
t.left(90)
t.circle(radius, 90, steps=15)
t.end_fill()
def ring_of_dots(radius, count, size, color, turn=0):
"""Dots spaced evenly round a circle, the first one at angle `turn`."""
t.penup()
for number in range(count):
t.goto(0, 0)
t.setheading(turn + number * 360 / count)
t.forward(radius)
t.dot(size, color)
# A white chalk circle for the outer petals to stand on
t.penup()
t.goto(0, -140)
t.setheading(0)
t.pendown()
t.color("white")
t.circle(140, steps=80)
# Outer ring: 16 petals, pink and orange taking turns
for number in range(16):
if number % 2 == 0:
color = "#ff3d7f"
else:
color = "#ff9f1c"
petal(140, number * 360 / 16, 75, color)
# Eight big yellow petals, with eight teal petals in the gaps
for number in range(8):
petal(30, number * 45, 110, "#ffd60a")
for number in range(8):
petal(30, number * 45 + 22.5, 80, "#2ec4b6")
# White dots where the petals meet, and lilac dots between the outer petals
ring_of_dots(140, 16, 12, "white")
ring_of_dots(170, 16, 9, "#c77dff", turn=11.25)
# The centre: circles inside circles
t.goto(0, 0)
for size, color in [(110, "white"), (100, "#7b2cbf"), (70, "#ff3d7f"), (40, "#ffd60a"), (14, "white")]:
t.dot(size, color)
t.hideturtle()
turtle.done()

How the code works

A petal from two quarter circles

The turtle walks out from the centre, turns 45 degrees right, draws a quarter circle, turns 90 degrees left and draws another quarter circle back. The two curves make a leaf shape pointing at angle.

def petal(distance, angle, length, color):
    """A leaf-shaped petal that starts `distance` from the centre and points at `angle`."""
    t.penup()
    t.goto(0, 0)
    t.setheading(angle)
    t.forward(distance)
    t.right(45)
    t.pendown()
    t.color("white", color)
    t.begin_fill()
    radius = length / 1.414  # two quarter circles make a petal this long
    t.circle(radius, 90, steps=15)
    t.left(90)
    t.circle(radius, 90, steps=15)
    t.end_fill()

Dots in a ring

For each dot the turtle goes back to the centre, faces turn + number * 360 / count and walks out radius steps, so the dots are evenly spaced round the circle.

def ring_of_dots(radius, count, size, color, turn=0):
    """Dots spaced evenly round a circle, the first one at angle `turn`."""
    t.penup()
    for number in range(count):
        t.goto(0, 0)
        t.setheading(turn + number * 360 / count)
        t.forward(radius)
        t.dot(size, color)

Sixteen petals in two colours

number % 2 is 0 for even numbers and 1 for odd ones, so pink and orange take turns. Each petal is turned 360 / 16 = 22.5 degrees from the last.

for number in range(16):
    if number % 2 == 0:
        color = "#ff3d7f"
    else:
        color = "#ff9f1c"
    petal(140, number * 360 / 16, 75, color)

Two layers of eight petals

Later shapes cover earlier ones, so the eight yellow petals go down first and the eight teal petals sit on top of them in the gaps, turned 22.5 degrees further round.

for number in range(8):
    petal(30, number * 45, 110, "#ffd60a")
for number in range(8):
    petal(30, number * 45 + 22.5, 80, "#2ec4b6")

Make it yours

Change the code above and press Run. Some ideas:

  • Change range(16) to range(24) and 360 / 16 to 360 / 24 for a busier outer ring.
  • Change the yellow petal length from 110 to 130 and watch them push into the outer ring.
  • Swap in your own colours, for example "#ff3d7f" to "crimson" and "#2ec4b6" to "limegreen".

What it uses

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