Class Rect2

The Rect2 implements a 2D rectangle with an origin in top left and a width and height. The class follows the 'copy-on-write' pattern, which means that every operation such as Rect2.inset or Rect2.outset returns a copy of the rect with updates values. It also implies that the values of the rect cannot be directly modified.

The concept of immutability improves sharing of objects as all types in TypeScript are assigned by identity, instead of by-value (e.g. a copy) as in other programming languages like C.

Constructors

  • Parameters

    • Optionalx: number
    • Optionaly: number
    • Optionalwidth: number
    • Optionalheight: number

    Returns Rect2

Properties

_height: number
_width: number
_x: number
_y: number

Accessors

  • get bottom(): number
  • Gets the Y position of the bottom edge.

    Returns number

  • get height(): number
  • Returns number

  • set height(value): void
  • Parameters

    • value: number

    Returns void

  • get left(): number
  • Gets the X position of the left edge.

    Returns number

  • get right(): number
  • Gets the X position of the right edge.

    Returns number

  • get top(): number
  • Gets the Y position of the top edge.

    Returns number

  • get width(): number
  • Returns number

  • set width(value): void
  • Parameters

    • value: number

    Returns void

  • get x(): number
  • Returns number

  • set x(value): void
  • Parameters

    • value: number

    Returns void

  • get y(): number
  • Returns number

  • set y(value): void
  • Parameters

    • value: number

    Returns void

Methods

  • Copies all properties defined by the prototype of Rect2 from other to this instance.

    Parameters

    Returns Rect2

  • Clips the point to the rectangle.

    Parameters

    Returns void

  • Clones this instance and returns a new Rect2 with identical values.

    Returns Rect2

  • Returns true if the rectangle contains the provided point.

    Parameters

    Returns boolean

  • Clips current rectangle so that it does not overlap the provided rectangle. After clipping no area of this rectangle will intersect the clip area.

    Parameters

    Returns Rect2

  • Extends this rectangle so that the provided rectangle is completely contained within it.

    Parameters

    Returns Rect2

  • Insets this rectangle by the given amount.

    Parameters

    • inset: number

      The amount to inset the rectangle by.

    Returns Rect2

    This rectangle modified by the given inset.

  • Insets this rectangle by the given amount.

    Parameters

    • outset: number

    Returns Rect2

    This rectangle modified by the given inset.

  • Rounds the vector to the nearest multiple.

    Parameters

    • multiple: number

      Multiple to round to.

    Returns Rect2

    This instance.

    This is a helpful utility to quickly snap an rectangle to a specic grid size.

  • Returns a copy of this rectangle extended by the provided rectangle, so that the given rectangle is completely contained within this.

    Parameters

    Returns Rect2

  • Computes the absolute area of the rectangle.

    Returns number

  • Computes the signed area of the rectangle.

    Returns number

  • Insets a rectangle by the given amount.

    Parameters

    • inset: number

      The amount to inset the rectangle by.

    Returns Rect2

    Returns a new rectangle with the insets applied.

  • Performs a segment/rectangle intersection using the Liang-Barsky line clipping algorithm.

    Parameters

    Returns Vector2

    Returns the intersection point or null if no intersection was found.

  • Returns whether the provided rect, given a error margin, has the same rect components values as this one.

    Parameters

    • rhs: Immutable<Rect2>

      The other rect to compare with.

    • Optionalepsilon: number

      Optional. Error margin within which the numbers should be considered equal.

    Returns boolean

    True if equal, false otherwise.

  • Determines if both rectangles are element-wise equal.

    Parameters

    Returns boolean

  • Outsets a rectangle by the given amount.

    Parameters

    • outset: number

      The amount to outset the rectangle by.

    Returns Rect2

    Returns a new rectangle with the outsets applied.

  • Returns true if the rectangle overlaps the provided rectangle. Also returns true if the rectangles are contained within each other completely (no intersecting edges).

    Parameters

    Returns boolean

  • Returns a copy of the rectangle rounded to the nearest multiple.

    Parameters

    • multiple: number

      Multiple to round to.

    Returns Rect2

    This instance.

    This is a helpful utility to quickly snap an rectangle to a specic grid size.

  • Assigns the values to the rectangle.

    Parameters

    • Optionalx: number
    • Optionaly: number
    • Optionalwidth: number
    • Optionalheight: number

    Returns void

  • Returns a new Size2 that represents the size of the Rect.

    Returns Size2

  • Returns a string representation of an object.

    Returns string

  • Compares two rectangles with an error margin.

    Parameters

    • lhs: Immutable<Rect2>

      First rect to compare.

    • rhs: Immutable<Rect2>

      Second rect to compare.

    • Optionalepsilon: number

      Optional. Error margin within which the numbers should be considered equal.

    Returns boolean

    True if equal, false otherwise.

  • Determines if both rectangles are element-wise equal.

    Parameters

    Returns boolean

  • Creates a new rectangle from the given position and size.

    Parameters

    Returns Rect2