Registry / data / line-circle-collision

line-circle-collision

JSON →
library1.1.3jsnpmunverified

The `line-circle-collision` package provides a focused utility for performing 2D collision tests between a line segment and a circle. It returns a boolean indicating whether an intersection occurred and can optionally store the nearest collision point. The current stable version is 1.1.3, last published in 2014. Given its stability and specific use case, it operates under a maintenance cadence rather than active feature development. This package differentiates itself by offering a simple, low-level geometric test without the overhead of a full physics engine, making it suitable for projects requiring precise collision detection for these specific shapes. It's often used in conjunction with other basic geometric utilities like `point-in-triangle` or `point-circle-collision` for building more complex collision systems.

npm install line-circle-collision
INSTALL
IMPORT
SIG · LINE-CIRCLE-COLLIS
L
line-circle-collision
datajavascriptv1.1.3
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

collide
const collide = require('line-circle-collision');
import { collide } from 'line-circle-collision';
This package is primarily a CommonJS module. Attempting named ESM imports will result in errors as it exports a single function as its module.exports.
collide (ESM)
import collide from 'line-circle-collision';
import * as lineCircleCollision from 'line-circle-collision'; const collide = lineCircleCollision.collide;
While primarily CommonJS, Node.js and bundlers often provide interoperability allowing a default ESM import. The `import * as` pattern would require accessing the `default` property, e.g., `lineCircleCollision.default`.

This quickstart demonstrates how to use the `collide` function to check for an intersection between a line segment and a circle, including how to retrieve the nearest collision point if a hit occurs.

const collide = require('line-circle-collision'); // Define the circle (center X, center Y) and its radius const circleCenter = [50, 50]; const radius = 25; // Define the line segment by its two endpoints (A and B) const linePointA = [20, 30]; const linePointB = [80, 70]; // Optional: a vector to store the nearest collision point const nearestPoint = [0, 0]; // Perform the collision test const hit = collide(linePointA, linePointB, circleCenter, radius, nearestPoint); if (hit) { console.log(`Collision detected!`); console.log(`Nearest collision point: [${nearestPoint[0]}, ${nearestPoint[1]}]`); } else { console.log('No collision.'); } // Example of no collision const noHitLineA = [0,0]; const noHitLineB = [10,10]; const noHit = collide(noHitLineA, noHitLineB, circleCenter, radius); console.log(`No collision example: ${noHit}`);
Debug
Known issues
gotchaThe `nearest` output parameter is only updated if a collision (`hit` returns true) is detected. If `hit` is false, the contents of the `nearest` array should not be relied upon, as they might be stale or uninitialized from a previous call.
fix
Always check the boolean return value of `collide` before using the `nearest` array's contents.
affects: >=1.0.0
gotchaThe `a`, `b`, and `circle` parameters are expected to be 2D vectors represented as arrays of two numbers (e.g., `[x, y]`). Providing objects, non-array types, or arrays with incorrect dimensions will lead to runtime errors.
fix
Ensure all vector inputs (`a`, `b`, `circle`) are consistently formatted as `[number, number]` arrays.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: collide is not a function
Attempting to use named import syntax (`import { collide } from '...'`) for a CommonJS module that exports a single function as its `module.exports`.
fix
Use CommonJS `require` syntax (`const collide = require('line-circle-collision');`) or a default ESM import (`import collide from 'line-circle-collision';`) if your environment supports CJS-ESM interop.
ReferenceError: require is not defined
Using `require()` in an ECMAScript Module (ESM) file or a browser environment without a compatible bundler/runtime.
fix
If in an ESM context, change `const collide = require('line-circle-collision');` to `import collide from 'line-circle-collision';`. For browser use, ensure your build process bundles CommonJS modules.
TypeError: a.map is not a function (or similar property access errors)
Input parameters `a`, `b`, or `circle` are not correctly formatted as 2D arrays (e.g., `[x, y]`), leading to internal vector operations failing.
fix
Verify that all vector inputs are consistently `[number, number]` arrays. For example, `[5, 6]` for a point, not `{x: 5, y: 6}` or `[5]`.
Upgrade
Version history
1.1.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
line-circle-collision — npm install line-circle-collision · libregistry