diff --git a/src/signed_area.js b/src/signed_area.js index dad818c..a544fba 100644 --- a/src/signed_area.js +++ b/src/signed_area.js @@ -1,5 +1,7 @@ 'use strict'; +var intersection = require('./segment_intersection'); + /** * Signed area of the triangle (p0, p1, p2) * @param {Array.} p0 @@ -8,5 +10,8 @@ * @return {Number} */ module.exports = function signedArea(p0, p1, p2) { + // avoid rounding error b/t intersection calculation and triangle area calc + var inters = intersection(p0, p1, p0, p2) + if (inters.length === 2) return 0 return (p0[0] - p2[0]) * (p1[1] - p2[1]) - (p1[0] - p2[0]) * (p0[1] - p2[1]); }; diff --git a/test/edge_cases.test.js b/test/edge_cases.test.js index cbc635e..090631d 100644 --- a/test/edge_cases.test.js +++ b/test/edge_cases.test.js @@ -218,6 +218,31 @@ tap.test('Edge cases', function(main) { t.end(); }); + main.test('no rounding error between intersection calculation and triangle area', (t) => { + const p1 = [[ + [-62.8, -41], + [-63.0001099, -41.1121599], + [-62.93564, -41.0940399], + [-62.8, -41] + ]]; + const p2 =[[ + [-62.8, -41.2], + [-62.8, -41], + [-62.964969880531925, -41.10228339712406], + [-63.0001099, -41.1121599], + [-62.8, -41.2] + ]]; + const expected = [[[ + [-63.0001099, -41.1121599], + [-62.964969880531925, -41.10228339712406], + [-62.8, -41], + [-63.0001099, -41.1121599] + ]]] + + t.deepEqual(martinez.diff(p1, p2), expected) + t.end(); + }); + main.test('collapsed edges removed', (t) => { const p1 = [[ [355,139],