API Reference

Geometric.js uses the geometric primitives points, lines, and polygons.

  • Points are represented as arrays of two numbers, such as [0, 0].
  • Lines are represented as arrays of two points, such as [[0, 0], [1, 0]]. Because they have endpoints, these are technically line segments, but Geometric.js refers to them as lines for simplicity’s sake.
  • Polygons are represented as arrays of vertices, each of which is a point, such as [[0, 0], [1, 0], [1, 1], [0, 1]]. Polygons can be closed, meaning the first and last vertices are the same, or open.
  • There are also functions to calculate relationships between these primitives.
  • Polygon boolean operations return a single array of points. If a true geometric result would split into multiple regions or create a hole, Geometric.js bridges those regions with straight segments instead of returning MultiPolygons.

You will also encounter angles, areas, distances, and lengths.

  • Angles are represented as numbers, measured in degrees. Geometric.js also provides functions to convert angles from degrees to radians or vice versa.
  • Areas, distances, and lengths are represented as numbers, measured in pixels.

Points

pointRotate

geometric.pointRotate(point[, angle[, origin]])

#

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

Returns

Point

Parameters

point

Point

angle

number

origin

Point

Examples

pointTranslate

geometric.pointTranslate(point[, angle[, distance]])

#

Returns the coordinates resulting from translating a point by an angle in degrees and a distance. If angle is not specified, defaults to 0. If distance is not specified, defaults to 0.

Returns

Point

Parameters

point

Point

angle

number

distance

number

Examples

Lines

lineAngle

geometric.lineAngle(line)

#

Returns the angle of a line, in degrees, with respect to the horizontal axis.

Returns

number

Parameters

line

Line

Examples

lineInterpolate

geometric.lineInterpolate(line[, clamp])

#

Returns an interpolator function given a line [a, b]. The returned interpolator function takes a single argument t, 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 returned interpolator will clamp the output to the ends of the line segment. You can pass an optional boolean indicating whether to return points outside the line segment if t is greater than 1 or less than 0.

Returns

(t: number) => Point

Parameters

line

Line

clamp

boolean

Examples

lineLength

geometric.lineLength(line)

#

Returns the length of a line.

Returns

number

Parameters

line

Line

Examples

lineRotate

geometric.lineRotate(line, angle[, origin])

#

Returns the coordinates resulting from rotating a line about an origin by an angle in degrees. If origin is not specified, the origin defaults to the midpoint of the line.

Returns

Line

Parameters

line

Line

angle

number

origin

Point

Examples

lineTranslate

geometric.lineTranslate(line, angle, distance)

#

Returns the coordinates resulting from translating a line by an angle in degrees and a distance. If angle is not specified, defaults to 0. If distance is not specified, defaults to 0.

Returns

Line

Parameters

line

Line

angle

number

distance

number

Examples

Polygons

polygonArea

geometric.polygonArea(vertices[, signed])

#

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

Returns

number

Parameters

vertices

Polygon

signed

boolean

Examples

polygonBounds

geometric.polygonBounds(polygon)

#

Returns the bounds of a polygon, 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 polygon has fewer than three points.

Returns

Bounds | null

Parameters

polygon

Polygon

Examples

polygonClose

geometric.polygonClose(polygon)

#

Returns a new polygon that is closed by appending the first point of the polygon to the end if it is not already closed. The input polygon is not modified.

Returns

Polygon

Parameters

polygon

Polygon

Examples

polygonClosed

geometric.polygonClosed(polygon)

#

Returns a boolean indicating whether the given polygon is closed. A polygon is considered closed if its first point is identical to its last point.

Returns

boolean

Parameters

polygon

Polygon

polygonDifference

geometric.polygonDifference(polygonA, polygonB)

#

Returns the portion of polygonA that is outside polygonB. Holes are bridged into a single point array.

Returns

Polygon | null

Parameters

polygonA

Polygon

polygonB

Polygon

