[R] Animated lissajous
Tuszynski, Jaroslaw W.
JAROSLAW.W.TUSZYNSKI at saic.com
Mon Oct 17 21:07:57 CEST 2005
Pretty neat.
Jarek
====================================================\====
Jarek Tuszynski, PhD. o / \
Science Applications International Corporation <\__,|
(703) 676-4192 "> \
Jaroslaw.W.Tuszynski at saic.com ` \
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of
rlist.10.phftt at xoxy.net
Sent: Saturday, October 15, 2005 11:34 PM
To: r-help at stat.math.ethz.ch
Subject: [R] Animated lissajous
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