-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
29 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
*.log | ||
test | ||
*/node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,48 @@ | ||
function findIntersection([P1, P2, P3, P4]) { | ||
const x = | ||
function findIntersection(array) { | ||
var P1 = array[0], | ||
P2 = array[1], | ||
P3 = array[2], | ||
P4 = array[3]; | ||
|
||
var x = | ||
((P1.x * P2.y - P2.x * P1.y) * (P3.x - P4.x) - | ||
(P1.x - P2.x) * (P3.x * P4.y - P3.y * P4.x)) / | ||
((P1.x - P2.x) * (P3.y - P4.y) - (P1.y - P2.y) * (P3.x - P4.x)); | ||
const y = | ||
var y = | ||
((P1.x * P2.y - P2.x * P1.y) * (P3.y - P4.y) - | ||
(P1.y - P2.y) * (P3.x * P4.y - P3.y * P4.x)) / | ||
((P1.x - P2.x) * (P3.y - P4.y) - (P1.y - P2.y) * (P3.x - P4.x)); | ||
return { x: x, y: y }; | ||
} | ||
|
||
function isPointBetween(p, a, b) { | ||
return ((a.x <= p.x && p.x <= b.x) || (a.x >= p.x && p.x >= b.x)) && ((a.y <= p.y && p.y <= b.y) || (a.y >= p.y && p.y >= b.y)); | ||
return ( | ||
((a.x <= p.x && p.x <= b.x) || (a.x >= p.x && p.x >= b.x)) && | ||
((a.y <= p.y && p.y <= b.y) || (a.y >= p.y && p.y >= b.y)) | ||
); | ||
} | ||
|
||
function findSegmentIntersection(points) { | ||
const i1 = findIntersection(points); | ||
const [P1, P2, P3, P4] = points; | ||
const isIntersected = isPointBetween(i1, P1, P2) && isPointBetween(i1, P3, P4); | ||
var i1 = findIntersection(points); | ||
var P1 = points[0], | ||
P2 = points[1], | ||
P3 = points[2], | ||
P4 = points[3]; | ||
const isIntersected = | ||
isPointBetween(i1, P1, P2) && isPointBetween(i1, P3, P4); | ||
return isIntersected ? i1 : false; | ||
} | ||
|
||
function isSegmentIntersected(points) { | ||
const i1 = findIntersection(points); | ||
const [P1, P2, P3, P4] = points; | ||
var i1 = findIntersection(points); | ||
var P1 = points[0], | ||
P2 = points[1], | ||
P3 = points[2], | ||
P4 = points[3]; | ||
return isPointBetween(i1, P1, P2) && isPointBetween(i1, P3, P4); | ||
} | ||
|
||
exports.findIntersection = findIntersection; | ||
exports.isPointBetween = isPointBetween; | ||
exports.findSegmentIntersection = findSegmentIntersection; | ||
exports.isSegmentIntersected = isSegmentIntersected; | ||
exports.isSegmentIntersected = isSegmentIntersected; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters