## Turn A Square: generative aRt

A while back I visited Artistes & Robots in Paris. Part of the exhibition was on the origins of computer-based art. Nowadays this is referred to as generative art, where computers generate artwork according to rules specified by the programmer. I wanted to emulate some of the early generative artwork I saw there, using R.

Some examples of early generative art

Georg Nees was a pioneer of computer-based artwork and he has a series of pieces using squares. An example I found on the web was Schotter (1968).

Another example using rotation of squares is Boxes by William J. Kolomyjec.

I set out to generate similar images where the parameters can be tweaked to adjust the final result. The code is available on GitHub. Here is a typical image. Read on for an explanation of the code.

Generating a grid of squares

I started by generating a grid of squares. We can use the segment command in R to do the drawing.

```xWave <- seq.int(1:10)
yWave <- seq.int(1:10)

for (i in seq_along(xWave)) {
xCentre <- xWave[i]
for (j in seq_along(yWave)) {
yCentre <- yWave[j]
lt <- c(xCentre - 0.4,yCentre - 0.4)
rt <- c(xCentre + 0.4,yCentre - 0.4)
rb <- c(xCentre + 0.4,yCentre + 0.4)
lb <- c(xCentre - 0.4,yCentre + 0.4)
new_shape_start <- rbind(lt,rt,rb,lb)
new_shape_end <- rbind(rt,rb,lb,lt)
new_shape <- cbind(new_shape_start,new_shape_end)
if(i == 1 &amp;&amp; j == 1) {
multiple_segments <- new_shape
} else {
multiple_segments <- rbind(multiple_segments,new_shape)
}
}
}
par(pty="s")
plot(0, 0,
xlim=c(min(xWave)-1,max(xWave)+1),
ylim=c(min(yWave)-1,max(yWave)+1),
col = "white", xlab = "", ylab = "", axes=F)
segments(x0 = multiple_segments[,1],
y0 = multiple_segments[,2],
x1 = multiple_segments[,3],
y1 = multiple_segments[,4])
```

We’re using base R graphics to make this artwork – nothing fancy. Probably the code can be simplified!

The next step was to add some complexity and flexibility. I wanted to be able to control three things:

1. the size of the grid
2. the grout (distance between squares)
3. the amount of hysteresis (distorting the squares, rather than rotating them).

Here is the code. See below for some explanation and examples.

```# this function will make grid art
# arguments define the number of squares in each dimension (xSize, ySize)
# grout defines the gap between squares (none = 0, max = 1)
# hFactor defines the amount of hysteresis (none = 0, max = 1, moderate = 10)
make_grid_art <- function(xSize, ySize, grout, hFactor) {
xWave <- seq.int(1:xSize)
yWave <- seq.int(1:ySize)
axMin <- min(min(xWave) - 1,min(yWave) - 1)
axMax <- max(max(xWave) + 1,max(yWave) + 1)
nSquares <- length(xWave) * length(yWave)
x <- 0
halfGrout <- (1 - grout) / 2
for (i in seq_along(yWave)) {
yCentre <- yWave[i]
for (j in seq_along(xWave)) {
if(hFactor < 1) {
hyst <- rnorm(8, halfGrout, 0)
}
else {
hyst <- rnorm(8, halfGrout, sin(x / (nSquares - 1) * pi) / hFactor)
}
xCentre <- xWave[j]
lt <- c(xCentre - hyst[1],yCentre - hyst[2])
rt <- c(xCentre + hyst[3],yCentre - hyst[4])
rb <- c(xCentre + hyst[5],yCentre + hyst[6])
lb <- c(xCentre - hyst[7],yCentre + hyst[8])
new_shape_start <- rbind(lt,rt,rb,lb)
new_shape_end <- rbind(rt,rb,lb,lt)
new_shape <- cbind(new_shape_start,new_shape_end)
if(i == 1 &amp;&amp; j == 1) {
multiple_segments <- new_shape
} else {
multiple_segments <- rbind(multiple_segments,new_shape)
}
x <- x + 1
}
}
par(mar = c(0,0,0,0))
plot(0, 0,
xlim=c(axMin,axMax),
ylim=c(axMax,axMin),
col = "white", xlab = "", ylab = "", axes=F, asp = 1)
segments(x0 = multiple_segments[,1],
y0 = multiple_segments[,2],
x1 = multiple_segments[,3],
y1 = multiple_segments[,4])
}
```

The amount of hysteresis is an important element. It determines the degree of distortion for each square. The distortion is done by introducing noise into the coordinates for each square that we map onto the grid. The algorithm used for the distortion is based on a half-cycle of a sine wave. It starts and finishes on zero distortion of the coordinates. In between it rises and falls symmetrically introducing more and then less distortion as we traverse the grid.

Changing the line of code that assigns a value to hyst will therefore change the artwork. Let’s look at some examples.

```# square grid with minimal hysteresis
make_grid_art(10,10,0.2,50)
```
```# square grid (more squares) more hysteresis
make_grid_art(20,20,0.2,10)
```
```# rectangular grid same hysteresis
make_grid_art(25,15,0.2,10)
```
```# same grid with no hysteresis
make_grid_art(25,15,0.2,0)
```
```# square grid moderate hysteresis and no grout
make_grid_art(20,20,0,10)
```

Hopefully this will give you some ideas for simple schemes to generate artwork in R. I plan to add to the GitHub repo when get the urge. There is already some other generative artwork code in there for Igor Pro. Two examples are shown below (I don’t think I have written about this before on quantixed).

