Home

Awesome

Geometric.js

A JavaScript library for doing geometry.

<img src="https://raw.githubusercontent.com/HarryStevens/geometric/master/img/angle-thumb.png" /> <img src="https://raw.githubusercontent.com/HarryStevens/geometric/master/img/length-thumb.png" /> <img src="https://raw.githubusercontent.com/HarryStevens/geometric/master/img/centroid-thumb.png" />

Installation

Web browser

In vanilla, a geometric global is exported. You can use the latest version from unpkg.

<script src="https://unpkg.com/geometric@2.5.5/build/geometric.js"></script>
<script src="https://unpkg.com/geometric@2.5.5/build/geometric.min.js"></script>

If you'd rather host it yourself, download the latest release from the build directory.

npm

npm i geometric -S
const geometric = require("geometric");

API

Geometric.js uses the geometric primitives <b>points</b>, <b>lines</b>, and <b>polygons</b>.

You will also encounter <b>angles</b>, <b>areas</b>, <b>distances</b>, and <b>lengths</b>.

<hr />

<a name="points"></a>Points

<a name="pointRotate" href="#pointRotate">#</a> geometric.<b>pointRotate</b>(<i>point</i>, <i>angle</i>[, <i>origin</i>]) · Source, Example

Returns the coordinates resulting from rotating a <i>point</i> about an origin by an <i>angle</i> in degrees. If <i>origin</i> is not specified, the origin defaults to [0, 0].

<a name="pointTranslate" href="#pointTranslate">#</a> geometric.<b>pointTranslate</b>(<i>point</i>, <i>angle</i>, <i>distance</i>) · Source, Example

Returns the coordinates resulting from translating a <i>point</i> by an <i>angle</i> in degrees and a <i>distance</i>.

<hr />

<a name="lines"></a>Lines

<a name="lineAngle" href="#lineAngle">#</a> geometric.<b>lineAngle</b>(<i>line</i>) · Source, Example

Returns the angle of a <i>line</i>, in degrees, with respect to the horizontal axis.

<a name="lineInterpolate" href="#lineInterpolate">#</a> geometric.<b>lineInterpolate</b>(<i>line</i>[, <i>clamp</i>]) · Source, Example

Returns an interpolator function given a <i>line</i> [a, b]. The returned interpolator function takes a single argument <i>t</i>, where t is a number in [0, 1]; a value of 0 returns a, while a value of 1 returns b. Intermediate values interpolate from a to b along the line segment.

By default, the interpolator will return points outside of the line segment if t is less than 0 or greater than 1. You can pass an optional boolean indicating whether to <i>clamp</i> the returned point to inside of the line segment, even if t is greater than 1 or less than 0.

<a name="lineLength" href="#lineLength">#</a> geometric.<b>lineLength</b>(<i>line</i>) · Source, Example

Returns the length of a <i>line</i>.

<a name="lineMidpoint" href="#lineMidpoint">#</a> geometric.<b>lineMidpoint</b>(<i>line</i>) · Source, Example

Returns the midpoint of a <i>line</i>.

<a name="lineRotate" href="#lineRotate">#</a> geometric.<b>lineRotate</b>(<i>line</i>, <i>angle</i>[, <i>origin</i>]) · Source, Example

Returns the coordinates resulting from rotating a <i>line</i> about an origin by an <i>angle</i> in degrees. If <i>origin</i> is not specified, the origin defaults to the <a href="#lineMidpoint">midpoint</a> of the line.

<a name="lineTranslate" href="#lineTranslate">#</a> geometric.<b>lineTranslate</b>(<i>line</i>, <i>angle</i>, <i>distance</i>) · Source, Example

Returns the coordinates resulting from translating a <i>line</i> by an <i>angle</i> in degrees and a <i>distance</i>.

<hr />

<a name="polygons"></a>Polygons

<a name="polygonArea" href="#polygonArea">#</a> geometric.<b>polygonArea</b>(<i>polygon</i>[, <i>signed</i>]) · Source, Example

Returns the area of a <i>polygon</i>. You can pass a boolean indicating whether the returned area is <i>signed</i>, which defaults to false.

