This morning at R-Bloggers I saw Markus’ post, providing R code to produce one of Bridget Riley’s Op Art pictures. Very nice! I played around with it, making it a little more general and adding colour, see attached image for a nearly equiluminant version (the luminance of red and blue are nearly the same), making it even more dynamic. See this paper from Johannes Zanker’s lab on eye movements and their role in Op Art pictures. R code below:

Moving SquaRes

Michael Bach 2012-07-12


Inspired by

in turn inspired by Bridget Riley’s Moving Squares

distortionFunc <- function(x) {  # this is a rounded fit to Markus’ data

return (845x - 46xx +0.88xxx)


odd <- function(arg) {

return (arg/2 != round(arg/2))


pntsX = 32;  pntsY = 12

x = distortionFunc(1:pntsX)

plot(y=x, x=1:pntsX)    # for a look at the distortion function

y = 1:pntsY

z = matrix(nrow=pntsX, ncol=pntsY)

for (ix in 1:pntsX) { # I’m sure this could be more elegant in R!

for (iy in 1:pntsY) {

 z[ix, iy] = ifelse(xor(odd(ix), odd(iy)), 0, 1)



cols = c(rgb(0, 0.8, 0), rgb(1, 0, 0)) # nearly equiluminant red&green

image(x[-pntsX], y[-pntsY], z[-pntsX,-pntsY], col=cols, axes=FALSE, xlab=””, ylab=””)