[R] Animated lissajous

rlist.10.phftt@xoxy.net rlist.10.phftt at xoxy.net
Sun Oct 16 05:34:29 CEST 2005


Here's some code to make lissajous dance.  I've attached a small sample 
GIF.

Cheers,
Rob Steele
robsteele at yahoo dot com



plot.lissajous = function(omega.x, omega.y, delta = 0, num.thetas = 200)
{
   thetas = seq(0, 2 * pi, length = num.thetas)
   xs = sin(omega.x * thetas + delta)
   ys = cos(omega.y * thetas)
   plot(xs, ys, type = 'l', lwd = 3, ann = FALSE, axes = FALSE)
}


## Show one.
par(mar = c(2, 2, 2, 2))
plot.lissajous(4, 3)

## Animate it.
while (TRUE) {
   for (delta in seq(0, 2 * pi, length = 120)) {
       plot.lissajous(4, 3, delta)
       Sys.sleep(1 / 30)
   }
}


## Show a bunch.
par(mar = c(1, 1, 1, 1))
par(mfrow = c(length(omega.xs), length(omega.ys)))
for (omega.x in 1:5) {
   for (omega.y in 1:5) {
       plot.lissajous(omega.x, omega.y, deltas[i])
   }
}

## Animate them.  (Requires ImageMagick.)
num.frames = 120
image.dir = 'images'

if (! file.exists(image.dir)) {
   dir.create(image.dir)
}

deltas = seq(0, 2 * pi, length = num.frames)

for (i in 1 : length(deltas)) {
   png(file = file.path(image.dir, sprintf('img-%03d.png', i)))
   par(mar = c(1, 1, 1, 1))
   par(mfrow = c(length(omega.xs), length(omega.ys)))
   for (omega.x in 1:5) {
       for (omega.y in 1:5) {
           plot.lissajous(omega.x, omega.y, deltas[i])
       }
   }
   dev.off()
}

## This ImageMagick command combines the image files into a GIF animation:
# convert -delay 3 images/*.png images/animation.gif


More information about the R-help mailing list