包详细信息

line-intersect

psalaets33.1kMIT3.0.0

Line vs line, point vs line in 2D space

line, segment, intersect, 2d

自述文件

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'

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

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

更新日志

Change Log

All notable changes to this project will be documented in this file.

[3.0.0] - 2021-02-03

Changed

  • Improved TypeScript typings. Thanks to @seleb

[2.2.4] - 2020-07-13

Fixed

  • Include all built files in package.

[2.2.3] - 2019-10-10

Fixed

  • Declare module in TypeScript defs file, thanks @klymenkoo

[2.2.2] - 2019-10-08

Changed

  • Small package.json change so typescript defs are included when this package is installed.

[2.2.1] - 2019-05-23

Changed

  • Docs changes

[2.2.0] - 2018-12-04

Added

  • Typescript defs

[2.1.1] - 2018-02-20

Added

  • Docs clean up

[2.1.0] - 2018-02-20

Added

  • Now supporting es modules in additional to commonjs modules so app bundlers can take advantage of that

[2.0.0] - 2018-02-18

Breaking changes

  • Return same result object when intersect result is none, parallel, colinear

This is only a breaking change if you mutate the result object returned by checkIntersection().

[1.0.0] - 2014-10-29

Added

  • Initial impl