Brownian tree: Difference between revisions

mNo edit summary
Line 81:
=={{header|D}}==
D V.2, from the C version, writes a Portable Bit Map image to the stdout.
 
A more efficient version generates particles in a disk not too much larger than the current tree.
<lang d>import std.c.stdlib: rand, srand;
import std.c.time: time;
Line 92 ⟶ 94:
static assert(WORLD_WIDTH * WORLD_HEIGHT > NUM_PARTICLES);
 
typedefalias ubyte[WORLD_WIDTH][WORLD_HEIGHT] TWorld;
 
 
void dla(ref TWorld world) {
int px, py; // particle values
int dx, dy; // offsets
 
// put the tree seed
world[WORLD_HEIGHT / 2][WORLD_WIDTH / 2] = 1;
Line 104 ⟶ 102:
foreach (_; 0 .. NUM_PARTICLES) {
// set particle's initial position
int px = rand() % WORLD_WIDTH;
int py = rand() % WORLD_HEIGHT;
 
while (true) { // move particle
// randomly choose a direction
int dx = rand() % 3 - 1; // offsets
int dy = rand() % 3 - 1;
 
if (dx + px < 0 || dx + px >= WORLD_WIDTH ||
Line 122 ⟶ 120:
break;
} else {
// move particle
py += dy;
px += dx;
Line 145 ⟶ 144:
toPBM(world);
}</lang>
One output, WORLD_WIDTH = 12, NUM_PARTICLES = 20:
<pre>P1
12 12
000000000000
000000000000
000000000000
000000010000
000000110000
000000111000
000011111000
000001100000
000001110000
000010100000
000001000000
000000000000</pre>
 
=={{header|Delphi}}==
Anonymous user