Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rounding error between triangle area calculation and intersection calculation #60

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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