Gingerbread man fractal with generativepy

Martin McBride, 2020-11-01
Tags gingerbread man fractal
Categories generativepy generative art

generativepy has moved to the website, and this page might not be up to date. Please visit the new location.

The Gingerbread man fractal is based on an iterated function. Here we will look at an implementation using generativepy.

The basic algorithm is similar to the King's Dream fractal. It is best to read that article first, as this section mainly highlights the differences between them.

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

The code also modifies the scaling factors (in the Scaler constructor) to fit the size of the fractal.

Notice that the code is based on the uncoloured version of Kings Dream. For this fractal, the shape is quite interesting, but the distribution of counts within the shape is just random noise that doesn't really add anything.

Here is the result:

The code

Here is the full code for the image above:

from generativepy.bitmap import make_bitmap, Scaler
from generativepy.color import Color
import numpy as np
import math

MAX_COUNT = 1000000
BLACK = Color(0)
WHITE = Color(1)

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

    counts = np.zeros([pixel_height, pixel_width], np.int32)

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

    for px in range(pixel_width):
        for py in range(pixel_height):
            col = BLACK if counts[py, px]==0 else WHITE
            image.putpixel((px, py), col.as_rgb_bytes())

make_bitmap('gingerbread.png', paint, 600, 600)

See also

See the iterated functions article for a list of other fractal examples.