Koch Curve, Sierpinski Triangle, and Mandelbrot Set - Python Turtle
Fractals are captivating geometric shapes that exhibit self-similarity and infinite complexity. In this tutorial, we will explore three famous fractals - the Koch Curve, the Sierpinski Triangle, and the Mandelbrot Set - and learn how to create them using Python Turtle.
1. Koch Curve
The Koch Curve is a beautiful fractal with a simple recursive definition. It starts with an equilateral triangle and replaces each line segment with four smaller segments, forming a zigzag pattern. Here's how to draw the Koch Curve using Python Turtle:
import turtle
def koch_curve(t, order, size):
if order == 0:
t.forward(size)
else:
for angle in [60, -120, 60, 0]:
koch_curve(t, order - 1, size / 3)
t.left(angle)
window = turtle.Screen()
window.bgcolor("white")
koch_turtle = turtle.Turtle()
koch_turtle.speed(0)
koch_turtle.color("blue")
koch_turtle.penup()
koch_turtle.goto(-150, -50)
koch_turtle.pendown()
koch_curve(koch_turtle, 4, 300)
window.mainloop()
The koch_curve() function uses recursion to draw the Koch Curve. It repeatedly calls itself to draw smaller segments while turning the turtle at specific angles to create the zigzag pattern.
2. Sierpinski Triangle
The Sierpinski Triangle is another classic fractal pattern. It consists of equilateral triangles recursively subdivided into smaller triangles. Here's how to draw the Sierpinski Triangle using Python Turtle:
import turtle
def draw_triangle(t, size):
for _ in range(3):
t.forward(size)
t.left(120)
def draw_sierpinski(t, order, size):
if order == 0:
draw_triangle(t, size)
else:
draw_sierpinski(t, order - 1, size / 2)
t.forward(size / 2)
draw_sierpinski(t, order - 1, size / 2)
t.backward(size / 2)
t.left(60)
t.forward(size / 2)
t.right(60)
draw_sierpinski(t, order - 1, size / 2)
t.left(60)
t.backward(size / 2)
t.right(60)
window = turtle.Screen()
window.bgcolor("white")
sierpinski_turtle = turtle.Turtle()
sierpinski_turtle.speed(0)
sierpinski_turtle.color("green")
sierpinski_turtle.penup()
sierpinski_turtle.goto(-100, -50)
sierpinski_turtle.pendown()
draw_sierpinski(sierpinski_turtle, 3, 300)
window.mainloop()
The draw_triangle() function is used to draw an equilateral triangle, and the draw_sierpinski() function is a recursive function to draw the Sierpinski Triangle by subdividing triangles into smaller ones.
3. Mandelbrot Set
The Mandelbrot Set is a mesmerizing fractal pattern in complex numbers. It is represented in the complex plane, and points within the set have values that remain bounded under a specific mathematical iteration. Here's how to visualize the Mandelbrot Set using Python Turtle:
import turtle
def mandelbrot(c):
z = 0
n = 0
while abs(z) <= 2 and n < 100:
z = z * z + c
n += 1
return n
window = turtle.Screen()
window.bgcolor("white")
mandelbrot_turtle = turtle.Turtle()
mandelbrot_turtle.speed(0)
for y in range(-300, 301, 10):
for x in range(-300, 301, 10):
c = complex(x / 200, y / 200)
m = mandelbrot(c)
if m == 100:
mandelbrot_turtle.penup()
mandelbrot_turtle.goto(x, y)
mandelbrot_turtle.pendown()
mandelbrot_turtle.dot(5, "blue")
window.mainloop()
The mandelbrot() function calculates the iteration count for a given complex number c to determine if it belongs to the Mandelbrot Set. The mandelbrot_turtle then visualizes the points within the set by plotting blue dots on the screen.
Mistakes to Avoid
- Forgetting to specify the base case in recursive functions, leading to infinite recursion.
- Using the wrong iteration count or step size in fractal patterns, resulting in incorrect shapes.
- Not handling complex numbers properly when drawing the Mandelbrot Set, causing errors in visualization.
FAQs
-
What are fractals?
Fractals are complex geometric shapes with self-similarity and infinite complexity. They are created using recursive mathematical processes. -
How do I draw the Koch Curve?
The Koch Curve can be drawn using Python Turtle by defining a recursive function to create smaller zigzag segments and turning the turtle at specific angles. -
What is the Sierpinski Triangle?
The Sierpinski Triangle is a fractal pattern made of equilateral triangles recursively subdivided into smaller triangles. -
How does the Mandelbrot Set work?
The Mandelbrot Set is defined in the complex plane, and points within the set remain bounded under a specific mathematical iteration, while those outside the set diverge to infinity. -
Can I zoom into the Mandelbrot Set?
Yes, you can adjust the range of coordinates to zoom into specific regions of the Mandelbrot Set and explore its intricate patterns.
Summary
Koch Curve, Sierpinski Triangle, and Mandelbrot Set are fascinating fractals that offer a glimpse into the beauty of mathematics and geometry. By understanding their recursive nature, you can create these mesmerizing patterns using Python Turtle. Experiment with different iterations and sizes to reveal the intricacies and infinite complexity of these captivating fractals.