Skip to content

Commit

Permalink
Merge pull request #2 from mfogel/fix-another-rounding-error
Browse files Browse the repository at this point in the history
Add failing test and fix for it
  • Loading branch information
grassick authored Feb 6, 2018
2 parents 4dd24ed + 9a91291 commit 7db3374
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/signed_area.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var intersection = require('./segment_intersection');

/**
* Signed area of the triangle (p0, p1, p2)
* @param {Array.<Number>} p0
Expand All @@ -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]);
};
25 changes: 25 additions & 0 deletions test/edge_cases.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down

0 comments on commit 7db3374

Please sign in to comment.