Skip to content

Instantly share code, notes, and snippets.

@brezniczky
Created May 16, 2016 03:01
Show Gist options
  • Save brezniczky/7e040fad7f7dd3eebb7f7cad00ac12e7 to your computer and use it in GitHub Desktop.
Save brezniczky/7e040fad7f7dd3eebb7f7cad00ac12e7 to your computer and use it in GitHub Desktop.
Sinus plotter visualization (animGIF)
# install.packages("animation")
# A sinus plotter in R just to demo animations.
library(animation)
n.frames = 200
max.t = 60
max.i = 60
all.i = 1:max.i
trail.dt = 3 # length of trail timewise
draw.phase = function(frame.idx, alpha = 1) {
colors = rgb(1 - all.i / max.i, 1 - all.i / max.i, all.i / max.i, alpha)
t = (frame.idx - 1) / (n.frames - 1) * max.t
t.all = t + (0:(max.i - 1)) / (max.i - 1) * trail.dt
x = sin(t.all * 15 / 60 * pi * 2)
y = cos(t.all * 28 / 60 * pi * 2)
points(x, y, col = colors)
}
saveGIF({
dec.frame.index = function(frame.idx, by = 1) {
return((frame.idx - by) %% n.frames)
}
ani.options(nmax = n.frames)
for(frame.idx in 1:n.frames) {
plot(x = c(), xlim = c(-1.5, 1.5), ylim = c(-1.5, 1.5),
axes = FALSE, xlab = "", ylab = "",
main = "Once there was a sinus plotter ...")
draw.phase(dec.frame.index(frame.idx, 2), alpha = 0.25)
draw.phase(dec.frame.index(frame.idx, 1), alpha = 0.5)
draw.phase(frame.idx, alpha = 1)
}},
interval = 0.05, movie.name = "sinus_plotter.gif",
ani.width = 600, ani.height = 400)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment