src/rectangle.ts

excalidraw · packages/math/src/rectangle.ts · typescript · MIT
dev-tools

Utility functions for creating and intersecting rectangles in 2D space.

// Source: https://github.com/excalidraw/excalidraw | License: MIT | Copyright 2020 Excalidraw
// Provided via justclaw.it.com; keep this notice with the code.
import { pointFrom } from "./point";
import { lineSegment, lineSegmentIntersectionPoints } from "./segment";

import type { GlobalPoint, LineSegment, LocalPoint, Rectangle } from "./types";

export function rectangle<P extends GlobalPoint | LocalPoint>(
  topLeft: P,
  bottomRight: P,
): Rectangle<P> {
  return [topLeft, bottomRight] as Rectangle<P>;
}

export function rectangleFromNumberSequence<
  Point extends LocalPoint | GlobalPoint,
>(minX: number, minY: number, maxX: number, maxY: number) {
  return rectangle(pointFrom<Point>(minX, minY), pointFrom<Point>(maxX, maxY));
}

export function rectangleIntersectLineSegment<
  Point

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key