Skip to content

Commit

Permalink
Fix #1. patch 1.0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
radi-cho committed Aug 13, 2018
1 parent a3333df commit 3dd342f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.log
test
*/node_modules
35 changes: 25 additions & 10 deletions npm/index.js
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;
2 changes: 1 addition & 1 deletion npm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "line-intersection",
"version": "1.0.7",
"version": "1.0.9",
"description": "App that shows the intersection between 2 lines or line segments",
"main": "index.js",
"repository": {
Expand Down

0 comments on commit 3dd342f

Please sign in to comment.