3.0.0 • Published 3 years ago

line-intersect v3.0.0

Weekly downloads
8,377
License
MIT
Repository
github
Last release
3 years ago

line-intersect

Line vs line, point vs line in 2D space

Math is mostly from here.

Install

npm install line-intersect

API

es modules

import {
  checkIntersection,
  colinearPointWithinSegment
} from 'line-intersect';

commonjs modules

const {
  checkIntersection,
  colinearPointWithinSegment
} = require('line-intersect');

checkIntersection(x1, y1, x2, y2, x3, y3, x4, y4)

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.

Parameters

  • x1, y1, x2, y2 - 1st line segment
  • x3, y3, x4, y4 - 2nd line segment

All params are Numbers and are required.

Returns

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'

typeWhat it meansAre the line segments touching?
noneLine segments are not intersectingNo
parallelLine segments are not intersecting but they are parallel to eachotherNo
colinearLine segments are on the same line and may be overlapping. Use colinearPointWithinSegment() to checkMaybe
intersectingLine segments intersect at exactly one pointYes

colinearPointWithinSegment(px, py, x1, y1, x2, y2)

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.

Parameters

  • px, py - Point to check
  • x1, y1, x2, y2 - Line segment

All params are Numbers and are required.

Returns

true if point is within the line segment, false otherwise.

License

MIT