Class: Rectangle

Rectangle

new Rectangle(xopt, yopt, widthopt, heightopt)

This object represents a rectangular area within an abstract 2-dimensional matrix.

The unit of measure is typically pixels. (If used to model computer graphics, vertical coordinates are typically measured downwards from the top of the window. This convention however is not inherent in this object.)

Normally, the x and y parameters to the constructor describe the upper left corner of the rect. However, negative values of width and height will be added to the given x and y. That is, a negative value of the width parameter will extend the rect to the left of the given x and a negative value of the height parameter will extend the rect above the given y. In any case, after instantiation the following are guaranteed to always be true:

  • The extent, width, and height properties always give positive values.
  • The origin, top, and left properties always reflect the upper left corner.
  • The corner, bottom, and right properties always reflect the lower right corner.

Note: This object should be instantiated with the new keyword.

Parameters:
Name Type Attributes Default Description
x number <optional>
0

Horizontal coordinate of some corner of the rect.

y number <optional>
0

Vertical coordinate of some corner of the rect.

width number <optional>
0

Width of the new rect. May be negative (see above).

height number <optional>
0

Height of the new rect. May be negative (see above).

Members

area :number

Area of this rect.

(Formerly a function; now a getter.)

Type:
  • number

bottom :number

Maximum vertical coordinate of this rect + 1.

(Formerly a function; now a getter.)

Type:
  • number

(abstract) center :Point

Center of this rect.

Created upon instantiation by the constructor.

Type:

(abstract) corner :Point

Lower right corner of this rect.

This is a calculated value created upon instantiation by the constructor. It is origin offset by extent.

Note: These coordinates actually point to the pixel one below and one to the right of the rect's actual lower right pixel.

Type:

(abstract) extent :Point

this rect's width and height.

Unlike the other Point properties, extent is not a global coordinate pair; rather it consists of a width (x, always positive) and a height (y, always positive).

This object might be more legitimately typed as something like Area with properties width and height; however we wanted it to be able to use it efficiently with a point's plus and minus methods (that is, without those methods having to check and branch on the type of its parameter).

Created upon instantiation by the constructor.

Type:
See:

height :number

Height of this rect (always positive).

(Formerly a function; now a getter.)

Type:
  • number

left :number

Minimum horizontal coordinate of this rect.

(Formerly a function; now a getter.)

Type:
  • number

(abstract) origin :Point

Upper left corner of this rect.

Created upon instantiation by the constructor.

Type:

Maximum horizontal coordinate of this rect + 1.

(Formerly a function; now a getter.)

Type:
  • number

top :number

Minimum vertical coordinate of this rect.

(Formerly a function; now a getter.)

Type:
  • number

width :number

Width of this rect (always positive).

(Formerly a function; now a getter.)

Type:
  • number

Methods

contains(pointOrRect) → {boolean}

Parameters:
Name Type Description
pointOrRect Point

The point or rect to test for containment.

Returns:

true iff given point entirely contained within this rect.

Type
boolean

flattenXAt(x) → {Rectangle}

Parameters:
Name Type Description
x number

Horizontal coordinate of the new rect.

Returns:

A copy of this rect but with horizontal position reset to given x and no width.

Type
Rectangle

flattenYAt(y) → {Rectangle}

Parameters:
Name Type Description
y number

Vertical coordinate of the new rect.

Returns:

A copy of this rect but with vertical position reset to given y and no height.

Type
Rectangle

forEach(iteratee, contextopt)

iterate over all points within this rect, invoking iteratee for each.

Parameters:
Name Type Attributes Default Description
iteratee function

Function to call for each point. Bound to context when given; otherwise it is bound to this rect. Each invocation of iteratee is called with two arguments: the horizontal and vertical coordinates of the point.

context object <optional>
this

Context to bind to iteratee (when not this).

growBy(padding) → {Rectangle}

(Formerly: insetBy.)

Parameters:
Name Type Description
padding number

Amount by which to increase (+) or decrease (-) this rect

See:
Returns:

That is enlarged/shrunk by given padding.

Type
Rectangle

intersect(rect, ifNoneActionopt, contextopt) → {Rectangle}

Parameters:
Name Type Attributes Default Description
rect Rectangle

The rectangle to intersect with this rect.

ifNoneAction function <optional>

When no intersection, invoke and return result. Bound to context when given; otherwise bound to this rect. Invoked with rect as sole parameter.

context object <optional>
this

Context to bind to ifNoneAction (when not this).

Returns:

One of:

  • If this rect intersects with the given rect: a new rect representing that intersection.
  • If it doesn't intersect and ifNoneAction defined: result of calling ifNoneAction.
  • If it doesn't intersect and ifNoneAction undefined: null.
Type
Rectangle

intersects(rect) → {boolean}

Parameters:
Name Type Description
rect Rectangle

The rectangle to intersect with this rect.

Returns:

true iff this rect overlaps with given rect.

Type
boolean

shrinkBy(padding) → {Rectangle}

Parameters:
Name Type Description
padding number

Amount by which to decrease (+) or increase (-) this rect.

See:
Returns:

That is enlarged/shrunk by given padding.

Type
Rectangle

union(rect) → {Rectangle}

Parameters:
Name Type Description
rect Rectangle

The rectangle to union with this rect.

Returns:

Bounding rect that contains both this rect and the given rect.

Type
Rectangle

within(rect) → {boolean}

(Formerly isContainedWithinRectangle.)

Parameters:
Name Type Description
rect Rectangle

Rectangle to test against this rect.

Returns:

true iff this rect is entirely contained within given rect.

Type
boolean