Skip to content

applyToRect

Transforms a rectangle by a matrix and returns its bounding box: the smallest axis-aligned rectangle that contains it. After a rotation or a skew, the box is larger than the rectangle.

Available since v3.1.5

import { applyToRect } from '@helpers4/geometry';
// or, from the all-in-one package (same code, one install):
import { applyToRect } from 'helpers4/geometry';
applyToRect(m: Matrix, r: Rect): Rect
ParameterTypeDescription
mMatrixThe matrix
rRectThe rectangle

Rect — The bounding box of the transformed rectangle

The bounding box of the transformed rectangle.

applyToRect(scale(2), { x: 1, y: 1, width: 2, height: 3 }) // => { x: 2, y: 2, width: 4, height: 6 }

After a rotation, the box is larger than the rectangle.

applyToRect(rotate(Math.PI / 4), { x: 0, y: 0, width: 1, height: 1 }).width // => √2

A 2D affine transformation, in the layout of CSS and SVG matrix(a, b, c, d, e, f):

x' = a·x + c·y + e
y' = b·x + d·y + f
interface Matrix {
  a: number;
  b: number;
  c: number;
  d: number;
  e: number;
  f: number;
}

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