Boids

From Rosetta Code
Revision as of 12:25, 25 January 2014 by rosettacode>Dkf (→‎{{header|Racket}}: Make the links live an integrated)
Boids is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

This task requires the creation of a graphical or purely textual basic simulation of Boids.

See:

If you implement a purely textual simulation, this is a possible board representation, where "O" are the boids that should go toward the right, "#" are fixed walls that should be avoided by the boids, and "." is free space (using a space is also acceptable for free space, if you add some kind of frame around the board).

.......###############...............................................................
.......###############...............................................................
......#################..............................................................
O......###############...............................................................
OO.....###############...............................................................
OO.....###############....................#.........................#................
OO......#############...................#####...................#########............
OO......#############...................#####..................###########...........
OO.......###########...................#######................#############..........
OO.........#######......................#####................###############.........
OO............#.........................#####...............#################........
OO........................................#.................#################........
O...........................................................#################........
............................................................#################........
...........................................................###################.......
............................................................#################........

An even simpler simulation that doesn't contain walls is acceptable.

The board was generated by this simple Python code: <lang python>from array import array from math import hypot

nx = 85 ny = 16 background = '.' foreground = '#'

mat = [array("c", background) * nx for _ in xrange(ny)]

def add_circle(mat, (cx, cy, r)):

   for y in xrange(cy - r, cy + r + 1):
       for x in xrange(cx - r, cx + r + 1):
           if hypot(cx - x, cy - y) <= r:
               if x >= 0 and x < len(mat[0]) and y >= 0 and y < len(mat):
                   mat[y][x] = foreground

for c in [(14, 2, 8), (nx / 2, ny / 2, 3), (nx - 17, ny - 2, 9)]:

   add_circle(mat, c)

for row in mat:

   print row.tostring()</lang>

C

See Boids simulation/C

Racket

Here is the result. The Whalesong compiler was used to compile the Racket source into JavaScript.