Examples

polygonIntersection

geometric.polygonIntersection(polygonA, polygonB)

#

Returns the overlapping area shared by two polygons.

Returns

Polygon | null

Parameters

polygonA

Polygon

polygonB

Polygon

Examples

polygonUnion

geometric.polygonUnion(polygonA, polygonB)

#

Returns the union of two polygons. Disjoint results are bridged into a single point array.

Returns

Polygon | null

Parameters

polygonA

Polygon

polygonB

Polygon

Examples

polygonXor

geometric.polygonXor(polygonA, polygonB)

#

Returns the symmetric difference of two polygons. Split results are bridged into a single point array.

Returns

Polygon | null

Parameters

polygonA

Polygon

polygonB

Polygon

Examples

polygonHull

geometric.polygonHull(points)

#

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

Returns

Polygon | null

Parameters

points

Point[]

Examples

polygonInterpolate

geometric.polygonInterpolate(polygon[, clamp])

#

Returns an interpolator function given a polygon of vertices [a, b, ..., n]. The returned interpolator function takes a single argument t, 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.

You can pass an optional boolean, which defaults to true, indicating whether to clamp t to the range [0, 1]. When clamp is false, the interpolator applies modular arithmetic to t. If t is less than 0, the interpolator wraps around the polygon's perimeter in reverse. If t is greater than 1, the interpolator continues forward along the perimeter.

Returns

(t: number) => Point

Parameters

polygon

Polygon

clamp

boolean

Examples

polygonMean

geometric.polygonMean(vertices)

#

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.

Returns

Point | []

Parameters

vertices

Polygon

Examples

polygonRandom

geometric.polygonRandom([sides[, area[, centroid]]])

#

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

Returns

Polygon

Parameters

sides

number

area

number

centroid

Point

0]]

Examples

polygonReflectX

geometric.polygonReflectX(polygon[, reflectFactor])

#

Reflects a polygon over its vertical midline. Pass an optional reflectFactor 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.

Returns

Polygon

Parameters

polygon

Polygon

reflectFactor

number

Examples

polygonReflectY

geometric.polygonReflectY(polygon[, reflectFactor])

#

Reflects a polygon over its horizontal midline. Pass an optional reflectFactor 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.

Returns

Polygon

Parameters

polygon

Polygon

reflectFactor

number

Examples

polygonRegular

geometric.polygonRegular([sides[, area[, center]]])

#

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

Returns

Polygon

Parameters

sides

number

area

number

center

Point

Examples

polygonRotate

geometric.polygonRotate(polygon, angle[, origin])

#

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

Returns

Polygon

Parameters

polygon

Polygon

angle

number

origin

Point

Examples

polygonScale

geometric.polygonScale(polygon[, scale[, origin]])

#

