From dab4825785dcbd20ca7c09a4b0dfacbddbf64d7b Mon Sep 17 00:00:00 2001 From: Mike Date: Fri, 1 Nov 2024 11:24:55 -0700 Subject: [PATCH] 1.1.0 --- .gitignore | 1 + CHANGELOG.md | 6 ++++++ README.md | 12 ++++++++++-- index.js | 20 +++++++++++++++----- package-lock.json | 20 ++++++++++++++++++++ package.json | 9 ++++++--- segpoint.js | 34 ---------------------------------- test/index.js | 9 +++++++-- 8 files changed, 65 insertions(+), 46 deletions(-) delete mode 100644 segpoint.js diff --git a/.gitignore b/.gitignore index c8f50f7..93f1361 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +node_modules npm-debug.log diff --git a/CHANGELOG.md b/CHANGELOG.md index b86debf..6d41c06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/README.md b/README.md index cad5f7e..84e38a9 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.js b/index.js index 646c0a5..c10ffcd 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ -import segpoint from './segpoint.js' +import dist from 'point-to-segment-2d' const DONT_INTERSECT = 0 @@ -92,25 +92,25 @@ 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 @@ -118,3 +118,13 @@ export default function segseg (out, p1, p2, p3, p4, epsilon=0) { 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 +} diff --git a/package-lock.json b/package-lock.json index e58f7f6..86c3747 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,11 +5,31 @@ "requires": true, "packages": { "": { + "name": "segseg", "version": "1.0.0", "license": "MIT", + "dependencies": { + "point-to-segment-2d": "^1.0.0" + }, "engines": { "node": ">=12" } + }, + "node_modules/point-to-segment-2d": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/point-to-segment-2d/-/point-to-segment-2d-1.0.0.tgz", + "integrity": "sha512-qjMaW51jOWDjvNSEntnxiGCvfWKu64Iiu6FVRaCWB5hBlZG7adxhx1AG3sXEwgf7v7e+o0y7TpO65OgumpDhfg==", + "license": "MIT", + "engines": { + "node": ">=12" + } + } + }, + "dependencies": { + "point-to-segment-2d": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/point-to-segment-2d/-/point-to-segment-2d-1.0.0.tgz", + "integrity": "sha512-qjMaW51jOWDjvNSEntnxiGCvfWKu64Iiu6FVRaCWB5hBlZG7adxhx1AG3sXEwgf7v7e+o0y7TpO65OgumpDhfg==" } } } diff --git a/package.json b/package.json index adbec7f..313dba6 100644 --- a/package.json +++ b/package.json @@ -17,9 +17,12 @@ "collision", "2d" ], - "engines" : { - "node" : ">=12" + "engines": { + "node": ">=12" }, "author": "Elijah Insua ", - "license": "MIT" + "license": "MIT", + "dependencies": { + "point-to-segment-2d": "^1.0.0" + } } diff --git a/segpoint.js b/segpoint.js deleted file mode 100644 index 6e0c208..0000000 --- a/segpoint.js +++ /dev/null @@ -1,34 +0,0 @@ -// from https://gist.github.com/mattdesl/47412d930dcd8cd765c871a65532ffac - -function sqr (x) { - return x * x -} - - -function dist2 (v, w) { - return sqr(v[0] - w[0]) + sqr(v[1] - w[1]) -} - - -// p - point -// v - start point of segment -// w - end point of segment -function distToSegmentSquared (p, v, w) { - const l2 = dist2(v, w) - if (l2 === 0) - return dist2(p, v) - - let t = ((p[0] - v[0]) * (w[0] - v[0]) + (p[1] - v[1]) * (w[1] - v[1])) / l2 - t = Math.max(0, Math.min(1, t)) - return dist2(p, [ v[0] + t * (w[0] - v[0]), v[1] + t * (w[1] - v[1]) ]) -} - - -// 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 default function distToSegment (p, t0, t1, epsilon) { - return !(Math.sqrt(distToSegmentSquared(p, t0, t1)) > epsilon) -} diff --git a/test/index.js b/test/index.js index 862b832..8322608 100644 --- a/test/index.js +++ b/test/index.js @@ -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 @@ -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) \ No newline at end of file