{"id":16426,"library":"line-circle-collision","title":"Line-Circle Collision Detection","description":"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.","status":"maintenance","version":"1.1.3","language":"javascript","source_language":"en","source_url":"git://github.com/mattdesl/line-circle-collision","tags":["javascript","line","circle","point","collision","hit","test","hittest","collide"],"install":[{"cmd":"npm install line-circle-collision","lang":"bash","label":"npm"},{"cmd":"yarn add line-circle-collision","lang":"bash","label":"yarn"},{"cmd":"pnpm add line-circle-collision","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"import { collide } from 'line-circle-collision';","symbol":"collide","correct":"const collide = require('line-circle-collision');"},{"note":"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`.","wrong":"import * as lineCircleCollision from 'line-circle-collision';\nconst collide = lineCircleCollision.collide;","symbol":"collide (ESM)","correct":"import collide from 'line-circle-collision';"}],"quickstart":{"code":"const collide = require('line-circle-collision');\n\n// Define the circle (center X, center Y) and its radius\nconst circleCenter = [50, 50];\nconst radius = 25;\n\n// Define the line segment by its two endpoints (A and B)\nconst linePointA = [20, 30];\nconst linePointB = [80, 70];\n\n// Optional: a vector to store the nearest collision point\nconst nearestPoint = [0, 0]; \n\n// Perform the collision test\nconst hit = collide(linePointA, linePointB, circleCenter, radius, nearestPoint);\n\nif (hit) {\n  console.log(`Collision detected!`);\n  console.log(`Nearest collision point: [${nearestPoint[0]}, ${nearestPoint[1]}]`);\n} else {\n  console.log('No collision.');\n}\n\n// Example of no collision\nconst noHitLineA = [0,0];\nconst noHitLineB = [10,10];\nconst noHit = collide(noHitLineA, noHitLineB, circleCenter, radius);\nconsole.log(`No collision example: ${noHit}`);","lang":"javascript","description":"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."},"warnings":[{"fix":"Always check the boolean return value of `collide` before using the `nearest` array's contents.","message":"The `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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure all vector inputs (`a`, `b`, `circle`) are consistently formatted as `[number, number]` arrays.","message":"The `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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"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.","cause":"Attempting to use named import syntax (`import { collide } from '...'`) for a CommonJS module that exports a single function as its `module.exports`.","error":"TypeError: collide is not a function"},{"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.","cause":"Using `require()` in an ECMAScript Module (ESM) file or a browser environment without a compatible bundler/runtime.","error":"ReferenceError: require is not defined"},{"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]`.","cause":"Input parameters `a`, `b`, or `circle` are not correctly formatted as 2D arrays (e.g., `[x, y]`), leading to internal vector operations failing.","error":"TypeError: a.map is not a function (or similar property access errors)"}],"ecosystem":"npm"}