Simple Computer Sketch Algorithm

I spent an hour or so this morning programming a little algorithm to generate a new image from a reference through only drawing lines. The algorithm I devised is really very simple and occupies only a few lines of code. First lets see it in action:

The basic idea is to have a conceptual pen on the screen. The location of this pen is randomized every 5 seconds as well as at the start of the application. As each frame or ‘tick’ is processed we make around 64 movements of this virtual pen so that the image emerges quite fast. At the start of each move of the pen, we sample the colour of our reference image relative to where the pen is on the screen, and we take that as the drawing colour. The distance that the pen travels each iteration is chosen at random but the direction is less then random. The colour of 16 random points around the pen are sampled, each point being potential destinations for the pen, so they are all the same distance away from the pens current location. We can finally proceed to move the pen to one of the locations with the most similar colour of pixel, leaving a line as we go. It is probably related to a random walk algorithm, with a small guidance heuristic making the pen prefer areas of the reference image with similar colored pixels. If we did not forcefully randomize the position of the pen every 5 seconds or so then the pen can get stuck in sections of the reference image.

Source code is here:

sketcher.cpp