Boids: Difference between revisions
(Racket version) |
No edit summary |
||
(19 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
{{draft task}} |
{{draft task}} |
||
This task requires the creation of a graphical or purely textual |
A [[wp:Boids|Boids]] algorithm simulates the flocking behavior of birds. This task requires the creation of a graphical or purely textual simulation of a flock of birds navigating the cave with obstacles depicted below. |
||
<div style="font-size: .8em;"><pre> |
|||
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). |
||
⚫ | |||
.......###############............................................................... |
.......###############............................................................... |
||
......#################.............................................................. |
......#################.............................................................. |
||
Line 22: | Line 18: | ||
............................................................#################........ |
............................................................#################........ |
||
...........................................................###################....... |
...........................................................###################....... |
||
............................................................#################........ |
............................................................#################........ |
||
</pre></div> |
|||
⚫ | 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). |
||
⚫ | |||
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> |
|||
⚫ | |||
<br><br> |
|||
;See also: |
|||
⚫ | |||
* http://natureofcode.com/book/chapter-6-autonomous-agents/ |
|||
<br><br> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
See [[Boids |
See [[Boids/C]] |
||
⚫ | |||
See [[Boids/Go]] |
|||
⚫ | |||
=={{header|Java}}== |
|||
Here is the result: hashcollision.org/whalesong/examples/boid/boid.html |
|||
See [[Boids/Java]] |
|||
The Whalesong compiler was used to compile the Racket source into JavaScript. |
|||
=={{header|Julia}}== |
|||
See [[Boids/Julia]] |
|||
The Racket source is here: hashcollision.org/whalesong/examples/boid/boid.rkt |
|||
=={{header|Nim}}== |
|||
See [[Boids/Nim]] |
|||
=={{header|Phix}}== |
|||
See [[Boids/Phix]]<br> |
|||
Screenshot: [http://phix.x10.mx/shots/boids.png http://phix.x10.mx/shots/boids.png] |
|||
=={{header|Wren}}== |
|||
See [[Boids/Wren]] |
|||
http://hashcollision.org/whalesong/examples/boid/boid.html |
Latest revision as of 11:33, 28 July 2023
A Boids algorithm simulates the flocking behavior of birds. This task requires the creation of a graphical or purely textual simulation of a flock of birds navigating the cave with obstacles depicted below.
.......###############............................................................... .......###############............................................................... ......#################.............................................................. O......###############............................................................... OO.....###############............................................................... OO.....###############....................#.........................#................ OO......#############...................#####...................#########............ OO......#############...................#####..................###########........... OO.......###########...................#######................#############.......... OO.........#######......................#####................###############......... OO............#.........................#####...............#################........ OO........................................#.................#################........ O...........................................................#################........ ............................................................#################........ ...........................................................###################....... ............................................................#################........
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).
A simulation that doesn't contain obstacles but only shows flocking behavior is acceptable.
- See also
C
See Boids/C
Go
See Boids/Go
Java
See Boids/Java
Julia
See Boids/Julia
Nim
See Boids/Nim
Phix
See Boids/Phix
Screenshot: http://phix.x10.mx/shots/boids.png
Wren
See Boids/Wren