intersects

repository·master·Indexed 20 days ago

https://github.com/davidfig/intersects

A collection of 2D collision and intersection checkers supporting points, circles, circle outlines, lines, axis-aligned boxes, ellipses, and convex polygons. Version 2.7.1 provides a comprehensive set of functions for detecting collisions between these geometric primitives, available via npm, yarn, or CDN.

Tokens
3.3K
Snippets
13
Records
14
Agent score
19%

What's inside intersects

  1. Include intersects via CDN for browser use

    master

    For projects not using a bundler (like Rollup or Browserify), you can include the prepackaged UMD library via unpkg. This exposes a global Intersects object.

    <script src="https://unpkg.com/intersects/umd/intersects.min.js"></script>
    <script>
        var intersected = Intersects.polygonPoint(points, x, y);
    </script>
  2. Use specific intersection modules

    master

    If you only need a specific intersection check (like a circle-to-box check), you can require the specific sub-module directly to reduce bundle size.

    var circleBox = require('intersects/circle-box');
    
    var intersected = circleBox(x, y, r, x1, y1, w1, h1);
  3. Use intersects via CommonJS

    master

    To use the full library in a Node.js or bundled environment, require the main intersects module. This provides access to all collision/intersection checkers (e.g., boxBox).

    var intersects = require('intersects');
    
    var intersected = intersects.boxBox(x1, y1, w1, h1, x2, y2, w2, h2);
  4. Box collision functions

    master

    Functions for detecting collisions where the first shape is an axis-aligned bounding box (AABB). Boxes are defined by their top-left corner (x, y), width, and height.

    | Function | Parameters | Description |
    |---|---|---|
    | `boxBox` | `x1, y1, w1, h1, x2, y2, w2, h2` | Box-box collision |
    | `boxCircle` | `xb, yb, wb, hb, xc, yc, rc` | Box-circle collision |
    | `boxEllipse` | `xb, yb, wb, hb, xe, ye, rex, rey` | Box-ellipse collision |
    | `boxLine` | `xb, yb, wb, hb, x1, y1, x2, y2` | Box-line collision |
    | `boxPoint` | `x1, y1, w1, h1, x2, y2` | Box-point collision |
    | `boxPolygon` | `xb, yb, wb, hb, points` | Box-polygon (convex) collision |
    | `boxCircleOutline` | `xb, yb, wb, hb, xc, yc, rc` | Box-circle outline collision |
  5. Point collision functions

    master

    Functions for detecting collisions where the first shape is a single point (x, y).

    | Function | Parameters | Description |
    |---|---|---|
    | `pointBox` | `x1, y1, xb, yb, wb, hb` | Point-box collision |
    | `pointPolygon` | `x1, y1, points` | Point-polygon (convex) collision |
    | `pointLine` | `xp, yp, x1, y1, x2, y2` | Point-line collision |
    | `pointCircle` | `xp, yp, xc, yc, rc` | Point-circle collision |
    | `pointEllipse` | `xp, yp, xe, ye, rex, rey` | Point-ellipse collision |
    | `pointCircleOutline` | `x2, y2, x1, y1, r1` | Point-circle outline collision |
  6. Circle collision functions

    master

    Functions for detecting collisions where the first shape is a circle. Circles are defined by their center (xc, yc) and radius rc.

    | Function | Parameters | Description |
    |---|---|---|
    | `circleBox` | `xc, yc, rc, xb, yb, wb, hb` | Circle-box collision |
    | `circleCircle` | `x1, y1, r1, x2, y2, r2` | Circle-circle collision |
    | `circleEllipse` | `xc, yc, rc, xe, ye, rex, rey` | Circle-ellipse collision |
    | `circleLine` | `xc, yc, rc, x1, y1, x2, y2` | Circle-line collision |
    | `circlePoint` | `x1, y1, r1, x2, y2` | Circle-point collision |
    | `circlePolygon` | `xc, yc, rc, points` | Circle-polygon (convex) collision |
    | `circleCircleOutline` | `xc, yc, rc, xco, yco, rco` | (Not available yet) |
  7. Line collision functions

    master

    Functions for detecting collisions where the first shape is a line segment. Lines are defined by two points (x1, y1) and (x2, y2).

    | Function | Parameters | Description |
    |---|---|---|
    | `lineBox` | `x1, y1, x2, y2, xb, yb, wb, hb` | Line-box collision |
    | `lineCircle` | `x1, y1, x2, y2, xc, yc, rc` | Line-circle collision |
    | `lineEllipse` | `x1, y1, x2, y2, xe, ye, rex, rey` | Line-ellipse collision |
    | `lineLine` | `x1, y1, x2, y2, x3, y3, x4, y4, thickness1, thickness2` | Line-line collision |
    | `linePolygon` | `x1, y1, x2, y2, points, tolerance` | Line-polygon (convex) collision |
    | `linePoint` | `x1, y1, x2, y2, xp, yp` | Line-point collision |
    | `lineCircleOutline` | `x1, y1, x2, y2, xc, yc, rc` | Line-circle outline collision |
  8. Ellipse collision functions

    master

    Functions for detecting collisions where the first shape is an ellipse. Ellipses are defined by their center (xe, ye) and x/y radii rex, rey.

    | Function | Parameters | Description |
    |---|---|---|
    | `ellipseBox` | `xe, ye, rex, rey, x, y, w, h` | Ellipse-box collision |
    | `ellipseCircle` | `xe, ye, rex, rey, xc, yc, rc` | Ellipse-circle collision |
    | `ellipseEllipse` | `x1, y1, r1x, r1y, x2, y2, r2x, r2y` | Ellipse-ellipse collision |
    | `ellipseLine` | `xe, ye, rex, rey, x1, y1, x2, y2` | Ellipse-line collision |
    | `ellipsePoint` | `xe, ye, rex, rey, x, y` | Ellipse-point collision |
    | `ellipsePolygon` | `xe, ye, rex, rey, points2` | Ellipse-polygon collision |
  9. Polygon collision functions

    master

    Functions for detecting collisions where the first shape is a convex polygon. Polygons are defined by a flat array of coordinates [x1, y1, x2, y2, ... xn, yn].

    | Function | Parameters | Description |
    |---|---|---|
    | `polygonBox` | `points, x, y, w, h` | Polygon-box collision |
    | `polygonCircle` | `points, xc, yc, rc` | Polygon-circle collision |
    | `polygonEllipse` | `points, xe, ye, rex, rey` | Polygon-ellipse collision |
    | `polygonLine` | `points, x1, y1, x2, y2, tolerance` | Polygon-line collision |
    | `polygonPoint` | `points, x, y` | Polygon-point collision |
    | `polygonPolygon` | `points1, points2` | Polygon-polygon collision |
  10. Circle Outline collision functions

    master

    Functions for detecting collisions where the first shape is only the circumference (outline) of a circle.

    | Function | Parameters | Description |
    |---|---|---|
    | `circleOutlineBox` | `xc, yc, rc, xb, yb, wb, hb` | Circle outline-box collision |
    | `circleOutlineCircle` | `xco, yco, rco, xc, yc, rc` | (Not available yet) |
    | `circleOutlineEllipse` | `xc, yc, rc, xe, ye, rex, rey` | (Not available yet) |
    | `circleOutlineLine` | `xc, yc, rc, x1, y1, x2, y2` | Circle outline-line collision |
    | `circleOutlinePoint` | `x1, y1, r1, x2, y2` | Circle outline-point collision |
    | `circleOutlinePolygon` | `xc, yc, rc, points` | (Not available yet) |
  11. API Reference: Collision Detection Functions

    master

    The intersects library provides a comprehensive set of functions for detecting collisions between various geometric shapes. The API is organized by the primary shape type of the first argument. Supported shapes include:

    • box: Axis-aligned rectangles (AABB).
    • circle: Circles defined by center and radius.
    • line: Line segments defined by two points.
    • point: Single coordinates (x, y).
    • polygon: Convex polygons defined by a flat array of coordinates [x1, y1, x2, y2, ...].
    • ellipse: Ellipses defined by center and x/y radii.
    • circleOutline: Only the circumference of a circle.

    Each function follows a naming convention: {shape1}{Shape2}(params), where shape1 is the type of the first object and Shape2 is the type of the second object (e.g., boxCircle for box-circle collision).