[R] Animated lissajous

From: <rlist.10.phftt_at_xoxy.net>
Date: Sun 16 Oct 2005 - 13:48:46 EST

Oh my goodness how did that bug creep in there. Ignore that last post and try this instead.

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.

num.frames = 120
omega.xs = 1:5
omega.ys = 1:5
deltas = seq(0, 2 * pi, length = num.frames)

par(mar = c(1, 1, 1, 1))
par(mfrow = c(5, 5))
for (omega.x in omega.xs) {

for (omega.y in omega.ys) {

plot.lissajous(omega.x, omega.y)
}
}

## Animate them.

image.dir = 'images'

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

dir.create(image.dir)
}

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 displays the image files in rapid succession
## to animate them:

# animate -delay 2 images/*.png

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

R-help@stat.math.ethz.ch mailing list