Mandelbrot set: Difference between revisions

Content added Content deleted
(→‎{{header|Raku}}: simplifying first version and commenting the second one)
(→‎{{header|Raku}}: clarify $width and $height)
Line 11,402: Line 11,402:
(formerly Perl 6)
(formerly Perl 6)
{{Works with|rakudo|v2023.08-47-g695b9dc46}}
{{Works with|rakudo|v2023.08-47-g695b9dc46}}



Using the [https://raku.land/zef:raku-community-modules/Color color module] to create a palette.
Using the [https://raku.land/zef:raku-community-modules/Color color module] to create a palette.
Line 11,411: Line 11,410:


[[File:mandelbrot-raku-1.2.png|300px|thumb|right]]
[[File:mandelbrot-raku-1.2.png|300px|thumb|right]]
<syntaxhighlight lang="perl6" line>constant MAX_ITERATIONS = 1000;
<syntaxhighlight lang="perl6" line>constant MAX-ITERATIONS = 1000;
my $width = my $height = +(@*ARGS[0] // 800);
my $width = +(@*ARGS[0] // 800);
my $height = $width div 2 + 1;
say "P3";
say "$width $height";
say "255";


sub cut(Range $r, UInt $n where $n > 1 --> Seq) {
sub cut(Range $r, UInt $n where $n > 1 --> Seq) {
Line 11,418: Line 11,421:
}
}


my @re = cut(-2 .. 1/2, $height);
my @re = cut(-2 .. 1/2, $width);
my @im = cut( 0 .. 5/4, $width div 2 + 1) X* 1i;
my @im = cut( 0 .. 5/4, $height) X* 1i;
sub mandelbrot(Complex $z is copy, Complex $c --> Int) {
sub mandelbrot(Complex $z is copy, Complex $c --> Int) {
for 1 .. MAX_ITERATIONS {
for 1 .. MAX-ITERATIONS {
$z = $z*$z + $c;
$z = $z*$z + $c;
return $_ if $z.abs > 2;
return $_ if $z.abs > 2;
Line 11,428: Line 11,431:
return 0;
return 0;
}
}
say "P3";
say "{+@re} {+@im}";
say "255";
hyper for @im.reverse X+ @re {
hyper for @im.reverse X+ @re {
use Color;
use Color;
my $i = (255 * sqrt(mandelbrot(0i, $_) / (MAX_ITERATIONS + 1))).Int;
my $i = (255 * sqrt(mandelbrot(0i, $_) / (MAX-ITERATIONS + 1))).Int;
(state @)[$i] //= Color.new(hsv => $i xx 3).rgb
(state @)[$i] //= Color.new(hsv => $i xx 3).rgb
}.rotor(+@im).map(&put)
}.rotor($width).map(&put);
</syntaxhighlight>
</syntaxhighlight>