Rectangle

Rectangle class provides static functions to create and operate on rectangles. A rectangle is usually represented as a Group of 2 Pts, marking the top-left and bottom-right corners of the rectangle. You can use the static functions as-is, or apply the Group.op or Pt.op to enable functional programming. See Op guide for details.

Class in src/Op.ts

Methods

STATIC boundingBox ( rects )

Given an array of rectangles, get a rectangle that bounds all of them.

parameters
rects: Iterable

an array of (Groups or Iterables) that represents a set of rectangles

returns
Group

the bounding rectangle as a Group

source

STATIC center ( pts )

Get the center of this rectangle.

parameters
pts: PtIterable

returns
Pt

source

STATIC corners ( rect )

Get the 4 corners of this rectangle as a Group.

parameters
rect: PtIterable

a Group or an Iterable with 2 Pt representing a Rectangle

returns
Group

source

STATIC from ( topLeft, widthOrSize, height )

Create a rectangle from top-left anchor point. Same as Rectangle.fromTopLeft.

parameters
topLeft: PtLike

top-left point

widthOrSize: number | PtLike

width as a number, or a Pt that defines its size

height: number

optional height as a number

returns
Group

a Group of 2 Pts representing a rectangle

source

STATIC fromCenter ( center, widthOrSize, height )

Create a rectangle given a center position and a size.

parameters
center: PtLike

widthOrSize: number | PtLike

width as a number, or a Pt that defines its size

height: number

optional height as a number

returns
Group

a Group of 2 Pts representing a rectangle

source

STATIC fromTopLeft ( topLeft, widthOrSize, height )

Create a rectangle given a top-left position and a size.

parameters
topLeft: PtLike

top-left point

widthOrSize: number | PtLike

width as a number, or a Pt that defines its size

height: number

optional height as a number

returns
Group

a Group of 2 Pts representing a rectangle

source

STATIC halves ( rect, ratio, asRows )

Subdivde a rectangle into 2 rectangles, by row or by column.

parameters
rect: PtIterable

a Group or an Iterable with 2 Pt representing a Rectangle

ratio: number (default value: 0.5)

a value between 0 to 1 to indicate the split ratio

asRows: boolean (default value: false)

if true, split into 2 rows. Default is false which splits into 2 columns.

returns
Group[]

an array of 2 Groups of rectangles

source

STATIC hasIntersectRect2D ( rect1, rect2, resetBoundingBox )

Check if a rectangle is within the bounds of another rectangle.

parameters
rect1: GroupLike

a Group of 2 Pts representing a rectangle

rect2: GroupLike

a Group of 2 Pts representing a rectangle

resetBoundingBox: boolean (default value: false)

if true, reset the bounding box. Default is false which assumes the rect's first Pt at is its top-left corner.

returns
boolean

source

STATIC intersectRect2D ( rect1, rect2 )

An easy way to get rectangle-rectangle intersection points. For more optimized implementation, store the rectangle's sides separately (eg, Rectangle.sides()) and use Polygon.intersectPolygon2D().

parameters
rect1: GroupLike

a Group of 2 Pts representing a rectangle

rect2: GroupLike

a Group of 2 Pts representing a rectangle

returns
Group

source

STATIC polygon ( rect )

Convert this rectangle into a Group representing a polygon. An alias for Rectangle.corners

parameters
rect: PtIterable

a Group or an Iterable with 2 Pt representing a Rectangle

returns
Group

source

STATIC quadrants ( rect, center )

Subdivide a rectangle into 4 rectangles, one for each quadrant.

parameters
rect: PtIterable

a Group or an Iterable with 2 Pt representing a Rectangle

center: PtLike

returns
Group[]

an array of 4 Groups of rectangles

source

STATIC sides ( rect )

Get the 4 sides of this rectangle as an array of 4 Groups.

parameters
rect: PtIterable

a Group or an Iterable with 2 Pt representing a Rectangle

returns
Group[]

an array of 4 Groups, each of which represents a line segment

source

STATIC size ( pts )

Get the size of this rectangle as a Pt.

parameters
pts: PtIterable

returns
Pt

source

STATIC toCircle ( pts, within )

Create a new circle that either fits within or encloses the rectangle. Same as Circle.fromRect.

parameters
pts: PtIterable

a Group or an Iterable with 2 Pt representing a rectangle

within: boolean (default value: true)

if true, the circle will be within the rectangle. If false, the circle will enclose the rectangle.

returns
Group

a Group that represents a circle

source

STATIC toSquare ( pts, enclose )

Create a square that either fits within or encloses a rectangle.

parameters
pts: PtIterable

a Group or an Iterable with 2 Pt representing a rectangle

enclose: boolean (default value: false)

if true, the square will enclose the rectangle. Default is false, which will fit the square inside the rectangle.

returns
Group

a Group of 2 Pts representing a rectangle

source

STATIC withinBound ( rect, pt )

Check if a point is within a rectangle.

parameters
rect: GroupLike

a Group of 2 Pts representing a Rectangle

pt: PtLike

the point to check

returns
boolean

source