Skip to main content

Geometry

2D maths for points, rectangles and polygons — distance, angle, midpoint, area, and hit-testing. Coordinates are passed as plain numbers rather than Point or Rect objects, so nothing here needs a class, a canvas or an allocation per call, and it works equally for layout code, games and charts. Angles are radians to match Math.atan2 and the Canvas API, and distance2D uses Math.hypot, which avoids overflow on large coordinates.

Example

import { distance2D, pointInRect, polygonArea } from '@rtorcato/js-common/geometry'

// Hit-test a click against a box, in plain numbers — no Point/Rect classes.
const box = el.getBoundingClientRect()
pointInRect(event.clientX, event.clientY, box.left, box.top, box.width, box.height)

distance2D(0, 0, 3, 4) // 5 — via Math.hypot, no overflow
polygonArea([[0, 0], [4, 0], [4, 3]]) // 6

Import

import { angle2D, distance2D, midpoint2D } from '@rtorcato/js-common/geometry'

Exports

NameSummary
angle2DCalculates the angle (in radians) between two points in 2D space.
distance2DCalculates the distance between two points in 2D space.
midpoint2DCalculates the midpoint between two points in 2D space.
pointInRectChecks if a point is inside a rectangle.
polygonAreaCalculates the area of a polygon given its vertices (Shoelace formula).
rectsOverlapChecks if two rectangles overlap.

See also

  • numbers — sum, average, clamp, roundTo
  • numbers — sum, average, clamp, roundTo
  • colors — hex/RGB conversion, lighten, darken