<a name="polygonBounds" href="#polygonBounds">#</a> geometric.<b>polygonBounds</b>(<i>polygon</i>) · Source, Example

Returns the bounds of a <i>polygon</i>, ignoring points with invalid values (null, undefined, NaN, Infinity). The returned bounds are represented as an array of two points, where the first point is the top-left corner and the second point is the bottom-right corner. For example:

const rectangle = [[0, 0], [0, 1], [1, 1], [1, 0]];
const bounds = geometric.polygonBounds(rectangle); // [[0, 0], [1, 1]]

Returns null if the <i>polygon</i> has fewer than three points.

<a name="polygonCentroid" href="#polygonCentroid">#</a> geometric.<b>polygonCentroid</b>(<i>polygon</i>) · Source, Example

Returns the weighted centroid of a <i>polygon</i>. Not to be confused with a mean center.

<a name="polygonHull" href="#polygonHull">#</a> geometric.<b>polygonHull</b>(<i>points</i>) · Source, Example

Returns the convex hull, represented as a polygon, for an array of <i>points</i>. Returns null if the input array has fewer than three points. Uses Andrew’s monotone chain algorithm.

<a name="polygonInterpolate" href="#polygonInterpolate">#</a> geometric.<b>polygonInterpolate</b>(<i>polygon</i>]) · Source, Example

Returns an interpolator function given a <i>polygon</i> of vertices [a, ..., n]. The returned interpolator function takes a single argument <i>t</i>, where t is a number in [0, 1]; a value of 0 returns a, while a value of 1 returns n. Intermediate values interpolate from a to n along the polygon's perimeter.

<a name="polygonLength" href="#polygonLength">#</a> geometric.<b>polygonLength</b>(<i>polygon</i>) · Source, Example

Returns the length of a <i>polygon</i>'s perimeter.

<a name="polygonMean" href="#polygonMean">#</a> geometric.<b>polygonMean</b>(<i>polygon</i>) · Source, Example

Returns the arithmetic mean of the vertices of a polygon. Keeps duplicate vertices, resulting in different values for open and closed polygons. Not to be confused with a centroid.

<a name="polygonRandom" href="#polygonRandom">#</a> geometric.<b>polygonRandom</b>([<i>sides</i>[, <i>area</i>[, <i>centroid</i>]]]) · Source, Example

Returns the vertices of a random convex polygon of the specified number of <i>sides</i>, <i>area</i>, and <i>centroid</i> coordinates. If <i>sides</i> is not specified, defaults to 3. If <i>area</i> is not specified, defaults to 100. If <i>centroid</i> is not specified, defaults to [0, 0]. The returned polygon's winding order will be counter-clockwise. Based on an algorithm by Pavel Valtr and an implementation by Maneesh Agrawala.

<a name="polygonReflectX" href="#polygonReflectX">#</a> geometric.<b>polygonReflectX</b>(<i>polygon</i>[, <i>reflectFactor</i>]) · Source, Example

Reflects a <em>polygon</em> over its vertical midline. Pass an optional <em>reflectFactor</em> between 0 and 1, where 1 indicates a full reflection, 0 leaves the polygon unchanged, and 0.5 collapses the polygon on its vertical midline.

<a name="polygonReflectX" href="#polygonReflectY">#</a> geometric.<b>polygonReflectY</b>(<i>polygon</i>[, <i>reflectFactor</i>]) · Source, Example

Reflects a <em>polygon</em> over its horizontal midline. Pass an optional <em>reflectFactor</em> between 0 and 1, where 1 indicates a full reflection, 0 leaves the polygon unchanged, and 0.5 collapses the polygon on its horizontal midline.

<a name="polygonRegular" href="#polygonRegular">#</a> geometric.<b>polygonRegular</b>([<i>sides</i>[, <i>area</i>[, <i>center</i>]]]) · Source, Example

Returns the vertices of a regular polygon of the specified number of <i>sides</i>, <i>area</i>, and <i>center</i> coordinates. If <i>sides</i> is not specified, defaults to 3. If <i>area</i> is not specified, defaults to 100. If <i>center</i> is not specified, defaults to [0, 0]. The returned polygon's winding order will be counter-clockwise.

