Intersects
a simple collection of 2d collision/intersects functions. Supports points, circles, ellipses, lines, axis-aligned boxes, and polygons
Install / Use
/learn @davidfig/IntersectsREADME
intersects
Collection of 2d collision/intersection checkers, supporting points, circles, circle circumferences (outline of circle), ellipses, lines, rectangles, and polygons (covex).
Live Example
https://davidfig.github.io/intersects/
Installation
npm i intersects
or
yarn add intersects
Usage
var intersects = require('intersects');
var intersected = intersects.boxBox(x1, y1, w1, h1, x2, y2, w2, h2);
or
var circleBox = require('intersects/circle-box');
var intersected = circleBox(x, y, r, x1, y1, w1, h1);
Alternative Usage
If you don't want to package the library using rollup, browserify, etc., you can also include the prepackaged library, which includes a global Intersects object:
<script src="https://unpkg.com/intersects/umd/intersects.min.js"></script>
<script>
var intersected = Intersects.polygonPoint(points, x, y);
</script>
API
- box (AABB / axis-aligned rectangle)
- circle
- line
- point
- polygon (convex)
- ellipse
- circleOutline (only the circumference of circle)
boxBox(x1, y1, w1, h1, x2, y2, w2, h2)
Box-box collision.
Param | Meaning
---|---
x1 | top-left corner of first box
y1 | top-left corner of first box
w1 | width of first box
h1 | height of first box
x2 | top-left corner of second box
y2 | top-left corner of second box
w2 | width of second box
h2 | height of second box
boxCircle(xb, yb, wb, hb, xc, yc, rc)
Box-circle collision.
Param | Meaning
---|---
xb | top-left corner of box
yb | top-left corner of box
wb | width of box
hb | height of box
xc | center of circle
yc | center of circle
rc | radius of circle
boxEllipse(xb, yb, wb, hb, xe, ye, rex, rey)
Box-ellipse collision.
Param | Meaning
---|---
xb | top-left corner of box
yb | top-left corner of box
wb | width of box
hb | height of box
xe | center of ellipse
ye | center of ellipse
rex | x-radius of ellipse
rey | y-radius of ellipse
boxLine(xb, yb, wb, hb, x1, y1, x2, y2)
Box-line collision.
Param | Meaning
---|---
xb | top-left corner of box
yb | top-left corner of box
wb | width of box
hb | height of box
x1 | first point of line
y1 | first point of line
x2 | second point of line
y2 | second point of line
boxPoint(x1, y1, w1, h1, x2, y2)
Box-point collision.
Param | Meaning
---|---
x1 | top-left corner of box
y1 | top-left corner of box
w1 | width of box
h1 | height of box
x2 | point x
y2 | point y
boxPolygon(xb, yb, wb, hb, points)
Box-polygon (convex) collision.
Param | Meaning
---|---
xb | top-left corner of box
yb | top-left corner of box
wb | width of box
hb | height of box
points | [x1, y1, x2, y2, ... xn, yn] of polygon
boxCircleOutline(xb, yb, wb, hb, xc, yc, rc)
Box (axis-oriented rectangle)-Circle outline (circumference of circle) collision.
Param | Meaning
---|---
xb | top-left corner of rectangle
yb | top-left corner of rectangle
wb | width of rectangle
hb | height of rectangle
xc | center of circle outline
yc | center of circle outline
rc | radius of circle outline
circleBox(xc, yc, rc, xb, yb, wb, hb)
Circle-box (axis-oriented rectangle) collision.
Param | Meaning
---|---
xc | center of circle
yc | center of circle
rc | radius of circle
xb | top-left corner of rectangle
yb | top-left corner of rectangle
wb | width of rectangle
hb | height of rectangle
circleCircle(x1, y1, r1, x2, y2, r2)
Circle-circle collision.
Param | Meaning
---|---
x1 | center of circle 1
y1 | center of circle 1
r1 | radius of circle 1
x2 | center of circle 2
y2 | center of circle 2
r2 | radius of circle 2
circleEllipse(xc, yc, rc, xe, ye, rex, rey)
Circle-ellipse collision.
Param | Meaning
---|---
x1 | center of circle
y1 | center of circle
r1 | radius of circle
xe | center of ellipse
ye | center of ellipse
rex | x-radius of ellipse
rey | y-radius of ellipse
circleLine(xc, yc, rc, x1, y1, x2, y2)
Circle-line collision.
Param | Meaning
---|---
xc | center of circle
yc | center of circle
rc | radius of circle
x1 | first point of line
y1 | first point of line
x2 | second point of line
y2 | second point of line
circlePoint(x1, y1, r1, x2, y2)
Circle-point collision.
Param | Meaning
---|---
x1 | center of circle
y1 | center of circle
r1 | radius of circle
x2 | point x
y2 | point y
circlePolygon(xc, yc, rc, points)
Circle-polygon (convex) collision.
Param | Meaning
---|---
xc | center of circle
yc | center of circle
rc | radius of circle
points | [x1, y1, x2, y2, ... xn, yn] of polygon
circleCircleOutline(xc, yc, rc, xco, yco, rco)
(Not available yet.) Circle-Circle outline (circumference of circle) collision.
Param | Meaning
---|---
xc | center of circle
yc | center of circle
rc | radius of circle
xco | center of circle outline
yco | center of circle outline
rco | radius of circle outline
lineBox(x1, y1, x2, y2, xb, yb, wb, hb)
Line-box collision.
Param | Meaning
---|---
x1 | point 1 of line
y1 | point 1 of line
x2 | point 2 of line
y2 | point 2 of line
xb | top-left of box
yb | top-left of box
wb | width of box
hb | height of box
lineCircle(x1, y1, x2, y2, xc, yc, rc)
Line-circle collision.
Param | Meaning
---|---
x1 | point 1 of line
y1 | point 1 of line
x2 | point 2 of line
y2 | point 2 of line
xc | center of circle
yc | center of circle
rc | radius of circle
lineEllipse(x1, y1, x2, y2, xe, ye, rex, rey)
Line-ellipse collision.
Param | Meaning
---|---
x1 | point 1 of line
y1 | point 1 of line
x2 | point 2 of line
y2 | point 2 of line
xe | center of ellipse
ye | center of ellipse
rex | x-radius of ellipse
rey | y-radius of ellipse
lineLine(x1, y1, x2, y2, x3, y3, x4, y4, thickness1, thickness2)
Line-line collision.
Param | Meaning
---|---
x1 | first point in line 1
y1 | first point in line 1
x2 | second point in line 1
y2 | second point in line 1
x3 | first point in lin
