File: fire.R

package info (click to toggle)
r-cran-animation 2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,268 kB
  • sloc: javascript: 873; sh: 15; makefile: 2
file content (33 lines) | stat: -rw-r--r-- 1,051 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
## Original code by Linlin Yan
## See http://yihui.org/en/2009/06/simulation-of-burning-fire-in-r/ for the
##    introduction of this demo
## If your computer is too fast, just add Sys.sleep(0.01) somewhere in the loop
local({
  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) {
      dev.hold()
      image(fire, col = rev(heat.colors(row)),
            axes = FALSE, ...);
      dev.flush()
      
      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;
  }
  par(mar = rep(0, 4))
  Fire(50)
})