Ramer-Douglas-Peucker line simplification: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: 'strict' compliant) |
|||
Line 518: | Line 518: | ||
(7.000000, 9.000000) |
(7.000000, 9.000000) |
||
(9.000000, 9.000000)</pre> |
(9.000000, 9.000000)</pre> |
||
=={{header|JavaScript}}== |
|||
{{trans|Go}} |
|||
<lang JavaScript>/** |
|||
* @typedef {{ |
|||
* x: (!number), |
|||
* y: (!number) |
|||
* }} |
|||
*/ |
|||
let pointType; |
|||
/** |
|||
* @param {!Array<pointType>} l |
|||
* @param {number} eps |
|||
*/ |
|||
const RDP = (l, eps) => { |
|||
let x = 0; |
|||
let dMax = -1; |
|||
const last = l.length - 1; |
|||
const p1 = l[0]; |
|||
const p2 = l[last]; |
|||
const x21 = p2.x - p1.x; |
|||
const y21 = p2.y - p1.y; |
|||
l.slice(1,last).forEach((p, i) => { |
|||
const d = Math.abs(y21*p.x - x21*p.y + p2.x*p1.y - p2.y*p1.x); |
|||
if (d > dMax) { |
|||
x = i + 1; |
|||
dMax = d; |
|||
} |
|||
}); |
|||
if (dMax > eps) { |
|||
return [...RDP(l.slice(0, x+1), eps), ...RDP(l.slice(x), eps).slice(1)]; |
|||
} |
|||
return [l[0], l[last]] |
|||
}; |
|||
const points = [ |
|||
{x:0, y:0}, {x:1, y:0.1}, {x:2, y:-0.1}, {x:3, y:5}, {x:4, y:6}, {x:5, y:7}, |
|||
{x:6, y:8.1}, {x:7, y:9}, {x:8, y:9}, {x:9, y:9}]; |
|||
console.log(RDP(points, 1));</lang> |
|||
{{out}} |
|||
<pre>[ { x: 0, y: 0 }, |
|||
{ x: 2, y: -0.1 }, |
|||
{ x: 3, y: 5 }, |
|||
{ x: 7, y: 9 }, |
|||
{ x: 9, y: 9 } ]</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |