Skip to content

Latest commit

 

History

History
443 lines (307 loc) · 11.7 KB

File metadata and controls

443 lines (307 loc) · 11.7 KB

API Documentation

Table of Contents


RectOps

Basic rectangle operations including creation, validation, and conversion.

ceiling(size: Size): Size

Rounds up size dimensions to integers.

const size = { width: 10.3, height: 20.7 };
const rounded = RectOps.ceiling(size);
// { width: 11, height: 21 }

validate(rect: Rectangle): Rectangle

Validates and normalizes a rectangle ensuring positive dimensions.

const invalid = { x: 100, y: 100, width: -50, height: -30 };
const valid = RectOps.validate(invalid);
// Ensures width and height are positive

create(x1: number, y1: number, x2: number, y2: number): Rectangle

Creates a rectangle from two coordinate pairs.

const rect = RectOps.create(10, 20, 110, 70);
// { x: 10, y: 20, width: 100, height: 50 }

fromPoints(pt1: Point, pt2: Point): Rectangle

Creates a rectangle from two points.

const rect = RectOps.fromPoints({ x: 10, y: 20 }, { x: 110, y: 70 });
// { x: 10, y: 20, width: 100, height: 50 }

inflateToInteger(rect: Rectangle): Rectangle

Inflates a float rectangle to integer bounds (floor left/top, ceil right/bottom).

const rect = { x: 10.3, y: 20.7, width: 100.2, height: 50.8 };
const inflated = RectOps.inflateToInteger(rect);
// Floors x/y and ceils right/bottom

snap(rect: Rectangle, dpi: number, snapDpi: number): Rectangle

Snaps rectangle to pixel grid based on DPI conversion.

const rect = { x: 10.5, y: 20.5, width: 100, height: 50 };
const snapped = RectOps.snap(rect, 96, 72);
// Snaps to pixel boundaries at target DPI

RectCompare

Rectangle comparison and containment operations.

equals(rect1: Rectangle, rect2: Rectangle, epsilon?: number): boolean

Checks if two rectangles are equal within epsilon tolerance.

const rect1 = { x: 10, y: 20, width: 100, height: 50 };
const rect2 = { x: 10.0001, y: 20, width: 100, height: 50 };
const equal = RectCompare.equals(rect1, rect2, 0.001);
// true

isEmptySize(rect: Rectangle, epsilon?: number): boolean

Checks if rectangle has zero size.

const rect = { x: 10, y: 20, width: 0, height: 0 };
const empty = RectCompare.isEmptySize(rect);
// true

isEmpty(rect: Rectangle, epsilon?: number): boolean

Checks if rectangle is completely empty (zero position and size).

const rect = { x: 0, y: 0, width: 0, height: 0 };
const empty = RectCompare.isEmpty(rect);
// true

intersects(rect1: Rectangle, rect2: Rectangle, epsilon?: number): boolean

Checks if two rectangles intersect.

const rect1 = { x: 0, y: 0, width: 100, height: 100 };
const rect2 = { x: 50, y: 50, width: 100, height: 100 };
const intersects = RectCompare.intersects(rect1, rect2);
// true

contains(rect1: Rectangle, rect2: Rectangle, epsilon?: number): boolean

Checks if rect1 completely contains rect2.

const outer = { x: 0, y: 0, width: 100, height: 100 };
const inner = { x: 10, y: 10, width: 50, height: 50 };
const contains = RectCompare.contains(outer, inner);
// true

containsPoint(rect: Rectangle, pt: Point, digits?: number): boolean

Checks if rectangle contains a point.

const rect = { x: 0, y: 0, width: 100, height: 100 };
const contains = RectCompare.containsPoint(rect, { x: 50, y: 50 });
// true

containsX(rect: Rectangle, x: number): boolean

Checks if rectangle contains X coordinate.

const rect = { x: 0, y: 0, width: 100, height: 100 };
const contains = RectCompare.containsX(rect, 50);
// true

containsY(rect: Rectangle, y: number): boolean

Checks if rectangle contains Y coordinate.

const rect = { x: 0, y: 0, width: 100, height: 100 };
const contains = RectCompare.containsY(rect, 50);
// true

