Ramer-Douglas-Peucker line simplification: Difference between revisions
Content added Content deleted
m (Fixed spelling error) |
m (→{{header|REXX}}: changed whitespace, simplified some code.) |
||
Line 1,186: | Line 1,186: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
The computation for the ''perpendicular distance'' was taken from |
The computation for the ''perpendicular distance'' was taken from |
||
the '''GO''' example. |
|||
<lang rexx>/*REXX program uses the Ramer─Douglas─Peucker (RDP) line simplification algorithm for*/ |
<lang rexx>/*REXX program uses the Ramer─Douglas─Peucker (RDP) line simplification algorithm for*/ |
||
/*───────────────────────────── reducing the number of points used to define its shape. */ |
/*───────────────────────────── reducing the number of points used to define its shape. */ |
||
Line 1,193: | Line 1,194: | ||
if pts='' then pts= '(0,0) (1,0.1) (2,-0.1) (3,5) (4,6) (5,7) (6,8.1) (7,9) (8,9) (9,9)' |
if pts='' then pts= '(0,0) (1,0.1) (2,-0.1) (3,5) (4,6) (5,7) (6,8.1) (7,9) (8,9) (9,9)' |
||
pts= space(pts) /*elide all superfluous blanks. */ |
pts= space(pts) /*elide all superfluous blanks. */ |
||
say ' error threshold: ' epsilon /*echo the error threshold to the term.*/ |
|||
say ' points specified: ' pts /* " " shape points " " " */ |
|||
$= RDP(pts) /*invoke Ramer─Douglas─Peucker function*/ |
$= RDP(pts) /*invoke Ramer─Douglas─Peucker function*/ |
||
say 'points simplified: ' rez($) /*display points with () ───► terminal.*/ |
|||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
Line 1,202: | Line 1,203: | ||
px: parse arg _; return word( translate(_, , ','), 1) /*obtain the X coörd.*/ |
px: parse arg _; return word( translate(_, , ','), 1) /*obtain the X coörd.*/ |
||
py: parse arg _; return word( translate(_, , ','), 2) /* " " Y " */ |
py: parse arg _; return word( translate(_, , ','), 2) /* " " Y " */ |
||
reb: parse arg a,b,,_; do k=a to b; |
reb: parse arg a,b,,_; do k=a to b; _= _ @.k; end; return strip(_) |
||
rez: parse arg z,_; do k=1 for words(z); _= _ '('word(z, k)") "; end; return strip(_) |
rez: parse arg z,_; do k=1 for words(z); _= _ '('word(z, k)") "; end; return strip(_) |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
RDP: procedure expose epsilon; |
RDP: procedure expose epsilon; call bld space( translate(arg(1), , ')(][}{') ) |
||
L= px(@.#)-px(@.1) |
L= px(@.#) - px(@.1) |
||
H= py(@.#)-py(@.1) |
H= py(@.#) - py(@.1) /* [↓] find point IDX with max distance*/ |
||
do i=2 to #-1 |
do i=2 to #-1 |
||
d= abs(H*px(@.i) - L*py(@.i) + px(@.#)*py(@.1) - py(@.#)*px(@.1) |
d= abs(H*px(@.i) - L*py(@.i) + px(@.#)*py(@.1) - py(@.#)*px(@.1)) |
||
if d>dMax then do; idx= i; dMax= d |
if d>dMax then do; idx= i; dMax= d |
||
end |
end |
||
end /*i*/ |
end /*i*/ /* [↑] D is the perpendicular distance*/ |
||
if dMax>epsilon then do; r= RDP( reb(1, idx) ) |
if dMax>epsilon then do; r= RDP( reb(1, idx) ) |