Learning Processing (Python mode)

Sketches from spring 2021 made while learning Processing in Python mode — chaos game, fractals, random walks, ripples and a simple disease transmission simulator.

Source ↗

Cover image for Learning Processing (Python mode)

Overview

These are sketches I made while learning Processing using its Python mode in 2021. Most of them are based on challenges from The Coding Train, which are written in Java (Processing) or JavaScript (P5.js), so a big part of the learning was porting them to Python. Some of them I took a bit further. Each sketch is short, and the source for all of them is on my GitHub.

The Chaos Game

The chaos game is one of my favorites. You start with the corners of a polygon and a random point. Then you pick a random corner, move the point part of the way towards it, plot it, and repeat. With a triangle, this produces a Sierpinski triangle, which is surprising for something that is completely random.

With a square or more sides, the same rule just fills up the shape. To get a fractal, I added one restriction: the same corner cannot be picked twice in a row. The sketch goes through polygons from 3 to 10 sides.

Barnsley Fern

The Barnsley fern works on a similar idea. Here, there are four transformations and each one is picked with a set probability. One draws the stem, one draws the smaller and smaller leaflets, and the other two draw the bottom left and right leaflets. Applying them again and again to a single point draws the fern.

Random Walk

A random walker on a grid that moves one step up, down, left or right at random. In this version, if the walker lands on a spot it has already visited, it keeps walking until it finds a new one. The color changes slowly as it walks, and the sketch stops after 10,000 steps.

Snowflakes

These snowflakes are made using Brownian motion. A particle starts at the edge and moves inwards with a small random jitter until it touches the snowflake, and then it sticks. This is called diffusion-limited aggregation. The particles only move inside a thin 30° slice, which is then mirrored and rotated six times to get the six-fold symmetry of a snowflake.

Fractal Trees

Fractal trees are made with recursion. Each branch draws two smaller branches at a random angle and a random length, until the branches are small enough to be leaves. Since the angles and lengths are random, a new tree is drawn every frame.

Menger Sponge

The Menger sponge is a 3D fractal. A cube is divided into 27 smaller cubes, and the center cube and the center cube of each face are removed, leaving 20. Then the same is done to each of those 20 cubes. The number of cubes goes up very quickly: 1, 20, 400, 8,000.

Ripples in 2D

This is the classic water ripple effect. It uses two buffers, and each pixel is calculated from its four neighbours in the previous frame, with some damping so the waves die out. Coming from a simulation background, it was fun to see that this is basically a finite difference solution of the wave equation. You can drag the mouse to make ripples.

Transmission Simulator

I made this during COVID. It is a very simple disease transmission simulator. There are 500 people moving around randomly, and one of them is infected. Anyone who comes close enough to an infected person also gets infected. There is no recovery in this version, so eventually everyone gets infected. It is far from a real model, but it was interesting to see how quickly it spreads.

slowed down 4×

Hyperspace

Stars flying towards the screen, like jumping to hyperspace in Star Wars. Each star’s position is divided by its depth to get the perspective, and a line is drawn from where it was to where it is to get the streaks.

Circle Packing

Circles are placed at random points that are not already inside another circle. Each circle takes its color from the image at that point and grows until it touches the edge or another circle. The sketch stops when it cannot find any more space. I used The Starry Night for this one.

Flowery Wallpapers

This one came out of a generative art workshop. It draws a circle whose radius is changed with perlin noise, and every frame the circle gets a bit bigger, rotates a little and changes color. Since the screen is never cleared, the layers build up into flower-like shapes. When the circle gets bigger than the screen, the sketch saves the image as a wallpaper. I made them in phone and desktop sizes. Some of my favorites are below.

Generated flowery desktop wallpaper 1Generated flowery desktop wallpaper 2Generated flowery desktop wallpaper 3Generated flowery desktop wallpaper 4Generated flowery desktop wallpaper 5Generated flowery desktop wallpaper 6Generated flowery desktop wallpaper 7Generated flowery desktop wallpaper 8Generated flowery desktop wallpaper 9
Generated flowery phone wallpaper 1Generated flowery phone wallpaper 2

Render Only

The source code for these two is missing, but the renders are on YouTube.

References