[This article was first published on Keep on Fighting! » R Language, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Linlin Yan posted a cool (hot?) simulation of burning fire with R in the COS forum yesterday, which was indeed a warm welcome. I’m not sure whether our forum members will be scared by the “fire” under the title “Welcome to COS Forum”. Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
image()
with carefully designed rows and columns in heated colors heat.colors()
. Here is one of the pictures generated from his code:
And code here:
Fire <- function(row = 100, col = 100, time = 500, fade = 0.03) { fire <- matrix(0, col, row); fire[,1] <- runif(col); for (t in 1:time) { image(fire, col = rev(heat.colors(row)), axes = FALSE, main = "Welcome to COS Forum!"); fire <- ( fire + cbind(fire[,1], fire[c(col,1:(col-1)), 1:(row - 1)]) + cbind(fire[,1], fire[ , 1:(row - 1)]) + cbind(fire[,1], fire[c(2:col,1) , 1:(row - 1)]) ) / 4; fire <- cbind(fire[,1], (fire + fade / 5 - runif(1, max = fade))[,-1]); fire[fire < 0] <- 0; r <- runif(1); if (r < .1) fire[,1] <- fire[,1][c(2:col, 1)]; if (r > .9) fire[,1] <- fire[,1][c(col, 1:(col-1))]; }; NULL; }
The speed of drawing animation frames is rather slow in my computer, but it doesn’t matter since we can use the animation
package (hey, you are advertising!) to save all the image frames and convert them to a single animation file.
library(animation) # set row=50 instead of the default 100 to let the fire burn to the ceiling # make sure you have installed the SWF Tools and the command 'png2swf' can # be executed (with or w/o it installation path); see ?saveSWF saveSWF(Fire(50), interval = 0.05, dev = "png", outdir = getwd(), para = list(mar = c(0, 0, 2, 0)))
< embed style="width: 480px; height: 480px;" type="application/x-shockwave-flash" width="450" src="http://yihui.name/en/wp-content/uploads/2009/06/fire-with-r-by-yan-linlin.swf" wmode="transparent" quality="high">
Now the animation is much more smooth than what we saw in R graphics window.
Thanks, awesome Linlin.
For those who are not familiar with the website “Capital of Statistics” (COS), I’d like to give a brief introduction here: this website was built 3 years ago by me and it was originally a Chinese website for discussion in statistics, but later I thought a place for English-speaking visitors was also necessary, so an English forum was constructed: http://cos.name/en/. Please feel free to join us if you are interested.Related Posts
To leave a comment for the author, please follow the link and comment on their blog: Keep on Fighting! » R Language.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.