Ramer-Douglas-Peucker line simplification: Difference between revisions
Content added Content deleted
No edit summary |
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Add Perl 6 example) |
||
Line 126: | Line 126: | ||
7,9 |
7,9 |
||
9,9</pre> |
9,9</pre> |
||
=={{header|Perl 6}}== |
|||
{{works with|Rakudo|2016.11}} |
|||
{{trans|C++}} |
|||
<lang perl6>sub perpendicular-distance (@start, @end where @end !eqv @start , @point) { |
|||
my $Δx = @end[0] - @start[0]; |
|||
my $Δy = @end[1] - @start[1]; |
|||
my $Δpx = @point[0] - @start[0]; |
|||
my $Δpy = @point[1] - @start[1]; |
|||
($Δx, $Δy) «/=» ($Δx² + $Δy²).sqrt; |
|||
my ($offset-x, $offset-y) = ($Δpx, $Δpy) «-» ($Δx, $Δy) |
|||
«*» ($Δx * $Δpx + $Δy * $Δpy); |
|||
($offset-x² + $offset-y²).sqrt; |
|||
} |
|||
sub Ramer-Douglas-Peucker( @points where * > 1, \ε = 1 ) { |
|||
return @points if @points == 2; |
|||
my ($index, $dmax) = (0,0); |
|||
for (0 ^..^ @points) { |
|||
my $d = perpendicular-distance(@points[0], @points[*-1], @points[$_]); |
|||
if $d > $dmax { |
|||
$dmax = $d; |
|||
$index = $_ |
|||
} |
|||
} |
|||
if $dmax > ε { |
|||
return Ramer-Douglas-Peucker( @points[0..$index] , ε )[0..*-2], |
|||
Ramer-Douglas-Peucker( @points[$index..*-1], ε ) |
|||
} else { |
|||
return @points[0, *-1]; |
|||
} |
|||
} |
|||
# TESTING |
|||
say flat Ramer-Douglas-Peucker( |
|||
[(0.0, 0.0), |
|||
(1.0, 0.1), |
|||
(2.0, -0.1), |
|||
(3.0, 5.0), |
|||
(4.0, 6.0), |
|||
(5.0, 7.0), |
|||
(6.0, 8.1), |
|||
(7.0, 9.0), |
|||
(8.0, 9.0), |
|||
(9.0, 9.0)] |
|||
);</lang> |
|||
{{out}} |
|||
<pre>((0 0) (2 -0.1) (3 5) (7 9) (9 9))</pre> |
|||
=={{header|Python}}== |
=={{header|Python}}== |