Brownian tree: Difference between revisions

m
 
(8 intermediate revisions by 4 users not shown)
Line 1,447:
=={{header|EasyLang}}==
 
[https://easylang.dev/show/#cod=dZHLboMwEEX3/oqzLoqxiahUCfIjiAUlRrVK7IjQFv6+snmkqpKFrfH1eM6dcet7PxxRaLTojaOrajKleAm7uPhvQ67IlRhMO6JkHpboKr3lkKCVqinRwlHyppQSPx+2N1gKnIDBXE0zCoCJkqFxZ+vG+PiAjvr8RP9yo+3pqnmHTQFYU5xihhSwwHR0EBmekmkpG8J5JSd3xJED2Qp+KNuOiQKFH5i3YOJURhdRXOKYDe+DaT5XvtwqPLC9eYQ42smTkgWb6QoO0/oz6EXqqtnfK/n/pWzoYT90WC7+jH6lZPMH7frTMiPBkuLin2/Xt96Y654un7QlhRS/ Run it]
[https://easylang.online/show/#cod=dVLLboMwELz7K0bKrVEdQ5RKlUgO/Y2IA02MahXsyKEN/H3HJhgOdAXWej07M35s8OHdw5rKovNaiw3ExTXO76GQIRONtqjPJXKl8BJG0bpfjYPiJ7y+dFDyEH5Rn7MJgy2YlziSwHJ8V+x7fJlGw6CAFQC8vumqCxmjJ8hX9ura2P7KvnFh+G/hx3amobMhKfZBtURxGjEyDKNmFo08lRwn/cQeJkPysE1aeyrlycJ63dRsKXhMzhPzTHqcjtFPLI75E8/49Lr6TlbkTLSyj9kyI545re+QB9O7ZALhIBeXkKpkdDOlW+E0YV/Lec1S667I3ghcuGZMT0LmJDLUt/FxLCH3Ruvbsk3O6cq+pZDiDw== Run it]
 
<syntaxhighlight lang="text">
color3 0 1 1
len f[] 200 * 200
Line 1,458:
while i < n
repeat
x = randomrandint 200 - 1
y = randomrandint 200 - 1
until f[y * 200 + x + 1] <> 1
.
Line 1,465:
xo = x
yo = y
x += randomrandint 3 - 2
y += randomrandint 3 - 2
if x < 0 or y < 0 or x >= 200 or y >= 200
break 1
Line 4,959:
world[py][px] := 1;
rect(SCALE * pred(px), SCALE * pred(py), SCALE, SCALE, white);
DRAW_FLUSHflushGraphic;
bumped := TRUE;
else
Line 4,967:
until bumped;
end for;
end func;
const proc: main is func
begin
screen(SIZE * SCALE, SIZE * SCALE);
KEYBOARD := GRAPH_KEYBOARD;
genBrownianTree(SIZE, 20000);
readln(KEYBOARD);
end func;</syntaxhighlight>
 
Original source: [http://seed7.sourceforge.net/algorith/graphic.htm#brownian_tree]
 
=={{header|SequenceL}}==
'''SequenceL Code:'''<br>
Line 5,426 ⟶ 5,419:
:Pause
:RecallGDB 0</syntaxhighlight>
 
=={{header|Uiua}}==
Uiua Pad will show well-shaped arrays as images directly. If running locally you can uncomment the final few lines to save it as a file instead. (Running local is ~10 times faster too.)
 
The main move loop passes round a pair of points: here and previous position, so when we hit a set cell we can just back up one.
 
<syntaxhighlight lang="Uiua">
S ← 80
# Create SxS grid, and set the centre point as seed.
⍜⊡(+1)↯2⌊÷2S ↯ S_S 0
 
RandInt ← ⌊×⚂
RandPoint ← ([⍥(RandInt S)2])
# Update the pair to be a new adjacent [[Here] [Last]]
Move ← ⊟∵(-1+⌊RandInt 3).⊢
In ← /××⊃(≥0)(<S) # Is this point in bounds?
# Given a grid return a free point pair and that grid.
SeedPair ← ⊟.⍢(RandPoint ◌)(=1⊡) RandPoint
# Find next adjacent position, or new seed if out of bounds.
Next ← ⟨SeedPair ◌|∘⟩:⟜(In ⊢)Move
# Start from a new Seed Pair and move until you hit the tree. Add the prior pos to the tree.
JoinTree ← ⍜⊡(+1)◌°⊟⍢Next (=0⊡⊢) SeedPair
# Do it multiple times.
⍜now⍥JoinTree500
 
# ◌
# &ime "png"
# &fwa "BrownianTree.png"
</syntaxhighlight>
 
Or if you like your code terse :-)
 
<syntaxhighlight lang="Uiua">
S ← 80
⍜⊡(+1)↯2⌊÷2S↯S_S0
Rp ← (⊟⍥(⌊×⚂S)2)
Sd ← ⊟.⍢(Rp◌)(=1⊡) Rp
Nx ← ⟨Sd◌|∘⟩:⟜(/××⊃(≥0)(<S)⊢)⊟∵(-1+⌊×⚂3).⊢
⍜now⍥(⍜⊡(+1)◌°⊟⍢Nx(=0⊡⊢)Sd)500
</syntaxhighlight>
{{out}}
[[File:UiuaBrownianTree.png|thumb|center||Sample with higher values than provided code]]
 
=={{header|Visual Basic .NET}}==
Windows Forms Application.
Line 5,566 ⟶ 5,602:
{{trans|Go}}
As you'd expect, not very fast so have halved Go's parameters to draw the tree in around 45 seconds.
<syntaxhighlight lang="ecmascriptwren">import "graphics" for Canvas, Color
import "dome" for Window
import "random" for Random
Line 5,657 ⟶ 5,693:
 
var Game = BrownianTree.new(200, 150, 7500)</syntaxhighlight>
 
=={{header|XPL0}}==
[[File:BrownXPL0.gif|right]]
62

edits