Options
All
  • Public
  • Public/Protected
  • All
Menu

Class BoundingBox

A rectangle aligned to the environment's x- and y-axis (Vector.xAxis. A bounding box is defined by two IntervalSorteds, which give the dimensions of the rectangle along the x- and y-axis.

Example

import { BoundingBox, IntervalSorted, Point } from 'shapetypes';

// Create a new bounding box
const bb = new BoundingBox(new IntervalSorted(0, 10), new IntervalSorted(5, 25));

// Get properties of the bounding box
console.log(bb.area);
// => 200
console.log(bb.center.toString());
// => '(5,15)'

// Test for containment
console.log(bb.contains(new Point(5, 15)));
// => True

// Change the bounding box's xRange
const shifted = bb.withXRange(new IntervalSorted(0, 20));
console.log(shifted.area);
// => 400

// Scale the bounding box
const scaled = bb.scale(2);
console.log(scaled.area);
// => 800

Hierarchy

Index

Constructors

constructor

Accessors

area

  • get area(): number

center

max

min

xRange

yRange

Create Methods

Static fromCorners

  • Creates a bounding box from two corner points.

    Parameters

    • cornerA: Point

      One corner of the bounding box.

    • cornerB: Point

      The opposite corner of the bounding box.

    Returns BoundingBox

Static fromPoints

  • Returns the smallest bounding box that contains all the points.

    Parameters

    • points: keyof Point[]

      The points to encapsulate in a bounding box.

    Returns BoundingBox

Static intersection

  • Finds the overlapping portion of two bounding boxes and returns it as a new bounding box.

    returns:

    A bounding box representing the overlap between these two boxes. If no overlap exists, returns undefined.

    Parameters

    Returns BoundingBox | undefined

Static union

Other Methods

closestPoint

  • closestPoint(testPoint: Point, includeInterior?: boolean): Point
  • Finds the closest point on the bounding box and returns the point.

    Parameters

    • testPoint: Point

      Target to get closest to.

    • Default value includeInterior: boolean = true

      If true, the closest point can be within the bounding box. If false, the closest point can only be on the bounding box's outer edge.

    Returns Point

contains

  • contains(testPoint: Point, strict?: boolean, tolerance?: number): boolean
  • Checks whether a point is inside the bounding box.

    Parameters

    • testPoint: Point

      Point to test for containment

    • Default value strict: boolean = false

      If true, the point needs to be entirely inside the bounding box and can't be coincident with the edge.

    • Default value tolerance: number = shapetypesSettings.absoluteTolerance

      The distance the point can be outside the box and still considered inside.

    Returns boolean

corner

  • corner(minX: boolean, minY: boolean): Point
  • Gets one of the bounding box's four corners.

    Parameters

    • minX: boolean

      If true, point will be at the min x value. If false, it will be at max.

    • minY: boolean

      If true, point will be at the min y value. If false, it will be at max.

    Returns Point

equals

getCorners

  • getCorners(): keyof Point[]
  • Gets the four corners of the bounding box. The order is: [[minX, minY], [minX, maxY], [maxX, maxY], [maxX, minY]].

    Returns keyof Point[]

getEdges

  • getEdges(): keyof Line[]

inflate

  • Evenly increases the size of the bounding box in all directions. Returns the enlarged bounding box.

    Parameters

    • amount: number

      The amount to inflate each side of the bounding box.

    Returns BoundingBox

  • Parameters

    • amountX: number
    • amountY: number

    Returns BoundingBox

overlaps

  • Checks whether the bounding box overlaps another. Returns true if it does.

    Parameters

    Returns boolean

pointAt

  • Remaps a point from the u-v space of the bounding box to the global coordinate system. This is the opposite of remapToBox.

    Parameters

    • u: number

      The normalized distance along the x-axis of the bounding box.

    • v: number

      The normalized distance along the y-axis of the bounding box.

    Returns Point

    The uvPoint remapped to the global coordinate system.

  • Remaps a point from the u-v space of the bounding box to the global coordinate system. This is the opposite of remapToBox.

    Parameters

    • uvPoint: Point

      A point in the u-v coordinates of the bounding box. The point's x value is the normalized distance along the x-axis of the bounding box (u-direction). The point's y value is the normalized distance along the y-axis of the bounding box (v-direction).

    Returns Point

    The uvPoint remapped to the global coordinate system.

