Skip to content

Rectangle

Module Geometry.Rects

In ixfx, a rectangle has this shape:

type Rect = {
height: number
width: number
}

Rectangles can be positioned, meaning they also have an x & y, denoting the top-left corner:

type RectPositioned = Rect & {
x: number
y: number
}

Once you have data that conforms to Rect or RectPositioned, ixfx has a bunch of functions to help you work with them:

  • area - compute area
  • cardinal - a point on cardinal direction (n, ne, e, se, sw, w, nw)
  • center - center point
  • corners - corner locations
  • edges - edges of rectangle as Line instances.
  • encompass - returns a rectangle as big as source rectangle and also including provided point(s).
  • getEdgeX getEdgeY - point on edge of rectangle
  • randomPoint - random point within rectangle
  • lengths - length of each edge

Distances, intersection

Creating

  • fromCenter - create a rectangle from a center
  • fromElement - create a rectangle based on size of a HTML element
  • fromNumbers - create a rectangle from a series of numbers (opposite of toNumbers)
  • fromTopLeft - create a rectangle from its top-left corner
  • random - create a random rectangle

Math

Etc

  • isEmpty - true if rectangle has 0 for height and width. Use Rects.Empty or Rects.EmptyPositioned to get an empty rectangle.
  • isPlaceholder - true if rectangle has NaN for height and width. Use Rects.Placeholder or Rects.PlaceholderPositioned to get a placeholder rectangle.
// Sub module
import { Rects } from "@ixfx/geometry.js"
// Eg: Rects.random();
// Whole module
import * as Geometry from "@ixfx/geometry.js"
// Eg: Geometry.Rects.random();
// From bundle
import { Geometry } from "@ixfx"
// Eg: Geometry.Rects.random();
// And within your HTML's <HEAD> </HEAD> block:
<script type="importmap">{ "imports": { "@ixfx":"/ixfx/index.js", "@ixfx/": "/ixfx/" } }</script>