Returns the vertices resulting from scaling a polygon by a scaleFactor (where 1 is the polygon's current size) from an origin point. If origin 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 scaleFactor. To scale the polygon's area by the scaleFactor itself, see geometric.polygonScaleArea.

Returns

Polygon

Parameters

polygon

Polygon

scale

number

origin

Point

Examples

polygonScaleArea

geometric.polygonScaleArea(polygon, scale[, origin])

#

Returns the vertices resulting from scaling a polygon by a scaleFactor (where 1 is the polygon's current size) from an origin point. If origin 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 scaleFactor. To scale the polygon's area by the square of the scaleFactor, see geometric.polygonScale.

Returns

Polygon

Parameters

polygon

Polygon

scale

number

origin

Point

Examples

polygonScaleX

geometric.polygonScaleX(polygon, scale[, origin])

#

Returns the vertices resulting from scaling the horizontal coordinates of a polygon by a scaleFactor (where 1 is the polygon's current size) from an origin point. The vertical coordinates remain unchanged. If origin 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 scaleFactor.

Returns

Polygon

Parameters

polygon

Polygon

scale

number

origin

Point

Examples

polygonScaleY

geometric.polygonScaleY(polygon, scale[, origin])

#

Returns the vertices resulting from scaling the vertical coordinates of a polygon by a scaleFactor (where 1 is the polygon's current size) from an origin point. The horizontal coordinates remain unchanged. If origin 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 scaleFactor.

Returns

Polygon

Parameters

polygon

Polygon

scale

number

origin

Point

Examples

polygonTranslate

geometric.polygonTranslate(polygon, angle, distance)

#

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

Returns

Polygon

Parameters

polygon

Polygon

angle

number

distance

number

Examples

polygonWind

geometric.polygonWind(polygon[, order])

#

Returns a polygon in the specified winding order. If an order 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 polygon has fewer than three points.

Uses the convention that a polygon with a negative signed area has a clockwise winding order, and a polygon with a positive signed area has a counter-clockwise winding order. As a result, the winding order will appear reversed on computer screens where the y-axis is reversed (i.e. 0 is on top rather than on bottom).

Returns

Polygon | null

Parameters

polygon

Polygon

order

"cw" | "clockwise" | "ccw" | "counterclockwise"

Examples

Relationships

lineIntersection

geometric.lineIntersection(a, b)

#

Returns a point where line a intersects line b. If the two lines do not intersect, returns null.

Returns

Point | null

Parameters

a

Line

b

Line

Examples

lineIntersectsPolygon

geometric.lineIntersectsPolygon(line, polygon)

#

Returns an array of points where a line intersects a polygon. If the line does not intersect the polygon, returns null.

Returns

Point[] | null

Parameters

line

Line

polygon

Polygon

Examples

pointOnPolygon

geometric.pointOnPolygon(point, polygon[, epsilon])

#

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

Returns

boolean

Parameters

point

Point

polygon

Polygon

epsilon

number

Examples

pointOnLine

geometric.pointOnLine(point, line[, epsilon])

#

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

Returns

boolean

Parameters

point

Point

line

Line

epsilon

number

Examples

pointWithLine

geometric.pointWithLine(point, line[, epsilon])

#

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

Returns

boolean

Parameters

point

Point

line

Line

epsilon

number

Examples

pointsEqual

geometric.pointsEqual(pointA, pointB[, epsilon])

#

Returns a boolean representing whether two points are equal within an optional epsilon tolerance. If epsilon is not specified, a small default tolerance is used.

Returns

boolean

Parameters

pointA

Point

pointB

Point

epsilon

number

pointToLine

geometric.pointToLine(line, point)

#

Returns the closest position on a line to a point.

Returns

Point

Parameters

line

Line

point

Point

Examples

pointToPolygon

geometric.pointToPolygon(polygon, point)

#

Returns the closest position on the perimeter of a polygon to a point.

Returns

Point | []

Parameters

polygon

Polygon

point

Point

Examples

polygonInPolygon

geometric.polygonInPolygon(polygonA, polygonB)

#

Returns a boolean representing whether polygonA is contained by polygonB. Points and edges on polygonB's boundary are considered contained.

Returns

boolean

Parameters

polygonA

Polygon

polygonB

Polygon

Examples

polygonIntersectsPolygon

geometric.polygonIntersectsPolygon(polygonA, polygonB)

#

Returns a boolean representing whether polygonA intersects but is not contained by polygonB.

Returns

boolean

Parameters

polygonA

Polygon

polygonB

Polygon

Examples

Angles

angleReflect

geometric.angleReflect(incidenceAngle, surfaceAngle)

#

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

Returns

number

Parameters

incidenceAngle

number

surfaceAngle

number

Examples

angleToDegrees

geometric.angleToDegrees(angle)

#

Returns the result of converting an angle in radians to the same angle in degrees.

Returns

number

Parameters

angle

number

angleToRadians

geometric.angleToRadians(angle)

#

Returns the result of converting an angle in degrees to the same angle in radians.

Returns

number

Parameters

angle

number