Ramer-Douglas-Peucker line simplification: Difference between revisions

m
→‎{{header|Perl}}: 'strict' compliant
m (→‎{{header|Perl}}: 'strict' compliant)
Line 704:
=={{header|Perl}}==
{{trans|Perl 6}}
<lang perl>use List::MoreUtils qw(firstidx minmax)strict;
use warnings;
use feature 'say';
use List::MoreUtils qw(firstidx minmax);
 
my $epsilon = 1;
Line 710 ⟶ 713:
sub norm {
my(@list) = @_;
my( $sum);
$sum += $_**2 for @list;
sqrt($sum)
Line 716 ⟶ 719:
 
sub perpendicular_distance {
local *our(@start = shift ,@_; local *end = shift ,@_; local *point = shift @_);
local(*start,*end,*point) = (shift, shift, shift);
return 0 if $start[0]==$point[0] && $start[1]==$point[1]
or $end[0]==$point[0] && $end[1]==$point[1];
Line 730 ⟶ 734:
my(@points) = @_;
return @points if @points == 2;
my( @d);
push @d, perpendicular_distance(@points[0, -1, $_]) for 0..@points-1;
my(undef,$dmax) = minmax @d;
my( $index) = firstidx { $_ == $dmax } @d;
if ($dmax > $epsilon) {
my @lo = Ramer_Douglas_Peucker( @points[0..$index]);
Line 742 ⟶ 746:
}
 
$result .=say '(' . join(' ', @$_) . ') '
for Ramer_Douglas_Peucker( [0,0],[1,0.1],[2,-0.1],[3,5],[4,6],[5,7],[6,8.1],[7,9],[8,9],[9,9] );</lang>
print "$result\n";</lang>
{{out}}
<pre>(0 0) (2 -0.1) (3 5) (7 9) (9 9)</pre>
(2 -0.1)
(3 5)
(7 9)
(9 9)</pre>
 
=={{header|Perl 6}}==
2,392

edits