Ramer-Douglas-Peucker line simplification: Difference between revisions

(Go solution)
Line 266:
</pre>
 
=={{header|J}}==
'''Solution:'''
<lang j>mp=: +/ .* NB. matrix product
norm=: +/&.:*: NB. vector norm
normalize=: (% norm)^:(0 < norm)
 
dxy=. normalize@({: - {.)
pv=. -"1 {.
pvdot=. dxy mp"1 pv
NB.*perpDist v Calculate perpendicular distance of points from a line
perpDist=: norm"1@(pv -"1 pvdot */ dxy) f.
 
rdp=: verb define
1 rdp y
:
points=. y
if. 2 > # points do. points return. end.
 
NB. index and maximum distance from line between start and end
'dmax imax'=. (>./ , ] i. >./) perpDist points
if. dmax > x do.
res1=. x }:@rdp (1+imax) {. points
res2=. x rdp imax }. points
out=. res1 , res2
else.
out=. ({. ,: {:) points
end.
out
)</lang>
'''Example Usage:'''
<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
2 _0.1
3 5
7 9
9 9</lang>
=={{header|Java}}==
{{trans|Kotlin}}
892

edits