Represents a rectangle in 2D space, parallel to the XY axes.

Example:

import { Rect2, Vec2 } from '@js-draw/math';

const rect = Rect2.fromCorners(
  Vec2.of(0, 0),
  Vec2.of(10, 10),
);
console.log('area', rect.area);
console.log('topLeft', rect.topLeft);

invariant: w ≥ 0, h ≥ 0, immutable

Hierarchy (View Summary)

Constructors

Properties

area: number
h: number

Height

size: Vec3
topLeft: Vec3

Derived state: topLeft assumes up is -y

w: number

Width

x: number

Top left x coordinate

y: number

Top left y coordinate

empty: Rect2 = ...
unitSquare: Rect2 = ...

Accessors

Methods

  • Returns true if and only if the given point is contained within this shape.

    epsilon is a small number used to counteract floating point error. Thus, if point is within epsilon of the inside of this shape, containsPoint may also return true.

    The default implementation relies on signedDistance. Subclasses may override this method to provide a more efficient implementation.

    Parameters

    Returns boolean

  • Parameters

    Returns boolean

    true iff other is completely within this Rect2.

  • Returns a the subdivision of this into [columns] columns and [rows] rows. For example, Rect2.unitSquare.divideIntoGrid(2, 2) -> [ Rect2(0, 0, 0.5, 0.5), Rect2(0.5, 0, 0.5, 0.5), Rect2(0, 0.5, 0.5, 0.5), Rect2(0.5, 0.5, 0.5, 0.5) ] The rectangles are ordered in row-major order.

    Parameters

    • columns: number
    • rows: number

    Returns Rect2[]

  • Parameters

    • other: Rect2
    • tolerance: number = 0

    Returns boolean

    true iff this is equal to other ± tolerance

  • Returns a bounding box that precisely fits the content of this shape.

    Note: This bounding box should aligned with the x/y axes. (Thus, it may be possible to find a tighter bounding box not axes-aligned).

    Returns Rect2

  • Returns this grown by [margin] in both the x and y directions.

    Parameters

    • margin: number

    Returns Rect2

  • Returns a rectangle containing this and [point]. [margin] is the minimum distance between the new point and the edge of the resultant rectangle.

    Parameters

    • point: Vec3
    • margin: number = 0

    Returns Rect2

  • If this rectangle is smaller than minSize, returns a copy of this with a larger width/height.

    If smaller than minSize, padding is applied on both sides.

    Parameters

    Returns Rect2

  • Returns true iff all points in this rectangle are within distance from point:

    If RR is the set of points in this rectangle, returns true iff aR,pointa<radius \forall {\bf a} \in R, |\texttt{point} - {\bf a}| < \texttt{radius}

    Parameters

    • radius: number
    • point: Vec3

    Returns boolean

  • Returns a string representation of an object.

    Returns string

  • [affineTransform] is a transformation matrix that both scales and translates. the bounding box of this' four corners after transformed by the given affine transformation.

    Parameters

    Returns Rect2

  • Returns a box that contains all points in [points] with at least [margin] between each point and the edge of the box.

    Parameters

    • points: Vec3[]
    • margin: number = 0

    Returns Rect2

  • Parameters

    Returns Rect2

    a rectangle that contains all of the given rectangles, the bounding box of the given rectangles.

OpenSource licenses