Brownian tree: Difference between revisions

m
→‎{{header|Perl}}: modernized code style a bit
(1-based index)
m (→‎{{header|Perl}}: modernized code style a bit)
Line 2,790:
 
Code runs until the tree reached specified radius. Output is written to "test.eps" of wherever the current directory is.
<syntaxhighlight lang="perl">subuse PI() { atan2(1,1) * 4 } # The, er, pistrict;
use warnings;
sub STEP() { .5 } # How far does the particle move each step. Affects
 
# both speed and accuracy greatly
use constant PI => 2*atan2(1,0); # π
sub STOP_RADIUS() { 100 } # When the tree reaches this far from center, end
use constant STEP => 0.5; # How far particle moves each step. # Affects both speed and accuracy greatly
subuse constant STOP_RADIUS() {=> 100 } ; # When the tree reaches this far from center, end
 
# At each step, move this much towards center. Bigger numbers help the speed because
# particles are less likely to wander off, but greatly affects tree shape.
# Should be between 0 and 1 ish. Set to 0 for pain.
subuse constant ATTRACT() {=> 0.2 };
 
my @particles = map([ map([], 0 .. 2 * STOP_RADIUS) ], 0 .. 2 * STOP_RADIUS);
push @{ $particles[STOP_RADIUS][STOP_RADIUS] }, [0, 0];
my($r_start, $max_dist) = (3, 0);
 
my $r_start = 3;
my $max_dist = 0;
 
sub dist2 {
no warnings 'uninitialized';
my ($dx, $dy) = ($_[0][0] - $_[1][0], $_[0][1] - $_[1][1]);
$dx * $dx + $dy * $dy
Line 2,890 ⟶ 2,891:
 
my $count;
PARTICLE: while (1) {
my $a = rand(2 * PI);
my $p = [ $r_start * cos($a), $r_start * sin($a) ];
while (my $m = move( $p)) {
if ($m == 1) { next }
elsif ($m == 2) { $count++; last; }
elsif ($m == 3) { last PARTICLE }
else { last }
}
print STDERR "$count $max_dist/@{[int($r_start)]}/@{[STOP_RADIUS]}\r" unless $count% 7;
Line 2,904 ⟶ 2,905:
sub write_eps {
my $size = 128;
my $p = $size / (STOP_RADIUS * 1.05);
my $b = STOP_RADIUS * $p;
if ($p < 1) {
$size = STOP_RADIUS * 1.05;
$bp = STOP_RADIUS1;
$pb = 1STOP_RADIUS;
}
 
my $hp = $p / 2;
 
open OUT, "'>"', "'test.eps"';
print OUT <<~"HEAD";
 
# print EPS to standard%!PS-Adobe-3.0 outEPSF-3.0
%%BoundingBox: 0 0 @{[$size*2, $size*2]}
print OUT <<"HEAD";
$size $size translate
%!PS-Adobe-3.0 EPSF-3.0
/l{ rlineto }def
%%BoundingBox: 0 0 @{[$size*2, $size*2]}
/c{ $hp 0 360 arc fill }def
$size $size translate
-$size -$size moveto
/l{ rlineto }def
- $size 2 mul 0 l
/c{ $hp 0 360 arc fill }def
-$size - 0 $size moveto2 mul l
-$size 2 mul 0 l
closepath
0 $size 2 mul l
0 setgray fill
-$size 2 mul 0 l
0 setlinewidth .1 setgray 0 0 $b 0 360 arc stroke
closepath
.8 setgray /TimesRoman findfont 16 scalefont setfont
0 setgray fill
-$size 10 add $size -16 add moveto
0 setlinewidth .1 setgray 0 0 $b 0 360 arc stroke
(Step = @{[STEP]} Attract = @{[ATTRACT]}) show
.8 setgray /TimesRoman findfont 16 scalefont setfont
0 1 0 setrgbcolor newpath
-$size 10 add $size -16 add moveto
HEAD
(Step = @{[STEP]} Attract = @{[ATTRACT]}) show
0 1 0 setrgbcolor newpath
HEAD
 
for (@particles) {
Line 2,945 ⟶ 2,943:
}
 
write_eps();</syntaxhighlight>
 
=={{header|Phix}}==
As-is, runs in about 2s, but can be very slow when bigger or (even worse) resize-able.
2,392

edits