JSFiddle - React, Tailwind, and code Playground
by vigneshwaranr
HTML
<script src="https://rawgithub.com/vigneshwaranr/line_intersection.js/master/line_intersection.js"></script>
<ul id="output"></ul>
<script>
var output = document.getElementById('output');
function assert(outcome, description) {
var li = document.createElement('li');
li.className = outcome ? 'pass' : 'fail';
li.innerHTML = description;
output.appendChild(li);
};
function checkIntersection(intersect, x, y) {
function lineContains(line, x, y) {
for (var i = 0; i < line.length; i++) {
if (line[i][0] === x && line[i][1] === y) {
return true;
}
}
return false;
}
return intersect.icptX === x &&
intersect.icptY === y &&
lineContains(intersect.line1_data, x, y) &&
lineContains(intersect.line2_data, x, y);
}
</script>
CSS
.pass,.fail {
padding: 20px 0px;
}
.pass:before {
content:'PASS: ';
color: blue;
font-weight: bold;
}
.fail:before {
content:'FAIL: ';
color: red;
font-weight: bold;
}
JavaScript
(function() {
assert(getLineIntersectionData() === undefined,
'getLineIntersectionData() === undefined');
})();
(function() {
var line = [[0, 0], [1, 1]];
assert(getLineIntersectionData(line) === undefined,
'getLineIntersectionData(onlyOneLine) === undefined');
})();
(function() {
var line1 = [[0, 0], [1, 1]];
var invalidLineFormat = [0, 0];
assert(getLineIntersectionData(line1, invalidLineFormat) === undefined,
'getLineIntersectionData(line1, invalidLineFormat) === undefined');
})();
(function() {
var line1 = [[0, 0], [1, 0]];
var lineWithOnlyOnePoint = [[0, 1]];
assert(getLineIntersectionData(line1, lineWithOnlyOnePoint, "invalidOptions") === undefined,
'getLineIntersectionData(line1, lineWithOnlyOnePoint, "invalidOptions") === undefined');
})();
(function() {
var line1 = [[0, 0], [1, 0]];
var line2 = [[0, 1], [1, 1]];
assert(getLineIntersectionData(line1, line2, "invalidOptions") === undefined,
'getLineIntersectionData(line1, line2, "invalidOptions") === undefined');
})();
(function() {
var line1 = [[0, 0], [1, 0]];
var line2 = [[0, 1], [1, 1]];
var intersect = getLineIntersectionData(line1, line2);
assert(intersect === undefined,
'Parallel lines (=) L1 [[0, 0], [1, 0]] and ' +
'L2 [[0, 1], [1, 1]] can never meet');
})();
(function() {
var line1 = [[0, 0], [0, 1]];
var line2 = [[1, 0], [1, 1]];
var intersect = getLineIntersectionData(line1, line2);
assert(intersect === undefined,
'Parallel lines (||) L1 [[0, 0], [0, 1]] and ' +
'L2 [[1, 0], [1, 1]] can never meet');
})();
(function() {
var line1 = [[1, 0], [2, 1]];
var line2 = [[0, 1], [1, 2]];
var intersect = getLineIntersectionData(line1, line2);
assert(intersect === undefined,
'Parallel lines (//) L1 [[1, 0], [2, 1]] and ' +
'L2 [[0, 1], [1, 2]] can never meet');
})();
(function() {
...