Zhang-Suen thinning algorithm: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
m (→‎{{header|Perl}}: quiet the warnings about 'uninitialized' variables)
Line 3,574: Line 3,574:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<syntaxhighlight lang="perl">use List::Util qw(sum min);
<syntaxhighlight lang="perl">use v5.36.0;
no warnings 'uninitialized';
use List::Util qw(sum min);


$source = <<'END';
$source = <<'END';
Line 3,612: Line 3,614:


do {
do {
sub seewhite {
sub seewhite ($w1,$w2) {
my($w1,$w2) = @_;
my @results;
sub cycles (@neighbors) { my $c; $c += $neighbors[$_] < $neighbors[($_+1)%8] for 0..$#neighbors; $c }
my(@results);
sub cycles { my(@neighbors)=@_; my $c; $c += $neighbors[$_] < $neighbors[($_+1)%8] for 0..$#neighbors; return $c }
sub blacks (@neighbors) { sum @neighbors }
sub blacks { my(@neighbors)=@_; sum @neighbors }

@prior = @cand; @cand = ();
@prior = @cand; @cand = ();
for $p (@prior) {
for $p (@prior) {
Line 3,627: Line 3,627:
}
}
}
}
return @results;
@results
}
}


@goners1 = seewhite [0,2,4], [2,4,6]; @black[@goners1] = 0 x @goners1;
@goners1 = seewhite [0,2,4], [2,4,6]; @black[@goners1] = 0 x @goners1;
@goners2 = seewhite [0,2,6], [0,4,6]; @black[@goners2] = 0 x @goners2;
@goners2 = seewhite [0,2,6], [0,4,6]; @black[@goners2] = 0 x @goners2;
} until @goners1 == 0 and @goners2 == 0;
} while @goners1 or @goners2;


while (@black) { push @thinned, join '', qw<. #>[splice(@black,0,$h)] }
while (@black) { push @thinned, join '', qw<. #>[splice(@black,0,$h)] }


print join "\n", @thinned;</syntaxhighlight>
say join "\n", @thinned;</syntaxhighlight>
{{out}}
{{out}}
<pre>............................................................
<pre>............................................................