Brownian tree: Difference between revisions
m
→{{header|Perl}}: modernized code style a bit
(1-based index) |
SqrtNegInf (talk | contribs) 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">
use warnings;
# 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.
# 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.
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 $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 (
my $a = rand(2 * PI);
my $p = [ $r_start * cos($a), $r_start * sin($a) ];
while (my $m = move
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;
$
$
}
my $hp = $p / 2;
open OUT,
print OUT <<~"HEAD";▼
%%BoundingBox: 0 0 @{[$size*2, $size*2]}▼
▲ print OUT <<"HEAD";
$size $size translate▼
/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
▲/c{ $hp 0 360 arc fill }def
-$size 2 mul 0 l
closepath▼
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
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.
|