remapToBox

  • Remaps a point to the u-v space of the bounding box. This is the opposite of pointAt.

    Parameters

    • point: Point

      Point to remap.

    Returns Point

    A point in the u-v coordinates of the bounding box. See pointAt for more details.

toPolyline

toString

  • toString(): string
  • Gets the bounding box as a string in the format: [xRange,yRange].

    Returns string

withXRange

withYRange

Transform Methods

changeBasis

  • changeBasis(planeFrom: Plane, planeTo: Plane): this
  • Translates the geometry from one coordinate system to another while keeping the geometry in the same position. In other words, if the geometry is currently described relative to planeFrom, after changeBasis, it will be in the same position but described relative to planeTo.

    see

    Transform.changeBasis.

    Parameters

    • planeFrom: Plane

      The coordinate system the geometry is currently described relative to.

    • planeTo: Plane

      The coordinate system to describe the geometry relative to.

    Returns this

    The geometry in the new coordinate system.

planeToPlane

  • planeToPlane(planeFrom: Plane, planeTo: Plane): this

rotate

  • rotate(angle: number): this
  • rotate(angle: number, pivot: Point): this
  • Rotates the geometry about (0,0).

    see

    Transform.rotate.

    Parameters

    • angle: number

      Angle to rotate the geometry (in radians). The direction is counter-clockwise.

    Returns this

  • Rotates the geometry about a point.

    see

    Transform.rotate.

    category

    Transform

    Parameters

    • angle: number

      Angle to rotate the geometry (in radians). If the environment's y-axis points upwards, the direction is counter-clockwise.

    • pivot: Point

      Point to pivot the geometry about.

    Returns this

scale

  • scale(amount: number): this
  • scale(x: number, y: number): this
  • scale(x: number, y: number, center: Point): this
  • Scales the geometry and returns the resized geometry. The geometry will be scaled about (0,0), meaning everything will shrink or expand away from this point.

    see

    Transform.scale.

    Parameters

    • amount: number

      Magnitude to scale in x- and y-direction. If less than 1, the object will shrink. If greater than 1, it will grow.

    Returns this

  • Scales the geometry and returns the resized geometry. The geometry will be scaled about (0,0), meaning everything will shrink or expand away from this point.

    see

    Transform.scale.

    category

    Transform

    Parameters

    • x: number

      Magnitude to scale in x-direction. If less than 1, the object will shrink. If greater than 1, it will grow.

    • y: number

      Magnitude to scale in y-direction. If less than 1, the object will shrink. If greater than 1, it will grow.

    Returns this

  • Scales the geometry about a point and returns the resized geometry.

    see

    Transform.scale.

    category

    Transform

    Parameters

    • x: number

      Magnitude to scale in x-direction. If less than 1, the object will shrink. If greater than 1, it will grow.

    • y: number

      Magnitude to scale in y-direction. If less than 1, the object will shrink. If greater than 1, it will grow.

    • center: Point

      Center of scaling. Everything will shrink or expand away from this point.

    Returns this

transform

  • Transforms the bounding box by a transform matrix and returns the result.

    Example

    import { BoundingBox, IntervalSorted, Transform } from 'shapetypes';
    
    // Create bounding box
    const bb = new BoundingBox(new IntervalSorted(0, 10), new IntervalSorted(5, 25));
    console.log(bb.area);
    // => 200
    
    // Scale using a transform matrix
    const matrix = Transform.scale(2);
    const scaled = bb.transform(matrix);
    console.log(scaled.area);
    // => 800
    
    // Scale using the direct method
    const otherScaled = bb.scale(2);
    console.log(otherScaled.area);
    // => 800
    note

    If you're applying the same transformation a lot of geometry, creating the Transform matrix once and calling this function is faster than using the direct methods.

    Parameters

    Returns this

translate

  • translate(move: Vector): this
  • translate(move: Vector, distance: number | undefined): this
  • Moves the geometry along a vector and returns the moved geometry. The translation is always linear.

    see

    Transform.translate.

    Parameters

    • move: Vector

      The direction and distance to move the geometry.

    Returns this

  • Moves the geometry along a vector and returns the moved geometry. The translation is always linear.

    see

    Transform.translate.

    category

    Transform

    Parameters

    • move: Vector

      The direction to move the geometry.

    • distance: number | undefined

      The distance to move the geometry.

    Returns this

Generated using TypeDoc