Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
npm-debug.log
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 1.1.0
* expose `isPointOnSegment` function
* use `point-to-segment-2d` module
* remove stale warning about node `< 12.x` which has been unsupported for a while now


# 1.0.0
* accidentally changed default behavior to allow segments <= 1 distance to be considered intersecting
* refactor the API to provide a garbage collector friendly invocation and better performance (#2)
Expand Down
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,17 @@ segseg(isect, [ 1, 1 ], [ 5, 3 ], [ 3, 3 ], [ 5, 6 ], ESPILON) // returns true
```


## Node compatibility
### segment-point intersection

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.
This module also exposes a simple boolean test indicating if a point intersects a segment:

```javascript
import { isPointOnSegment } from 'segseg'

// point segment
// ┌──────┐ ┌───────────────────┐
console.log(isPointOnSegment([ 20, 0], [ -10, 0 ], [ 10, 0 ])) // false
```


## Credits
Expand Down
20 changes: 15 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import segpoint from './segpoint.js'
import dist from 'point-to-segment-2d'


const DONT_INTERSECT = 0
Expand Down Expand Up @@ -92,29 +92,39 @@ export default function segseg (out, p1, p2, p3, p4, epsilon=0) {
return result

// handle colinear cases and when a line segment endpoint lies on the other segment
if (segpoint(p1, p3, p4, epsilon)) {
if (isPointOnSegment(p1, p3, p4, epsilon)) {
out[0] = p1[0]
out[1] = p1[1]
return true
}

if (segpoint(p2, p3, p4, epsilon)) {
if (isPointOnSegment(p2, p3, p4, epsilon)) {
out[0] = p2[0]
out[1] = p2[1]
return true
}

if (segpoint(p3, p1, p2, epsilon)) {
if (isPointOnSegment(p3, p1, p2, epsilon)) {
out[0] = p3[0]
out[1] = p3[1]
return true
}

if (segpoint(p4, p1, p2, epsilon)) {
if (isPointOnSegment(p4, p1, p2, epsilon)) {
out[0] = p4[0]
out[1] = p4[1]
return true
}

return false
}


// p - point
// t0 - start point of segment
// t1 - end point of segment
// epsilon - maximum distance from the segment that a point can still be considered on the segment
// return boolean indicating if p is on the segment
export function isPointOnSegment (p, t0, t1, epsilon=0) {
return dist(p, t0, t1) <= epsilon
}
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
"collision",
"2d"
],
"engines" : {
"node" : ">=12"
"engines": {
"node": ">=12"
},
"author": "Elijah Insua <tmpvar@gmail.com>",
"license": "MIT"
"license": "MIT",
"dependencies": {
"point-to-segment-2d": "^1.0.0"
}
}
34 changes: 0 additions & 34 deletions segpoint.js

This file was deleted.

9 changes: 7 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import t from 'assert';
import segseg from '../index.js';
import t from 'assert'
import segseg from '../index.js'
import { isPointOnSegment } from '../index.js'


const out = [ 0, 0 ] // the output vector
Expand Down Expand Up @@ -140,3 +141,7 @@ t.deepEqual(segseg(out, [ 1, 1 ], [ 5, 3 ], [ 3, 3 ], [ 5, 6 ], EPSILON), true)


t.equal(segseg(out, [ 1, 1 ], [ 5, 3 ], [ 3, 3 ], [ 5, 6 ]), false)


t.equal(isPointOnSegment([ 0, 0], [ -10, 0 ], [ 10, 0 ]), true)
t.equal(isPointOnSegment([ 20, 0], [ -10, 0 ], [ 10, 0 ]), false)
Loading