Skip to content

intersection

The area two rectangles have in common.

Rectangles that only touch (along an edge or at a corner) have no area in common.

Available since v3.1.5

import { intersection } from '@helpers4/geometry';
// or, from the all-in-one package (same code, one install):
import { intersection } from 'helpers4/geometry';
intersection(a: Rect, b: Rect): Rect | undefined
ParameterTypeDescription
aRectA rectangle
bRectAnother rectangle

Rect | undefined — The overlapping rectangle, or undefined when they do not overlap

Intersect a layer with the viewport.

intersection({ x: 0, y: 0, width: 10, height: 10 }, { x: 5, y: 5, width: 10, height: 10 })
// => { x: 5, y: 5, width: 5, height: 5 }

Sharing an edge is not overlapping.

intersection({ x: 0, y: 0, width: 10, height: 10 }, { x: 10, y: 0, width: 10, height: 10 }) // => undefined

An axis-aligned rectangle: its top-left corner and its size. Width and height are expected to be non-negative.

interface Rect {
  height: number;
  width: number;
  x: number;
  y: number;
}

View source on GitHub