Files
priya-sundaram-dev 1813f7d7d8 Clean up the fractals directory (#15099)
* Clean up the fractals directory

Add a directory README plus two new, fully doctested fractal generators.

- README.md: overview table of every fractal, how to run them, and further
  reading. Distinguishes the visual (turtle/matplotlib) demos from the
  pure-computation ones that run under doctest in CI.
- sierpinski_carpet.py: integer-arithmetic Sierpinski carpet. is_filled(),
  generate_carpet() and count_filled_cells() are all doctested; no display
  needed.
- barnsley_fern.py: the Barnsley fern iterated function system. transform(),
  choose_transformation() and generate_fern() are doctested and made
  deterministic via an optional seed; matplotlib plotting is optional and
  guarded behind __main__.

* fractals: inline demo code under __main__ (address keeper-bot doctest note)
2026-08-28 08:10:48 +02:00

45 lines
1.9 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Fractals
A fractal is a geometric figure that is *self-similar*: zooming into a piece of
it reveals a copy of the whole. Fractals show up in mathematics, physics,
computer graphics and even biology (coastlines, ferns, snowflakes).
This directory collects small, self-contained fractal generators. They fall
into two groups:
- **Visual demos** that open a window (via `turtle`) or produce an image
(via `matplotlib`/`PIL`). Run these directly to see the picture.
- **Pure-computation** generators whose output can be checked with `doctest`,
so they run in CI without a display.
## Contents
| File | Fractal | Output | Notes |
| ---- | ------- | ------ | ----- |
| [`barnsley_fern.py`](barnsley_fern.py) | Barnsley fern | matplotlib (optional) | Iterated function system; deterministic with a seed |
| [`julia_sets.py`](julia_sets.py) | Julia sets | matplotlib | Complex-plane escape-time fractal |
| [`koch_snowflake.py`](koch_snowflake.py) | Koch snowflake | matplotlib | Line-segment subdivision |
| [`mandelbrot.py`](mandelbrot.py) | Mandelbrot set | PIL image | Complex-plane escape-time fractal |
| [`sierpinski_carpet.py`](sierpinski_carpet.py) | Sierpinski carpet | text | Integer arithmetic, fully doctested |
| [`sierpinski_triangle.py`](sierpinski_triangle.py) | Sierpinski triangle | turtle | Recursive midpoint subdivision |
| [`vicsek.py`](vicsek.py) | Vicsek fractal | turtle | Recursive cross pattern |
## Running
```bash
# text fractal – prints to the terminal
python fractals/sierpinski_carpet.py
# image fractal – opens a matplotlib window (needs matplotlib)
python fractals/barnsley_fern.py
# turtle fractal – opens a drawing window (needs a display)
python fractals/vicsek.py
```
## Further reading
- Benoit B. Mandelbrot, *The Fractal Geometry of Nature* (1982)
- Michael Barnsley, *Fractals Everywhere* (1988)
- <https://en.wikipedia.org/wiki/Fractal>