intersect(first: Rectangle, second: Rectangle): Rectangle | null

Computes intersection of two rectangles.

const rect1 = { x: 0, y: 0, width: 100, height: 100 };
const rect2 = { x: 50, y: 50, width: 100, height: 100 };
const intersection = RectCompare.intersect(rect1, rect2);
// { x: 50, y: 50, width: 50, height: 50 }

intersectsExcludeBounds(first: Rectangle, second: Rectangle): boolean

Checks if rectangles intersect with non-zero area.

const rect1 = { x: 0, y: 0, width: 100, height: 100 };
const rect2 = { x: 100, y: 0, width: 100, height: 100 };
const intersects = RectCompare.intersectsExcludeBounds(rect1, rect2);
// false (only touching at boundary)

unionNonEmpty(first: Rectangle, second: Rectangle): Rectangle

Unions two rectangles, ignoring empty ones.

const rect1 = { x: 0, y: 0, width: 50, height: 50 };
const rect2 = { x: 100, y: 100, width: 50, height: 50 };
const union = RectCompare.unionNonEmpty(rect1, rect2);
// { x: 0, y: 0, width: 150, height: 150 }

isEqualArray(arr1: Rectangle[], arr2: Rectangle[]): boolean

Checks if two rectangle arrays are equal.

const arr1 = [{ x: 0, y: 0, width: 50, height: 50 }];
const arr2 = [{ x: 0, y: 0, width: 50, height: 50 }];
const equal = RectCompare.isEqualArray(arr1, arr2);
// true

RectTransform

Rectangle transformation operations including inflate, deflate, offset, and alignment.

inflate(rect: Rectangle, left?: number, top?: number, right?: number, bottom?: number): Rectangle

Expands rectangle by specified amounts on each side.

const rect = { x: 10, y: 10, width: 80, height: 80 };
const inflated = RectTransform.inflate(rect, 5, 5, 5, 5);
// { x: 5, y: 5, width: 90, height: 90 }

inflateWithMargins(rect: Rectangle, margins: Margins): Rectangle

Expands rectangle using margins.

const rect = { x: 10, y: 10, width: 80, height: 80 };
const margins = { left: 5, top: 5, right: 5, bottom: 5 };
const inflated = RectTransform.inflateWithMargins(rect, margins);

deflate(rect: Rectangle, left?: number, top?: number, right?: number, bottom?: number): Rectangle

Shrinks rectangle by specified amounts on each side.

const rect = { x: 10, y: 10, width: 100, height: 100 };
const deflated = RectTransform.deflate(rect, 5, 5, 5, 5);
// { x: 15, y: 15, width: 90, height: 90 }

deflateWithMargins(rect: Rectangle, margins: Margins): Rectangle

Shrinks rectangle using margins.

const rect = { x: 10, y: 10, width: 100, height: 100 };
const margins = { left: 5, top: 5, right: 5, bottom: 5 };
const deflated = RectTransform.deflateWithMargins(rect, margins);

offset(rect: Rectangle, x: number, y: number): Rectangle

Moves rectangle by specified offset.

const rect = { x: 10, y: 10, width: 100, height: 100 };
const moved = RectTransform.offset(rect, 20, 30);
// { x: 30, y: 40, width: 100, height: 100 }

offsetByPoint(rect: Rectangle, pt: Point): Rectangle

Moves rectangle by point offset.

const rect = { x: 10, y: 10, width: 100, height: 100 };
const moved = RectTransform.offsetByPoint(rect, { x: 20, y: 30 });

align(rect: Rectangle, baseRect: Rectangle, alignment: ContentAlignment): Rectangle

Aligns rectangle within base rectangle according to alignment.

const rect = { x: 0, y: 0, width: 50, height: 50 };
const base = { x: 0, y: 0, width: 200, height: 200 };
const aligned = RectTransform.align(rect, base, ContentAlignment.MiddleCenter);
// { x: 75, y: 75, width: 50, height: 50 }

calcCropping(bounds: Rectangle, rect: Rectangle): Padding

Calculates cropping/padding needed to fit bounds within rect.

