Skip to content

Instantly share code, notes, and snippets.

@kohske
Created February 23, 2016 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kohske/4880157dd96a7bd987ea to your computer and use it in GitHub Desktop.
Save kohske/4880157dd96a7bd987ea to your computer and use it in GitHub Desktop.
library(animation)
library(grid)
graphics.off()
N = 500
x = seq(0, 1, length.out = N)
xd = 30
NY = 36
ys = (0:NY/NY)[rep(c(F,T,T,F), length.out=NY)]
di = rep(c(1,1,-1,-1), length.out = NY)
f = function(xo) {
grid.rect(gp = gpar(fill = "gray50"))
for (i in 1:length(ys)) {
y = (cos(xd*(x)*pi)+1)/100*0.25 + ys[i]
id = (sin(xd/2*(x+di[i]*xo)*pi))>0
id2 = cumsum(abs(c(0, diff(id))))
id2[id<=0] = NA
grid.lines(x, y, gp = gpar(col = "white", lwd = 3))
grid.polyline(x, y, id = id2, gp = gpar(col = "black", lwd = 3))
}
}
saveGIF({
ani.options(nmax = 50)
for (xo in seq(0, 1/xd*4, length.out = 50)) {
grid.newpage()
f(xo)
}}, interval = 0.05, movie.name = "bm_demo.gif", ani.width = 600, ani.height = 600)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment