Gingerbread man fractal with generativepy

Martin McBride, 2021-06-12
Tags gingerbread man fractal
Categories generativepy generative art

This article has been moved to my blog. Please refer to that article as it might be more up to date.

The gingerbread man is another fractal that works in a similar way to the tinkerbell fractal. It is worth reading the tinkerbell fractal article before tackling the gingerbread man fractal in this article.

Gingerbread man formula

The fractal equations for gingerbread man are:

xnext = 1 - y + abs(x)
ynext = x

There are no parameters, and the required initial values are:

x = -0.01  # Initial value
y = 0      # Initial value

For this fractal, we are reusing the black and white version of the tinkerbell fractal. That mean each pixel will be black if the algorithm lands there one or more times, or it will be white if the algorithm never lands there.

We could count the the number of times each pixel is visited, and give each count a different colour, like we did for the coloured tinkerball fractal. But it turns out that isn't very interesting, it just looks like random noise. The black and white image is more striking.

Here is the result:

The code

Here is the full code for the image above:

from generativepy.bitmap import Scaler
from generativepy.nparray import make_nparray

MAX_COUNT = 1000000

def paint(image, pixel_width, pixel_height, frame_no, frame_count):
    scaler = Scaler(pixel_width, pixel_height, width=12, startx=-3.5, starty=-3.5)

    x = -0.1
    y = 0.0
    for i in range(MAX_COUNT):
        x, y = 1 - y + abs(x), x
        px, py = scaler.user_to_device(x, y)
        image[py, px] = 0

make_nparray('gingerbread.png', paint, 600, 600, channels=1)
If you found this article useful, you might be interested in the book Computer Graphics in Python or other books by the same author.


Popular tags

2d arrays abstract data type alignment and animation arc array arrays behavioural pattern bezier curve built-in function callable object chain circle classes clipping close closure cmyk colour combinations comparison operator comprehension context context manager conversion count creational pattern data types design pattern device space dictionary drawing duck typing efficiency ellipse else encryption enumerate fill filter font font style for loop function function composition function plot functools game development generativepy tutorial generator geometry gif gradient greyscale higher order function hsl html image image processing imagesurface immutable object index inner function input installing iter iterable iterator itertools l system lambda function len line linear gradient linspace list list comprehension logical operator lru_cache magic method mandelbrot mandelbrot set map monad mutability named parameter numeric python numpy object open operator optional parameter or partial application path pattern permutations polygon positional parameter print pure function python standard library radial gradient range recipes rectangle recursion reduce repeat rgb rotation roundrect scaling sector segment sequence setup shape singleton slice slicing sound spirograph sprite square str stream string stroke structural pattern subpath symmetric encryption template text text metrics tinkerbell fractal transform translation transparency triangle tuple turtle unpacking user space vectorisation webserver website while loop zip