Commit 22d30135 authored by Starbeamrainbowlabs's avatar Starbeamrainbowlabs
Browse files

Bugfix the new algorithm

parent 04b599af
......@@ -2,7 +2,7 @@
<html>
<head>
<meta charset='utf-8' />
<title>Ramer Douglas Peucker Demo</title>
<title>Line Simplification Demo</title>
</head>
<body>
<canvas id="canvas-main"></canvas>
......@@ -10,7 +10,7 @@
<div class="debug-data">
<ul>
<li>Points: <output id="output-point-count"></output> &#10142; <output id="output-simple-point-count"></output></li>
<li>Epsilon: <input type="range" id="input-epsilon" value="2" min="0" max="100" step="0.05" /> <output id="output-epsilon"></output>
<li>Min Area: <input type="range" id="input-epsilon" value="2" min="0" max="100" step="0.05" /> <output id="output-epsilon"></output>
</ul>
</div>
......
......@@ -110,10 +110,10 @@ class RDPDemo
// Clear the screen
canvas.width = canvas.width;
if(this.drawSimpleLine)
this.draw_line(canvas, context, this.simplePoints, this.simpleLine)
if(this.drawRawLine)
this.draw_line(canvas, context, this.points, this.rawLine);
if(this.drawSimpleLine)
this.draw_line(canvas, context, this.simplePoints, this.simpleLine)
}
draw_line(canvas, context, points, settings) {
......
......@@ -10,10 +10,16 @@ import Vector from './lib/Vector.js';
*/
function simplify_line(points, min_area)
{
let smallest_area = Number.MAX_SAFE_INTEGER, smallest_area_i = 1;
// We need 3+ points to use this algorithm!
if(points.length < 3)
return points;
while(smallest_area > min_area)
points = points.slice(); // Shallow clone the array
while(true)
{
let smallest_area = Number.MAX_SAFE_INTEGER, smallest_area_i = 1;
for(let i = 1; i < points.length - 1; i++)
{
let next_area = triangle_area(points[i - 1], points[i], points[i + 1]);
......@@ -23,9 +29,14 @@ function simplify_line(points, min_area)
}
}
if(smallest_area >= min_area || points.length <= 3)
break;
// Remove the central point of the smallest triangle
points.splice(smallest_area_i, 1);
}
return points;
}
/**
......@@ -39,7 +50,7 @@ function triangle_area(a, b, c)
{
return Math.abs(
(
a.x * (b.x - c.x) +
a.x * (b.y - c.y) +
b.x * (c.y - a.y) +
c.x * (a.y - b.y)
) / 2
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment