Koch snowflake
Koch snowflake fractal.
Simple Python/Tkinter visualization of the Koch snowflake fractal.
Developed to explore, understand, and teach how fractals work.
What It Does
The script starts from a single equilateral triangle and recursively transforms each edge into four smaller segments, producing the classic Koch snowflake pattern. The drawing is animated so you can watch the fractal emerge step by step.
Highlights:
- recursive edge subdivision
- equilateral triangle point construction
- animated rendering on a
Tkinter.Canvas - rainbow coloring for the final recursion level
- optional labels for selected iteration counts
Requirements
- Python 3
- Tkinter support in your Python installation
Run
python snowflake.py A window will open and begin drawing the snowflake after a short delay.
How It Works
The main flow in snowflake.py is:
- create a base equilateral triangle
- split each edge into thirds
- build the outward peak of a smaller equilateral triangle on the middle third
- recurse into the four resulting edge segments
- draw the deepest-level segments to visualize the fractal
Important functions:
thirds(p1, p2)computes the one-third and two-third points of a segmentget_3rd_point(p1, p2, o)finds the third vertex of an equilateral triangleprocess_edge(canvas, p1, p2)performs the recursive Koch edge constructiontriangle(canvas, p1, p2)creates the initial triangle and starts the recursion
Tuning The Visualization
You can experiment by editing these values in snowflake.py:
max_levelcontrols recursion depthp1andp2control the size and position of the starting trianglewinsideprocess_edge()changes the orientation behavior and can produce alternate shapes such as an anti-snowflaketime.sleep(0.01)controls drawing speed
Notes
- Higher recursion levels increase the number of drawn segments quickly.
- The script uses global counters to track the current recursion depth and drawing iteration.
- This project is best suited for learning, experimentation, and visual explanation rather than performance-heavy rendering.
Images