Ramer-Douglas-Peucker line simplification: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: syntax coloured)
m (syntax highlighting fixup automation)
Line 22:
{{trans|Go}}
 
<langsyntaxhighlight lang="11l">F rdp(l, ε) -> [(Float, Float)]
V x = 0
V dMax = -1.0
Line 49:
(7.0, 9.0),
(8.0, 9.0),
(9.0, 9.0)], 1.0))</langsyntaxhighlight>
 
{{out}}
Line 57:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <assert.h>
#include <math.h>
#include <stdio.h>
Line 126:
print_points(out, n);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 135:
=={{header|C sharp|C#}}==
{{trans|Java}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 224:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Points remaining after simplification:
Line 235:
=={{header|C++}}==
 
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <cmath>
#include <utility>
Line 343:
 
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 355:
=={{header|D}}==
{{trans|C++}}
<langsyntaxhighlight Dlang="d">import std.algorithm;
import std.exception : enforce;
import std.math;
Line 443:
output ~= pointList[end];
}
}</langsyntaxhighlight>
 
{{out}}
Line 456:
=={{header|FreeBASIC}}==
{{trans|Yabasic}}
<langsyntaxhighlight lang="freebasic">
Function DistanciaPerpendicular(tabla() As Double, i As Integer, ini As Integer, fin As Integer) As Double
Dim As Double dx, dy, mag, pvx, pvy, pvdot, dsx, dsy, ax, ay
Line 518:
Next i
Sleep
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 527:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 559:
fmt.Println(RDP([]point{{0, 0}, {1, 0.1}, {2, -0.1},
{3, 5}, {4, 6}, {5, 7}, {6, 8.1}, {7, 9}, {8, 9}, {9, 9}}, 1))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 567:
=={{header|J}}==
'''Solution:'''
<langsyntaxhighlight lang="j">mp=: +/ .* NB. matrix product
norm=: +/&.:*: NB. vector norm
normalize=: (% norm)^:(0 < norm)
Line 590:
({. ,: {:) points
end.
)</langsyntaxhighlight>
'''Example Usage:'''
<langsyntaxhighlight lang="j"> Points=: 0 0,1 0.1,2 _0.1,3 5,4 6,5 7,6 8.1,7 9,8 9,:9 9
1.0 rdp Points
0 0
Line 598:
3 5
7 9
9 9</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|Kotlin}}
{{works with|Java|9}}
<langsyntaxhighlight Javalang="java">import javafx.util.Pair;
 
