Skip to content

Commit

Permalink
fix: add epsilon check, try fix w8r#155
Browse files Browse the repository at this point in the history
  • Loading branch information
wuyuxiang committed Apr 19, 2023
1 parent 738a698 commit 2178702
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/segment_intersection.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//const EPS = 1e-9;
const EPS = 1e-9;

/**
* Finds the magnitude of the cross product of two vectors (if we pretend
Expand Down Expand Up @@ -71,14 +71,14 @@ export default function (a1, a2, b1, b2, noEndpointTouch) {
let kross = crossProduct(va, vb);
let sqrKross = kross * kross;
const sqrLenA = dotProduct(va, va);
//const sqrLenB = dotProduct(vb, vb);
const sqrLenB = dotProduct(vb, vb);

// Check for line intersection. This works because of the properties of the
// cross product -- specifically, two vectors are parallel if and only if the
// cross product is the 0 vector. The full calculation involves relative error
// to account for possible very small line segments. See Schneider & Eberly
// for details.
if (sqrKross > 0/* EPS * sqrLenB * sqLenA */) {
if (sqrKross > EPS * sqrLenB * sqrLenA) {
// If they're not parallel, then (because these are line segments) they
// still might not actually intersect. This code checks that the
// intersection point of the lines is actually on both line segments.
Expand Down Expand Up @@ -109,11 +109,11 @@ export default function (a1, a2, b1, b2, noEndpointTouch) {
// the (vector) difference between the two initial points. If this is parallel
// with the line itself, then the two lines are the same line, and there will
// be overlap.
//const sqrLenE = dotProduct(e, e);
const sqrLenE = dotProduct(e, e);
kross = crossProduct(e, va);
sqrKross = kross * kross;

if (sqrKross > 0 /* EPS * sqLenB * sqLenE */) {
if (sqrKross > EPS * sqrLenB * sqrLenE ) {
// Lines are just parallel, not the same. No overlap.
return null;
}
Expand Down

0 comments on commit 2178702

Please sign in to comment.