Jump to content

Voronoi diagram: Difference between revisions

m
→‎{{header|Perl}}: 'strict' compliant, proper scoping for 'our' variable
No edit summary
m (→‎{{header|Perl}}: 'strict' compliant, proper scoping for 'our' variable)
Line 1,242:
=={{header|Perl}}==
{{trans|Perl 6}}
<lang perl>use Imagerstrict;
use warnings;
use Imager;
 
my %type = (
Line 1,251 ⟶ 1,253:
 
my($xmax, $ymax) = (400, 400);
my @domains;
 
for (1..30) {
push @domains, {
Line 1,265 ⟶ 1,267:
dot(1,@domains);
$img->write(file => "voronoi-$type.png");
}
 
sub voronoi {
my($type, $xmax, $ymax, @d) = @_;
for my $x (0..$xmax) {
for my $y (0..$ymax) {
my $i = 0;
my $d = 10e6;
for (0..$#d) {
my $dd = &{$type{$type}}($d[$_]{'x'}, $d[$_]{'y'}, $x, $y);
if ($dd < $d) { $d = $dd; $i = $_ }
}
$img->setpixel(x => $x, y => $y, color => $d[$i]{rgb} );
}
}
$img->setpixel(x => $x, y => $y, color => $d[$i]{rgb} );
}
}
}
 
sub dot {
my($radius, @d) = @_;
for (0..$#d) {
my $dx = $d[$_]{'x'};
my $dy = $d[$_]{'y'};
for my $x ($dx-$radius .. $dx+$radius) {
for my $y ($dy-$radius .. $dy+$radius) {
$img->setpixel(x => $x, y => $y, color => [0,0,0]);
}
}
}
2,392

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.