Voronoi diagram: Difference between revisions

Content added Content deleted
No edit summary
m (→‎{{header|Perl 6}}: added .race for concurrency, code tweaks)
Line 1,241: Line 1,241:


=={{header|Perl 6}}==
=={{header|Perl 6}}==
{{works with|Rakudo|2017.09}}
{{works with|Rakudo|2018.09}}
{{trans|Python}}
{{trans|Python}}
Perhaps "Inspired by Python" would be more accurate.
Perhaps "Inspired by Python" would be more accurate.
Line 1,249: Line 1,249:
<lang perl6>use Image::PNG::Portable;
<lang perl6>use Image::PNG::Portable;


my @bars = '▁▂▃▄▅▆▇█▇▆▅▄▃▂▁'.comb;
my @bars = '▁▂▃▅▆▇▇▆▅▃▂▁'.comb;


my %type = ( # Voronoi diagram type distance calculation
my %type = ( # Voronoi diagram type distance calculation
Line 1,276: Line 1,276:
sub voronoi (@domains, :$w, :$h, :$type) {
sub voronoi (@domains, :$w, :$h, :$type) {
my $png = Image::PNG::Portable.new: :width($w), :height($h);
my $png = Image::PNG::Portable.new: :width($w), :height($h);
for ^$w -> $x {
(^$w).race.map: -> $x {
print "\b" x 2+@bars, @bars.=rotate(1).join , ' ';
print "\b" x 2+@bars, @bars.=rotate(1).join , ' ';
for ^$h -> $y {
for ^$h -> $y {
Line 1,287: Line 1,287:


sub dot (%h, $png, $radius = 3) {
sub dot (%h, $png, $radius = 3) {
for %h<x> - $radius .. %h<x> + $radius -> $x {
for (%h<x> X+ -$radius .. $radius) X (%h<y> X+ -$radius .. $radius) -> ($x, $y) {
for %h<y> - $radius .. %h<y> + $radius -> $y {
$png.set($x, $y, 0, 0, 0) if ( %h<x> - $x + (%h<y> - $y) * i ).abs <= $radius;
$png.set($x, $y, 0, 0, 0) if ( %h<x> - $x + (%h<y> - $y) * i ).abs <= $radius;
}
}
}
}
}