Line vs line, point vs line in 2D space
Math is mostly from here.
npm install line-intersect
es modules
import {
checkIntersection,
colinearPointWithinSegment
} from 'line-intersect';
commonjs modules
const {
checkIntersection,
colinearPointWithinSegment
} = require('line-intersect');
Given a line segment from (x1, y1) to (x2, y2) and line segment from (x3, y3) to (x4, y4), check if the line segments intersect.
- x1, y1, x2, y2 - 1st line segment
- x3, y3, x4, y4 - 2nd line segment
All params are Numbers and are required.
A read-only Object that looks like
{
type: 'none' | 'parallel' | 'colinear'
}
// or
{
type: 'intersecting',
point: {
x: number,
y: number
}
}
point
is only exists when type == 'intersecting'
type |
What it means | Are the line segments touching? |
---|---|---|
none | Line segments are not intersecting | No |
parallel | Line segments are not intersecting but they are parallel to eachother | No |
colinear | Line segments are on the same line and may be overlapping. Use colinearPointWithinSegment() to check |
Maybe |
intersecting | Line segments intersect at exactly one point | Yes |
Given a point (px, py) that is on the same line as line segment (x1, y1) to (x2, y2), check if the point is within the line segment.
- px, py - Point to check
- x1, y1, x2, y2 - Line segment
All params are Numbers and are required.
true
if point is within the line segment, false
otherwise.
MIT