Ramer-Douglas-Peucker line simplification: Difference between revisions

m
→‎{{header|Perl 6}}: Style tweaks, code simplification
m (→‎{{header|Perl 6}}: factor out some common code)
m (→‎{{header|Perl 6}}: Style tweaks, code simplification)
Line 222:
<lang perl6>sub norm (*@list) { @list»².sum.sqrt }
 
sub perpendicular-distance (@start, @end where @end !eqv @start , @point) {
return 0 if @point eqv any(@start, @end);
my ( $Δx, $Δy ) = @end «-» @start;
Line 230:
}
 
sub Ramer-Douglas-Peucker( @points where * > 1, \ε = 1 ) {
return @points if @points == 2;
my @d = (^@points).map: { perpendicular-distance |@points[0, *-1, $_] };
{ perpendicular-distance(@points[0], @points[*-1], @points[$_]) };
my ($index, $dmax) = @d.first: @d.max, :kv;
return flat
 
return Ramer-Douglas-Peucker( @points[0..$index] , ε )[^(*-1)],
Ramer-Douglas-Peucker( @points[$index..*-1], ε )
if $dmax > ε;
@points[0, *-1];
}
 
# TESTING
say flat 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>
10,333

edits