const bounds = { x: 10, y: 10, width: 80, height: 80 };
const rect = { x: 0, y: 0, width: 100, height: 100 };
const padding = RectTransform.calcCropping(bounds, rect);
// { left: 10, top: 10, right: 10, bottom: 10 }

RectGeometry

Rectangle geometry operations for calculating center and corner points.

center(rect: Rectangle): Point

Calculates center point of rectangle.

const rect = { x: 0, y: 0, width: 100, height: 100 };
const center = RectGeometry.center(rect);
// { x: 50, y: 50 }

topLeft(rect: Rectangle): Point

Gets top-left corner point.

const rect = { x: 10, y: 20, width: 100, height: 50 };
const corner = RectGeometry.topLeft(rect);
// { x: 10, y: 20 }

topRight(rect: Rectangle): Point

Gets top-right corner point.

const rect = { x: 10, y: 20, width: 100, height: 50 };
const corner = RectGeometry.topRight(rect);
// { x: 110, y: 20 }

bottomLeft(rect: Rectangle): Point

Gets bottom-left corner point.

const rect = { x: 10, y: 20, width: 100, height: 50 };
const corner = RectGeometry.bottomLeft(rect);
// { x: 10, y: 70 }

bottomRight(rect: Rectangle): Point

Gets bottom-right corner point.

const rect = { x: 10, y: 20, width: 100, height: 50 };
const corner = RectGeometry.bottomRight(rect);
// { x: 110, y: 70 }

RectBorder

Rectangle border and side operations for extracting, cutting, and expanding.

Side Extraction

getTopSide(rect: Rectangle, size: number): Rectangle

getBottomSide(rect: Rectangle, size: number): Rectangle

getLeftSide(rect: Rectangle, size: number): Rectangle

getRightSide(rect: Rectangle, size: number): Rectangle

Gets side rectangle with specified size.

const rect = { x: 0, y: 0, width: 100, height: 100 };
const top = RectBorder.getTopSide(rect, 20);
// { x: 0, y: 0, width: 100, height: 20 }

Boundary Setting

setLeft(rect: Rectangle, value: number): Rectangle | null

setRight(rect: Rectangle, value: number): Rectangle | null

Sets boundary of rectangle. Returns null if invalid.

const rect = { x: 0, y: 0, width: 100, height: 100 };
const modified = RectBorder.setLeft(rect, 10);
// { x: 10, y: 0, width: 90, height: 100 }

Cutting Operations

cutFromTop(rect: Rectangle, height: number): Rectangle

cutFromBottom(rect: Rectangle, height: number): Rectangle

cutFromLeft(rect: Rectangle, width: number): Rectangle

cutFromRight(rect: Rectangle, width: number): Rectangle

Removes specified amount from side.

const rect = { x: 0, y: 0, width: 100, height: 100 };
const cut = RectBorder.cutFromTop(rect, 20);
// { x: 0, y: 20, width: 100, height: 80 }

Expansion Operations

expandToLeft(rect: Rectangle, value: number): Rectangle

expandToRight(rect: Rectangle, value: number): Rectangle

expandToTop(rect: Rectangle, value: number): Rectangle

expandToBottom(rect: Rectangle, value: number): Rectangle

Expands rectangle in specified direction.

const rect = { x: 10, y: 10, width: 80, height: 80 };
const expanded = RectBorder.expandToBottom(rect, 20);
// { x: 10, y: 10, width: 80, height: 100 }

RectSplit

Rectangle splitting and division operations.

horizontal(bounds: Rectangle, cellCount: number, isRightToLeft?: boolean): Rectangle[]

Splits rectangle horizontally into equal cells.

const rect = { x: 0, y: 0, width: 300, height: 100 };
const cells = RectSplit.horizontal(rect, 3);
// Returns 3 rectangles of width 100 each

const rtlCells = RectSplit.horizontal(rect, 3, true);
// Splits from right to left

vertical(bounds: Rectangle, cellCount: number): Rectangle[]

Splits rectangle vertically into equal cells.

const rect = { x: 0, y: 0, width: 100, height: 300 };
const cells = RectSplit.vertical(rect, 3);
// Returns 3 rectangles of height 100 each