Prerequisite Knowledge

A fractal is a geometric shape that contains infinite detail, and the detail is similar to the fractal itself. If you zoom in to a fractal, you’ll see the same structure with different scales repeatedly and endlessly.

Google search for fractals:

Fractal is a popular element in computer art. They are usually generated with Complex dynamics, Chaos game, or recursive programs.

More information: https://en.wikipedia.org/wiki/Fractal

Make Fractals in Grapycal

This tutorial goes with the example file “fractal.grapycal”.

A surprisingly easy way to produce a fractal is applying binary operations on integers.

We can see that the expression [[x&y for x in range(64)] for y in range(64)] produces a Sierpinski triangle.

The expression means: Create an image of 64*64 pixels, fill each pixel with x&y, where x and y are the coordinate of a pixel, and the & is the binary and operation.

To better observe what’s going on, change 64 to be a smaller number and add a PrintNode. It shows that the ExecuteNode produces an matrix, where each pixel has one value, and DisplayImageNode renders the values into an image using a color gradient.

Edit the Expression

Try to:

  1. Change the x&y part of the expression to create different fractals. The fun part is it’s not quite predictable of the outcome.

  2. Enlarge the image by raising the number in range(64). Also enlarge the DisplayImageNode.

Edit the Rendering Parameters

Select a DisplayImageNode. Find the node’s parameters in the inspector (right sidebar). The cmap means the color map that translates the values to pixel color. vmin and vmax controls the range of value that is mapped to the entire color map.

What’s Next?

If you like fractals, see also Mandelbrot Set.

If you have grapycal_torch installed, transform the fractal with a neural network. Maybe train a model that generates fractals, and incorporate it with usual mathematical techniques for generating fractals.

Consider print the fractals out for decoration in real life.

References