RosettaCodeData/Task/Brownian-tree/Seed7/brownian-tree.seed7

46 lines
1.4 KiB
Plaintext

$ include "seed7_05.s7i";
include "draw.s7i";
include "keybd.s7i";
const integer: SIZE is 300;
const integer: SCALE is 1;
const proc: genBrownianTree (in integer: fieldSize, in integer: numParticles) is func
local
var array array integer: world is 0 times 0 times 0;
var integer: px is 0;
var integer: py is 0;
var integer: dx is 0;
var integer: dy is 0;
var integer: i is 0;
var boolean: bumped is FALSE;
begin
world := fieldSize times fieldSize times 0;
world[rand(1, fieldSize)][rand(1, fieldSize)] := 1; # Set the seed
for i range 1 to numParticles do
# Set particle's initial position
px := rand(1, fieldSize);
py := rand(1, fieldSize);
bumped := FALSE;
repeat
# Randomly choose a direction
dx := rand(-1, 1);
dy := rand(-1, 1);
if dx + px < 1 or dx + px > fieldSize or dy + py < 1 or dy + py > fieldSize then
# Plop the particle into some other random location
px := rand(1, fieldSize);
py := rand(1, fieldSize);
elsif world[py + dy][px + dx] <> 0 then
# Bumped into something
world[py][px] := 1;
rect(SCALE * pred(px), SCALE * pred(py), SCALE, SCALE, white);
flushGraphic;
bumped := TRUE;
else
py +:= dy;
px +:= dx;
end if;
until bumped;
end for;
end func;