<a name="polygonRotate" href="#polygonRotate">#</a> geometric.<b>polygonRotate</b>(<i>polygon</i>, <i>angle</i>[, <i>origin</i>]) · Source, Example

Returns the vertices resulting from rotating a <i>polygon</i> about an origin by an <i>angle</i> in degrees. If <i>origin</i> is not specified, the origin defaults to [0, 0].

<a name="polygonScale" href="#polygonScale">#</a> geometric.<b>polygonScale</b>(<i>polygon</i>, <i>scaleFactor</i>[, <i>origin</i>]) · Source, Example

Returns the vertices resulting from scaling a <i>polygon</i> by a <i>scaleFactor</i> (where 1 is the polygon's current size) from an origin point. If <i>origin</i> is not specified, the origin defaults to the polygon's centroid.

The returned polygon's area is equal to the input polygon's area multiplied by the square of the <i>scaleFactor</i>. To scale the polygon's area by the <i>scaleFactor</i> itself, see <a href="#polygonScaleArea">geometric.polygonScaleArea</a>.

<a name="polygonScaleArea" href="#polygonScaleArea">#</a> geometric.<b>polygonScaleArea</b>(<i>polygon</i>, <i>scaleFactor</i>[, <i>origin</i>]) · Source, Example

Returns the vertices resulting from scaling a <i>polygon</i> by a <i>scaleFactor</i> (where 1 is the polygon's current size) from an origin point. If <i>origin</i> is not specified, the origin defaults to the polygon's centroid.

The returned polygon's area is equal to the input polygon's area multiplied by the <i>scaleFactor</i>. To scale the polygon's area by the square of the <i>scaleFactor</i>, see <a href="#polygonScale">geometric.polygonScale</a>.

<a name="polygonScaleX" href="#polygonScaleX">#</a> geometric.<b>polygonScaleX</b>(<i>polygon</i>, <i>scaleFactor</i>[, <i>origin</i>]) · Source, Example

Returns the vertices resulting from scaling the horizontal coordinates of a <i>polygon</i> by a <i>scaleFactor</i> (where 1 is the polygon's current size) from an origin point. The vertical coordinates remain unchanged. If <i>origin</i> is not specified, the origin defaults to the polygon's centroid.

The returned polygon's area is equal to the input polygon's area multiplied by the <i>scaleFactor</i>.

<a name="polygonScaleY" href="#polygonScaleY">#</a> geometric.<b>polygonScaleY</b>(<i>polygon</i>, <i>scaleFactor</i>[, <i>origin</i>]) · Source, Example

Returns the vertices resulting from scaling the vertical coordinates of a <i>polygon</i> by a <i>scaleFactor</i> (where 1 is the polygon's current size) from an origin point. The horizontal coordinates remain unchanged. If <i>origin</i> is not specified, the origin defaults to the polygon's centroid.

The returned polygon's area is equal to the input polygon's area multiplied by the <i>scaleFactor</i>.

<a name="polygonTranslate" href="#polygonTranslate">#</a> geometric.<b>polygonTranslate</b>(<i>polygon</i>, <i>angle</i>, <i>distance</i>) · Source, Example

Returns the vertices resulting from translating a <i>polygon</i> by an <i>angle</i> in degrees and a <i>distance</i>.

<a name="polygonWind" href="#polygonWind">#</a> geometric.<b>polygonWind</b>(<i>polygon</i>[, <i>order</i>]) · Source, Example

Returns a <i>polygon</i> in the specified winding order. If an <i>order</i> string is passed as either "cw" or "clockwise", returns a polygon with a clockwise winding order. Otherwise, returns a polygon with a counter-clockwise winding order. Returns null if the <i>polygon</i> has fewer than three points.

On computer screens where the top-left corner is at [0, 0], a polygon with a negative signed area has a counter-clockwise winding order.

<hr />

<a name="relationships"></a>Relationships

<a name="lineIntersectsLine" href="#lineIntersectsLine">#</a> geometric.<b>lineIntersectsLine</b>(<i>lineA</i>, <i>lineB</i>) · Source, Example

Returns a boolean representing whether <i>lineA</i> intersects <i>lineB</i>.

<a name="lineIntersectsPolygon" href="#lineIntersectsPolygon">#</a> geometric.<b>lineIntersectsPolygon</b>(<i>line</i>, <i>polygon</i>) · Source, Example

Returns a boolean representing whether a <i>line</i> intersects a <i>polygon</i>.

<a name="pointInPolygon" href="#pointInPolygon">#</a> geometric.<b>pointInPolygon</b>(<i>point</i>, <i>polygon</i>) · Source, Example

Returns a boolean representing whether a <i>point</i> is inside of a <i>polygon</i>. Uses ray casting.

<a name="pointOnPolygon" href="#pointOnPolygon">#</a> geometric.<b>pointOnPolygon</b>(<i>point</i>, <i>polygon</i>[, <i>epsilon</i>]) · Source, Example

Returns a boolean representing whether a <i>point</i> is located on one of the edges of a <i>polygon</i>. An optional <i>epsilon</i> number, such as 1e-6, can be passed to reduce the precision with which the relationship is measured.

<a name="pointOnLine" href="#pointOnLine">#</a> geometric.<b>pointOnLine</b>(<i>point</i>, <i>line</i>[, <i>epsilon</i>]) · Source, Example

Returns a boolean representing whether a <i>point</i> is collinear with a <i>line</i> and is also located on the line segment. An optional <i>epsilon</i> number, such as 1e-6, can be passed to reduce the precision with which the relationship is measured. See also pointWithLine.

<img width="150" src="https://raw.githubusercontent.com/HarryStevens/geometric/master/img/point-on-with-line.png" />

<a name="pointWithLine" href="#pointWithLine">#</a> geometric.<b>pointWithLine</b>(<i>point</i>, <i>line</i>[, <i>epsilon</i>]) · Source, Example

Returns a boolean representing whether a <i>point</i> is collinear with a <i>line</i>. An optional <i>epsilon</i> number, such as 1e-6, can be passed to reduce the precision with which the relationship is measured. See also pointOnLine.

<a name="pointLeftofLine" href="#pointLeftofLine">#</a> geometric.<b>pointLeftofLine</b>(<i>point</i>, <i>line</i>) · Source, Example

Returns a boolean representing whether a <i>point</i> is to the left of a <i>line</i>.

<a name="pointRightofLine" href="#pointRightofLine">#</a> geometric.<b>pointRightofLine</b>(<i>point</i>, <i>line</i>) · Source, Example

Returns a boolean representing whether a <i>point</i> is to the right of a <i>line</i>.

<a name="polygonInPolygon" href="#polygonInPolygon">#</a> geometric.<b>polygonInPolygon</b>(<i>polygonA</i>, <i>polygonB</i>) · Source, Example

Returns a boolean representing whether <i>polygonA</i> is contained by <i>polygonB</i>.

<a name="polygonIntersectsPolygon" href="#polygonIntersectsPolygon">#</a> geometric.<b>polygonIntersectsPolygon</b>(<i>polygonA</i>, <i>polygonB</i>) · Source, Example

Returns a boolean representing whether <i>polygonA</i> intersects but is not contained by <i>polygonB</i>.

<hr />

Angles

<a name="angleReflect" href="#angleReflect">#</a> geometric.<b>angleReflect</b>(<i>incidence</i>, <i>surface</i>) · Source, Example

Returns the angle of reflection given a starting angle, also known as the angle of <i>incidence</i>, and the angle of the <i>surface</i> off of which it is reflected.

<a name="angleToDegrees" href="#angleToDegrees">#</a> geometric.<b>angleToDegrees</b>(<i>angle</i>) · Source

Returns the result of converting an <i>angle</i> in radians to the same angle in degrees.

<a name="angleToRadians" href="#angleToRadians">#</a> geometric.<b>angleToRadians</b>(<i>angle</i>) · Source

Returns the result of converting an <i>angle</i> in degrees to the same angle in radians.