Commit b52f6bcb by Starbeamrainbowlabs

### Rewrite distance_from_line, but it's still not working correctly :-(

parent bb5213cb
 ... ... @@ -10,9 +10,7 @@ import Vector from './lib/Vector.js'; */ function simplify_line(points, epsilon) { // Don't bother with really short lines // if(points.length <= 2) // return points; points = collapse_points(points); let first_point = points[0], last_point = points[points.length - 1]; ... ... @@ -20,7 +18,7 @@ function simplify_line(points, epsilon) let farthest_index = 0, farthest_distance = 0; for (let i = 1; i < points.length - 1; i++) { let next_distance = sq_distance_from_line(first_point, last_point, points[i]); let next_distance = distance_from_line(first_point, last_point, points[i]); if(next_distance > farthest_distance) { farthest_index = i; farthest_distance = next_distance; ... ... @@ -28,7 +26,7 @@ function simplify_line(points, epsilon) } // This point is far enough away that it's meaningful - recurse! if(farthest_distance > epsilon*epsilon) { if(farthest_distance > epsilon) { let left_side = simplify_line(points.slice(0, farthest_index + 1), epsilon), right_side = simplify_line(points.slice(farthest_index), epsilon); ... ... @@ -41,6 +39,22 @@ function simplify_line(points, epsilon) ]; } /** * Collapses sequences of points that are identical. * @param {[Vector]} points The list of points to collapse. * @return {[Vector]} The list of collapsed points. */ function collapse_points(points) { let result = []; for(let i = 0; i < points.length; i++) { if(result.length > 0 && result[result.length - 1].equalTo(points[i])) continue; result.push(points[i]); } return result; } /** * Calculates the squared shortest distance between a point and the specified line. * @param {Vector} line_start The point at which the line starts. ... ... @@ -49,12 +63,21 @@ function simplify_line(points, epsilon) * @return {number} The shortest distance between the specified point * and the specified line. */ function sq_distance_from_line(line_start, line_end, point) function distance_from_line(line_start, line_end, point) { let main_line_length = line_end.clone().subtract(line_start).length; let direct_line_length = line_end.clone().subtract(point).length; let main_line = line_end.clone().subtract(line_start); let main_line_length = main_line.length; if(main_line_length === 0) return line_end.clone().subtract(point).length; let projectionFactor = Math.max(0, Math.min(1, point.clone().subtract(line_start).dotProduct(main_line) / main_line_length )); let projection = main_line.clone().multiply(projectionFactor).add(line_start); return Math.abs((direct_line_length*direct_line_length) - ((main_line_length/2)*(main_line_length/2))); return projection.clone().subtract(point).length; } export { simplify_line };
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!