The post title comes from “Turn A Square” by The Shins from their album Chutes Too Narrow.

## Multiplex: Small multiple artwork from GPX tracks

I’d seen the small multiple artwork of running and cycling routes from Marcus Volz’s R package Strava all over the web. Ads for “posters of your GPS tracks” pop up on Reddit and I’d notice a few #Rstats people put up their posters on Twitter. I’ve had the package bookmarked for a while and this week I finally got round to generating a small multiple poster of some of my cycling routes.

I was pleased with the result and wanted to post it here. But also, running the code was not straightforward as I’ll explain below. If you want to generate your own plot read on.

The idea behind the poster is really nice. You get a kind of generative art-style poster. It looks nice and you can identify individual routes which is fun to do.

The instructions on the GitHub page are absolutely correct and the code should run out-of-the-box. The idea is that you download your Strava data and then make your plots. Unfortunately, it seems that a change in Strava’s data export policy (possibly related to GDPR changes) has broken the package. I found that there are two problems. First, Strava’s “download your data” link gives you a mix of formats (in my case GPX and FIT files), the package only works with GPX. Second, if there is any elevation data missing from a track, the data frame that is needed to make the poster is not built properly.

Going GPX only: In my case, I don’t keep all my data in Strava and instead use a local repository managed with RubiTrack. This software allows me to filter for the tracks I want and export them in GPX format. The only problem is that it generates one huge file with all the tracks enclosed. This gets read by the package as a single track. To fix this, I split the file using awk.

```awk '/<trk>/{close(file);n++;}{file="track"n".gpx";print >> file;}' untitled.gpx
```

I could then discard track1.gpx which just had the xml header and then use the directory of gpx files.

The elevation problem: this affected only some of the tracks, so in the end the R code needed to be modified. The elevation data is not needed to make the posters so the file process_data.R needs editing, line 28 can be commented out and then line 32 should read:

`result <- data.frame(lat = lat, lon = lon, time = time, type = type) %<%`

This issue is raised on GitHub and has been closed, but the code doesn’t work with elevation blanks. If you run into this problem, this is the way I found to fix it. The other plots in the package which do use elevation will not run, but at least the poster can be made.

I exported the poster as PDF and then made some changes in Illustrator to give the result above.

The post title comes from Multiplex from Oliver’s Standing Stone LP from 1974.

## Frankly, Mr. Shankly

I read about Antonio Sánchez Chinchón’s clever approach to use the Travelling Salesperson algorithm to generate some math-art in R. The follow up was even nicer in my opinion, Pencil Scribbles. The subject was Boris Karloff as the monster in Frankenstein. I was interested in running the code (available here and here), so I thought I’d run it on a famous scientist.

By happy chance one of the most famous scientists of the 20th Century, Rosalind Franklin, shares a nominative prefix with the original subject. There is also a famous portrait of her that I thought would work well.

I first needed needed to clear up the background because it was too dark.

Now to run the TSP code.

The pencil scribbles version is nicer I think.

The R scripts basically ran out-of-the-box. I was using a new computer that didn’t have X11quartz on it nor the packages required, but once that they were installed I just needed to edit the line to use a local file in my working directory. The code just ran. The outputs FrankyTSP and Franky_scribbles didn’t even need to be renamed, given my subject’s name.

Thanks to Antonio for making the code available and so easy to use.

The post title comes from “Frankly, Mr. Shankly” by The Smiths which appears on The Queen is Dead. If the choice of post title needs an explanation, it wasn’t a good choice…

## Paintball’s Coming Home: generating Damien Hirst spot paintings

A few days ago, I read an article about Damien Hirst’s new spot paintings. I’d forgotten how regular the spots were in the original spot paintings from the 1990s (examples are on his page here). It made me think that these paintings could be randomly generated and so I wrote a quick piece of code to do this (HirstGenerator).

I used Hirst’s painting ‘Abalone Acetone Powder’ (1991), which is shown on this page as photographed by Alex Hartley. A wrote some code to sample the colours of this image and then a script to replicate it. The original is shown below  © Damien Hirst and Science Ltd. Click them for full size.

and then this is the replica:

Now that I had a palette of the colours used in the original. It was simple to write a generator to make spot paintings where the spots are randomly assigned.

The generator can make canvasses at whatever size is required.

The code can be repurposed to make spot paintings with different palettes from his other spot paintings or from something else. So there you have it. Generative Hirst Spot Paintings.

For nerds only

My original idea was to generate a palette of unique colours from the original painting. Because of the way I sampled them, each spot is represented once in the palette. This means the same colour as used by the artist is represented as several very similar but nonidentical colours in the palette. My original plan was to find the euclidean distances between all spots in RGB colour space and to establish a distance cutoff to decide what is a unique colour.

That part was easy to write but what value to give for the cutoff was tricky. After some reading, it seems that other colour spaces are better suited for this task, e.g. converting RGB to a CIE colour space. For two reasons, I didn’t pursue this. First, quantixed coding is time-limited. Second. assuming that there is something to the composition of these spot paintings (and they are not a con trick) the frequency of spots must have artistic merit and so they should be left in the palette for sampling in the generated pictures. The representation of the palette in RGB colour space had an interesting pattern (shown in the GIF above).

The post title comes from “Paintball’s Coming Home” by Half Man Half Biscuit from Voyage To The Bottom Of The Road. Spot paintings are kind of paintballs, but mostly because I love the title of this song.