Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Rectangle

A rectangular shape aligned to a plane.

Example

import { Plane, Point, Rectangle, Vector } from 'shapetypes';

// Create a new rectangle
const rect = new Rectangle(Plane.worldXY(), 10, 20);

// Get properties of the rectangle
console.log(rect.area);
// => 200
console.log(rect.circumference);
// => 60
console.log(rect.center.toString());
// => (5,10)

// Create a new rectangle aligned to a plane
const plane = new Plane(new Point(3,4), new Vector(1,1));
const angledRect = new Rectangle(plane, 10, 20);

// Get corners of angled rectangle
console.log(angledRect.getCorners());
// => [(3,4),(-11.14213562373095,8.14213562373095),(-4.071067811865474,25.213203435596427),(10.071067811865476,11.071067811865476)]

Hierarchy

Index

Constructors

constructor

  • Creates a rectangle aligned to a plane. One corner of the rectangle will sit on the plane's origin, with the rest of the rectangle growing along the plane's axes from that point.

    Parameters

    • plane: Plane

      The plane the rectangle is aligned to. The plane's origin will be one corner of the rectangle with the rest of the rectangle growing from that point.

    • x: number

      The width of the rectangle along the plane's x-axis.

    • y: number

      The width of the rectangle along the plane's y-axis.

    Returns Rectangle

  • Creates a new rectangle on a plane.

    Parameters

    • plane: Plane

      The plane the rectangle is aligned to. The plane's origin will be one corner of the rectangle with the rest of the rectangle growing from that point.

    • x: Interval | IntervalSorted

      The width of the rectangle along the plane's x-axis.

    • y: Interval | IntervalSorted

      The width of the rectangle along the plane's y-axis.

    Returns Rectangle

Accessors

area

  • get area(): number

boundingBox

center

circumference

  • get circumference(): number

plane

  • Gets the plane the rectangle is aligned to.

    Depending on how the rectangle is positioned on this plane (using the intervals x and y), the plane's origin may or may not be a corner or center of the rectangle.

    Returns Plane

widthX

  • get widthX(): number

widthY

  • get widthY(): number

x

y

Create Methods

Static fromCenter

  • Creates a rectangle centered and aligned to a plane.

    Parameters

    • center: Plane

      The plane the rectangle is aligned to. The plane's origin will be the center of the rectangle.

    • x: number

      Width of the rectangle along the x-axis of the plane (rectangle will be centered on the plane's origin).

    • y: number

      Width of the rectangle along the y-axis of the plane (rectangle will be centered on the plane's origin).

    Returns Rectangle

Static fromCorners

  • Creates a rectangle from two corner points.

    Parameters

    • cornerA: Point

      One corner of the rectangle.

    • cornerB: Point

      The opposite corner of the rectangle.

    • Default value plane: Plane = Plane.worldXY()

      The plane the rectangle is orientated to. If unset, the rectangle will be aligned to the global x- and y-axis.

    Returns Rectangle

Other Methods

closestPoint

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

    Parameters

    • testPoint: Point

      The target to get closest to.

    • Default value includeInterior: boolean = false

      If false, the closest point must lie on the outer edge of the rectangle. If true, the closest point can also be a point on the interior of the rectangle.

    Returns Point

contains

  • Checks whether a point is inside, outside, or on the edge of a rectangle.

    Parameters

    • testPoint: Point

      The point to test for containment.

    • Default value tolerance: number = shapetypesSettings.absoluteTolerance

      The distance the point can be from the edge of the rectangle and still considered coincident.

    Returns PointContainment

corner

  • corner(minX: boolean, minY: boolean): Point
  • Gets a corner of the rectangle.

    Parameters

    • minX: boolean

      If true, point will be at the min x value relative to the rectangle's plane. 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

  • equals(otherRectangle: Rectangle, tolerance?: number): boolean
  • Checks whether another rectangle has the same dimensions and location. Returns true if it does.

    Parameters

    • otherRectangle: Rectangle

      Rectangle to compare against.

    • Default value tolerance: number = shapetypesSettings.absoluteTolerance

      The amount the dimensions can differ and still be considered equal.

    Returns boolean

getCorners

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

    Returns keyof Point[]

getEdges

  • getEdges(): keyof Line[]

pointAt

  • Remaps a point from the u-v space of the rectangle to the global coordinate system.

    Parameters

    • u: number

      The normalized distance along the x-axis of the rectangle.

    • v: number

      The normalized distance along the y-axis of the rectangle.

    Returns Point

    The uvPoint remapped to the global coordinate system.

  • Remaps a point from the u-v space of the rectangle to the global coordinate system.

    Parameters

    • uvPoint: Point

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

    Returns Point

    The uvPoint remapped to the global coordinate system.

toPolyline

toString

  • toString(): string
  • Gets the rectangle as a string in the format: [plane,widthX,widthY].

    Returns string

withPlane

  • Creates a copy of the rectangle with a different plane.

    Parameters

    • newPlane: Plane

      The new plane for the rectangle. This will change the rectangle's location and orientation.

    Returns Rectangle

withX

withY

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 rectangle by a transform matrix and returns the result.

    Example

    import { Plane, Rectangle, Transform } from 'shapetypes';
    
    // Create a new rectangle
    const rect = new Rectangle(Plane.worldXY(), 10, 20);
    console.log(rect.area);
    // => 200
    
    // Scale rectangle using a transform matrix
    const matrix = Transform.scale(2);
    const scaled = rect.transform(matrix);
    console.log(scaled.area);
    // => 800
    
    // Scale rectangle using the direct method
    const otherScaled = rect.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