Brownian tree: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 370:
}</lang>
Run-time about 12.4 seconds with SIDE=600, NUM_PARTICLES=10000.
 
=={{header|C sharp|C#}}==
{{works with|C#|3.0}}
{{libheader|System.Drawing}}
<lang csharp>using System;
using System.Drawing;
 
namespace BrownianTree
{
class Program
{
static Bitmap BrownianTree(int size, int numparticles)
{
Bitmap bmp = new Bitmap(size, size);
Rectangle bounds = new Rectangle { X = 0, Y = 0, Size = bmp.Size };
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.Black);
}
Random rnd = new Random();
bmp.SetPixel(rnd.Next(size), rnd.Next(size), Color.White);
Point pt = new Point(), newpt = new Point();
for (int i = 0; i < numparticles; i++)
{
pt.X = rnd.Next(size);
pt.Y = rnd.Next(size);
do
{
newpt.X = pt.X + rnd.Next(-1, 2);
newpt.Y = pt.Y + rnd.Next(-1, 2);
if (!bounds.Contains(newpt))
{
pt.X = rnd.Next(size);
pt.Y = rnd.Next(size);
}
else if (bmp.GetPixel(newpt.X, newpt.Y).R > 0)
{
bmp.SetPixel(pt.X, pt.Y, Color.White);
break;
}
else
{
pt = newpt;
}
} while (true);
}
return bmp;
}
 
static void Main(string[] args)
{
BrownianTree(300, 3000).Save("browniantree.png");
}
}
}</lang>
 
=={{header|C++}}==
Line 671 ⟶ 726:
//--------------------------------------------------------------------</lang>
 
=={{header|C sharp|C#}}==
{{works with|C#|3.0}}
{{libheader|System.Drawing}}
<lang csharp>using System;
using System.Drawing;
 
namespace BrownianTree
{
class Program
{
static Bitmap BrownianTree(int size, int numparticles)
{
Bitmap bmp = new Bitmap(size, size);
Rectangle bounds = new Rectangle { X = 0, Y = 0, Size = bmp.Size };
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.Black);
}
Random rnd = new Random();
bmp.SetPixel(rnd.Next(size), rnd.Next(size), Color.White);
Point pt = new Point(), newpt = new Point();
for (int i = 0; i < numparticles; i++)
{
pt.X = rnd.Next(size);
pt.Y = rnd.Next(size);
do
{
newpt.X = pt.X + rnd.Next(-1, 2);
newpt.Y = pt.Y + rnd.Next(-1, 2);
if (!bounds.Contains(newpt))
{
pt.X = rnd.Next(size);
pt.Y = rnd.Next(size);
}
else if (bmp.GetPixel(newpt.X, newpt.Y).R > 0)
{
bmp.SetPixel(pt.X, pt.Y, Color.White);
break;
}
else
{
pt = newpt;
}
} while (true);
}
return bmp;
}
 
static void Main(string[] args)
{
BrownianTree(300, 3000).Save("browniantree.png");
}
}
}</lang>
=={{header|Common Lisp}}==
When the random walk lands on a set pixel it sets the pixel at the previous position.
Line 1,229 ⟶ 1,230:
 
end program</lang>
 
=={{header|FreeBASIC}}==
<lang freebasic>' version 16-03-2017
Line 2,008 ⟶ 2,010:
</body>
</html></lang>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
Line 2,206 ⟶ 2,209:
close #1
end</lang>
 
=={{header|Lua}}==
The output is stored in as a ppm-image. The source code of these output-functions is located at
Line 2,812 ⟶ 2,816:
 
write_eps;</lang>
 
=={{header|Perl 6}}==
 
[[File:Brownian_tree_perl6.png|thumb]]
 
This solution spawns new Particles at a growing square border and displays the Tree every 50 particles and at the end using unicode UPPER/LOWER HALF BLOCK and FULL BLOCK.
 
{{works with|Rakudo|2015.12}}
 
<lang perl6>constant size = 100;
constant particlenum = 1_000;
 
 
constant mid = size div 2;
 
my $spawnradius = 5;
my @map;
 
sub set($x, $y) {
@map[$x][$y] = True;
}
 
sub get($x, $y) {
return @map[$x][$y] || False;
}
 
set(mid, mid);
my @blocks = " ","\c[UPPER HALF BLOCK]", "\c[LOWER HALF BLOCK]","\c[FULL BLOCK]";
 
sub infix:<█>($a, $b) {
@blocks[$a + 2 * $b]
}
 
sub display {
my $start = 0;
my $end = size;
say (for $start, $start + 2 ... $end -> $y {
(for $start..$end -> $x {
if abs(($x&$y) - mid) < $spawnradius {
get($x, $y) █ get($x, $y+1);
} else {
" "
}
}).join
}).join("\n")
}
 
for ^particlenum -> $progress {
my Int $x;
my Int $y;
my &reset = {
repeat {
($x, $y) = (mid - $spawnradius..mid + $spawnradius).pick, (mid - $spawnradius, mid + $spawnradius).pick;
($x, $y) = ($y, $x) if (True, False).pick();
} while get($x,$y);
}
reset;
 
while not get($x-1|$x|$x+1, $y-1|$y|$y+1) {
$x = ($x-1, $x, $x+1).pick;
$y = ($y-1, $y, $y+1).pick;
if (False xx 3, True).pick {
$x = $x >= mid ?? $x - 1 !! $x + 1;
$y = $y >= mid ?? $y - 1 !! $y + 1;
}
if abs(($x | $y) - mid) > $spawnradius {
reset;
}
}
set($x,$y);
if $spawnradius < mid && abs(($x|$y) - mid) > $spawnradius - 5 {
$spawnradius = $spawnradius + 1;
}
}
 
display;</lang>
 
=={{header|Phix}}==
Line 3,069 ⟶ 2,997:
Until Event = #PB_Event_CloseWindow
EndIf</lang>[[File:BrownianTree.pb.png]]
 
 
=={{header|Python}}==
Line 3,585 ⟶ 3,512:
img
(save-image img "brownian-tree.png")</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
 
[[File:Brownian_tree_perl6.png|thumb]]
 
This solution spawns new Particles at a growing square border and displays the Tree every 50 particles and at the end using unicode UPPER/LOWER HALF BLOCK and FULL BLOCK.
 
{{works with|Rakudo|2015.12}}
 
<lang perl6>constant size = 100;
constant particlenum = 1_000;
 
 
constant mid = size div 2;
 
my $spawnradius = 5;
my @map;
 
sub set($x, $y) {
@map[$x][$y] = True;
}
 
sub get($x, $y) {
return @map[$x][$y] || False;
}
 
set(mid, mid);
my @blocks = " ","\c[UPPER HALF BLOCK]", "\c[LOWER HALF BLOCK]","\c[FULL BLOCK]";
 
sub infix:<█>($a, $b) {
@blocks[$a + 2 * $b]
}
 
sub display {
my $start = 0;
my $end = size;
say (for $start, $start + 2 ... $end -> $y {
(for $start..$end -> $x {
if abs(($x&$y) - mid) < $spawnradius {
get($x, $y) █ get($x, $y+1);
} else {
" "
}
}).join
}).join("\n")
}
 
for ^particlenum -> $progress {
my Int $x;
my Int $y;
my &reset = {
repeat {
($x, $y) = (mid - $spawnradius..mid + $spawnradius).pick, (mid - $spawnradius, mid + $spawnradius).pick;
($x, $y) = ($y, $x) if (True, False).pick();
} while get($x,$y);
}
reset;
 
while not get($x-1|$x|$x+1, $y-1|$y|$y+1) {
$x = ($x-1, $x, $x+1).pick;
$y = ($y-1, $y, $y+1).pick;
if (False xx 3, True).pick {
$x = $x >= mid ?? $x - 1 !! $x + 1;
$y = $y >= mid ?? $y - 1 !! $y + 1;
}
if abs(($x | $y) - mid) > $spawnradius {
reset;
}
}
set($x,$y);
if $spawnradius < mid && abs(($x|$y) - mid) > $spawnradius - 5 {
$spawnradius = $spawnradius + 1;
}
}
 
display;</lang>
 
=={{header|REXX}}==
10,327

edits