Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

segseg

tmpvar20.4kMIT1.0.0

2d segment to segment intersection detection

2dmath, intersection, collision, 2d

readme

segseg

Build Status

Intersection detection between two line segments in 2d space.

Usage

import segseg from 'segseg'


/*
                (0, 5)
                   |
  (-10, 0) --------+-------  (10, 0)
                   |
                (0, -5)
*/

const isect = [ NaN, NaN ] // the output vector where collision point is stored

//                       seg 1                   seg 2
//                ┌-------------------┐   ┌-----------------┐
if (segseg(isect, [ -10, 0 ], [ 10, 0 ],  [ 0, 5 ], [ 0, -5 ]))
    console.log('intersected!', isect.join(','))  // output:   intersected! 0,0

Node compatibility

This is a pure es module, and requires node v12+ to run. However if you're using a popular bundler such as rollup, webpack, etc. This should be compatible with most frontend setups too.

Credits

This code was ported from Mukesh Prasad's example implementation which was included in graphics gems 2.

Internally the code uses a point-segment check for some edge cases, from https://gist.github.com/mattdesl/47412d930dcd8cd765c871a65532ffac

changelog

1.0.0

  • refactor the API to provide a garbage collector friendly invocation and better performance (#2)
  • switch to pure es module (#3)
  • bump node dependency to v12+
  • handle some additional failing intersection tests for colinear cases
  • handle some an additional failing intersection test when a segment has one point on another segment (#1)
  • add travis build badge
  • simplify usage instructions in readme
  • switch to es6 (use let and const everywhere)
  • update URL in credits

0.2.2

  • update URL in credits

0.2.1

  • export to window object

0.2.0

  • support multiple point input formats

0.1.1

  • fix some collision bugs
  • update readme

0.1.0

  • initial implementation