File: busybees.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 (32 lines) | stat: -rw-r--r-- 779 bytes parent folder | download | duplicates (3)
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
####################################################################
# This has reminded me of bees I saw on my way to school in the
# village when I was a little child...
####################################################################

local({
  # number of bees
  n = 50
  # maximum number of frames
  nmax = 3000
  # locations
  x = rnorm(n)
  y = rnorm(n)
  # directions
  rx = sample(c(-1, 1), n, TRUE)
  ry = sample(c(-1, 1), n, TRUE)
  # range of the square
  r = 20
  op = par(pch = 19)
  for (i in 1:nmax) {
    dev.hold()
    rx[x >= r] = -1
    rx[x <= -r] = 1
    ry[y >= r] = -1
    ry[y <= -r] = 1
    x = x + abs(rnorm(n)) * rx
    y = y + abs(rnorm(n)) * ry
    plot(x, y, xlim = c(-r, r), ylim = c(-r, r), col = rainbow(n))
    dev.flush()
  }
  par(op)
})