Jump to content

Ramer-Douglas-Peucker line simplification: Difference between revisions

(Added Perl example)
Line 779:
{{out}}
<pre>((0 0) (2 -0.1) (3 5) (7 9) (9 9))</pre>
 
=={{header|Phix}}==
{{trans|Go}}
<lang Phix>function rdp(sequence l, atom e)
if length(l)<2 then crash("not enough points to simplify") end if
integer idx := 0
atom dMax := -1,
{p1x,p1y} := l[1],
{p2x,p2y} := l[$],
x21 := p2x - p1x,
y21 := p2y - p1y
for i=1 to length(l) do
atom {px,py} = l[i],
d = abs(y21*px-x21*py + p2x*p1y-p2y*p1x)
if d>dMax then
idx = i
dMax = d
end if
end for
if dMax>e then
return rdp(l[1..idx], e) & rdp(l[idx..$], e)[2..$]
end if
return {l[1], l[$]}
end function
 
sequence points = {{0, 0}, {1, 0.1}, {2, -0.1}, {3, 5}, {4, 6},
{5, 7}, {6, 8.1}, {7, 9}, {8, 9}, {9, 9}}
?rdp(points, 1)</lang>
{{out}}
<pre>
{{0,0},{2,-0.1},{3,5},{7,9},{9,9}}
</pre>
 
=={{header|Python}}==
7,820

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.