import java.util.ArrayList;
Line 697:
pointListOut.forEach(System.out::println);
}
}</langsyntaxhighlight>
{{out}}
<pre>Points remaining after simplification:
Line 708:
=={{header|JavaScript}}==
{{trans|Go}}
<syntaxhighlight lang="javascript">/**
<lang JavaScript>/**
* @typedef {{
* x: (!number),
Line 752:
{x: 9, y: 9}];
 
console.log(RDP(points, 1));</langsyntaxhighlight>
{{out}}
<pre>[{x: 0, y: 0},
Line 764:
{{trans|C++}}
 
<langsyntaxhighlight lang="julia">const Point = Vector{Float64}
 
function perpdist(pt::Point, lnstart::Point, lnend::Point)
Line 805:
plist = Point[[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]]
@show plist
@show rdp(plist)</langsyntaxhighlight>
 
{{out}}
Line 813:
=={{header|Kotlin}}==
{{trans|C++}}
<langsyntaxhighlight lang="scala">// version 1.1.0
 
typealias Point = Pair<Double, Double>
Line 888:
println("Points remaining after simplification:")
for (p in pointListOut) println(p)
}</langsyntaxhighlight>
 
{{out}}
Line 901:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import math
 
type
Line 938:
var line: seq[Point] = @[(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)]
echo rdp(line, line.low, line.high)</langsyntaxhighlight>
 
{{out}}
Line 948:
{{works with|Openscad|2019.05}}
 
<langsyntaxhighlight lang="openscad">function slice(a, v) = [ for (i = v) a[i] ];
 
// Find the distance from the point to the line
Line 1,023:
$fn=16;
points = [[0,0], [1,0.1], [2,-0.1], [3,5], [4,6], [5,7], [6,8.1], [7,9], [8,9], [9,9]];
demo(points);</langsyntaxhighlight>
 
{{out}}
Line 1,030:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,073:
 
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] )</langsyntaxhighlight>
{{out}}
<pre>(0 0)
Line 1,083:
=={{header|Phix}}==
{{trans|Go}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">rdp</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">e</span><span style="color: #0000FF;">)</span>
Line 1,110:
<span style="color: #0000FF;">{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8.1</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">}}</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">rdp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">points</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,118:
=={{header|PHP}}==
{{trans|C++}}
<langsyntaxhighlight lang="php">function perpendicular_distance(array $pt, array $line) {
// Calculate the normalized delta x and y of the line.
$dx = $line[1][0] - $line[0][0];
Line 1,190:
foreach ($result as $point) {
print $point[0] . ',' . $point[1] . "\n";
}</langsyntaxhighlight>
 
{{out}}
Line 1,205:
{{libheader|Shapely}}
An approach using the shapely library:
<langsyntaxhighlight lang="python">from __future__ import print_function
from shapely.geometry import LineString
if __name__=="__main__":
line = LineString([(0,0),(1,0.1),(2,-0.1),(3,5),(4,6),(5,7),(6,8.1),(7,9),(8,9),(9,9)])
print (line.simplify(1.0, preserve_topology=False))</langsyntaxhighlight>
{{out}}
<pre>LINESTRING (0 0, 2 -0.1, 3 5, 7 9, 9 9)</pre>
Line 1,216:
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket
(require math/flonum)
;; points are lists of x y (maybe extensible to z)
Line 1,260:
(module+ test
(require rackunit)
(check-= ((⊥-distance '(0 0) '(0 1)) '(1 0)) 1 epsilon.0))</langsyntaxhighlight>
 
{{out}}
Line 1,270:
{{trans|C++}}
 
<syntaxhighlight lang="raku" perl6line>sub norm (*@list) { @list»².sum.sqrt }
 
sub perpendicular-distance (@start, @end where @end !eqv @start, @point) {
Line 1,294:
say 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)]
);</langsyntaxhighlight>
{{out}}
<pre>((0 0) (2 -0.1) (3 5) (7 9) (9 9))</pre>
Line 1,301:
The computation for the &nbsp; ''perpendicular distance'' &nbsp; was taken from
the &nbsp; '''GO''' &nbsp; example.
<langsyntaxhighlight lang="rexx">/*REXX program uses the Ramer─Douglas─Peucker (RDP) line simplification algorithm for*/
/*───────────────────────────── reducing the number of points used to define its shape. */
parse arg epsilon pts /*obtain optional arguments from the CL*/
Line 1,331:
return subword(r, 1, words(r) - 1) RDP( reb(idx, #) )
end
return @.1 @.#</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,341:
=={{header|Rust}}==
===An implementation of the algorithm===
<langsyntaxhighlight lang="rust">#[derive(Copy, Clone)]
struct Point {
x: f64,
Line 1,409:
println!("{}", p);
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,423:
The geo crate contains an implementation of the Ramer-Douglas-Peucker line
simplification algorithm.
<langsyntaxhighlight lang="rust">// [dependencies]
// geo = "0.14"
 
Line 1,445:
println!("({}, {})", p.x, p.y);
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,458:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">func perpendicular_distance(Arr start, Arr end, Arr point) {
((point == start) || (point == end)) && return 0
var (Δx, Δy ) = ( end »-« start)...
Line 1,485:
say 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]]
)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,492:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">struct Point: CustomStringConvertible {
let x: Double, y: Double
 
Line 1,550:
Point(x: 9.0, y: 9.0)
]
print("\(ramerDouglasPeucker(points: points, epsilon: 1.0))")</langsyntaxhighlight>
 
{{out}}
Line 1,560:
{{trans|Go}}
{{libheader|Wren-dynamic}}
<langsyntaxhighlight lang="ecmascript">import "/dynamic" for Tuple
 
var Point = Tuple.create("Point", ["x", "y"])
Line 1,591:
Point.new(5, 7), Point.new(6, 8.1), Point.new(7, 9), Point.new(8, 9), Point.new(9, 9)
]
System.print(rdp.call(points, 1))</langsyntaxhighlight>
 
{{out}}
Line 1,599:
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">sub perpendicularDistance(tabla(), i, ini, fin)
local dx, cy, mag, pvx, pvy, pvdot, dsx, dsy, ax, ay
 
Line 1,658:
for i = 1 to 10
if matriz(i, 3) print matriz(i, 1), matriz(i, 2)
next</langsyntaxhighlight>
 
=={{header|zkl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="zkl">fcn perpendicularDistance(start,end, point){ // all are tuples: (x,y) -->|d|
dx,dy := end .zipWith('-,start); // deltas
dpx,dpy := point.zipWith('-,start);
Line 1,680:
RamerDouglasPeucker(points[index,*],epsilon)))
} else return(points[0],points[-1]);
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">RamerDouglasPeucker(
T( T(0.0, 0.0), T(1.0, 0.1), T(2.0, -0.1), T(3.0, 5.0), T(4.0, 6.0),
T(5.0, 7.0), T(6.0, 8.1), T(7.0, 9.0), T(8.0, 9.0), T(9.0, 9.0) ))
.println();</langsyntaxhighlight>
{{out}}
<pre>
10,327

edits