Ramer-Douglas-Peucker line simplification: Difference between revisions

m
→‎{{header|Perl 6}}: factor out some common code
m (→‎{{header|Perl 6}}: minor simplification)
m (→‎{{header|Perl 6}}: factor out some common code)
Line 217:
 
=={{header|Perl 6}}==
{{works with|Rakudo|20162017.1105}}
{{trans|C++}}
 
<lang perl6>sub perpendicular-distancenorm (*@start,list) @end where{ @endlist»².sum.sqrt !eqv @start , @point) {}
 
sub perpendicular-distance (@start, @end where @end !eqv @start , @point) {
return 0 if @point eqv any(@start, @end);
my ( $Δx, $Δy ) = @end «-» @start;
my ($Δpx, $Δpy) = @point «-» @start;
($Δx, $Δy) «/=» (norm $Δx² +, $Δy²).sqrt;
((norm ($Δpx, $Δpy) «-» ($Δx, $Δy) «*» ($Δx*$Δpx + $Δy*$Δpy)) «**» 2).sum.sqrt;
}
 
Line 234 ⟶ 236:
my ($index, $dmax) = @d.first: @d.max, :kv;
 
return Ramer-Douglas-Peucker( @points[0..$index] , ε )[^(*-1)],
if $dmax > ε {
return Ramer-Douglas-Peucker( @points[0$index..$index*-1] , ε )[^(*-1)],
if $dmax Ramer-Douglas-Peucker( @points[$index..*-1],> ε );
}
@points[0, *-1];
}
10,333

edits