Colourful Random Walks in Processing

A language suited for quickly prototyping your ideas

Processing is a great language for quickly prototyping and designing visual representations of processes. Built on top of Java, it provides a great layer of abstraction that takes away a lot of the verbosity of Java, allowing you to focus purely on creating your sketch (what Processing calls its programs). A quick search online will reveal some wonderful visual eye-candy created within the language, as well as representations of complex data processes, such as fractals and cellular automata, to evolutionary, physics and A.I simulations. Indeed, it’s a language well-suited to scientists and mathematicians just as it is to graphic designers and digital artists.

It’s also supported by a whole host of user-created libraries to extend to the language even further, offering a good level of extensibility in many different areas of programming.


The Sketch

Just as an example of what is possible in a short amount of time with Processing, I decided to experiment with random walks. I wanted to visually represent the walk by changing its colour at every single step, however the colours changed so frequently that it looked like a jumbled mess. It looked more like image noise than any sort of representation of a Random Walk, so that wasn’t going to work.

So, instead of randomly changing colour at every step/iteration, I added in a counter that changed the walker to a random colour value every 5000 iterations;

A random walk in processing

A random walk in Processing using random RGB values.

While this is visually appealing to look at when the walk doesn’t cover too much of the application window (right), it quickly becomes rather garish after many iterations, as is apparent on the left-hand side of the image.

So in the next version of the sketch, I created an array of colours (represented by hex values) that worked well together, and passed that array into my walker object’s constructor. Inside the Walker class, I pick a random index value from the array, and assign the hex value stored at that position in the array to be the colour of the walker every 5000 steps;

A random walk in processing

A random walk in Processing using a random hex value from a predefined set of colours.

Many more iterations later

Even after many iterations, with fewer colours, and by using a complementary colour-set, the effect is much nicer to look at;

A random walk in processing

Many more iterations later.

All of this took surprisingly little code, thanks to Processing. A little under 70 lines of code, including white-space. A great language to learn, whether you’re new to programming, or an experienced programmer.

Because Processing abstracts out a lot of the underlying Java code, it allows you to be much more succinct, without compromising on any of the syntactic sugar that makes Java an easy object-oriented language to work with.

Leave a Reply

Your email address will not be published. Required fields are marked *