Represents an axis-aligned rectangle in two-dimensional Cartesian coordinates.
ImplementsInheritance Hierarchy

Remarks

A Rect with negative width and height is considered empty and differs in interpretation from a rectangle with zero width and height (which is equivalent to a point).

Rect offers various utility methods for analyzing containment and intersections between rectangles and other geometric primitives: contains, intersects, intersectsLine, findLineIntersection.

This is a convertible type that can be used with the following notation(s) in parameter lists, parameter objects or setters.

It is possible to specify an Array or plain Object to define the Rect:

[5, 5, 20, 20] // x, y, width, height
{ x: 5, y: 5, width: 20, height: 20 }

Examples

The following example shows a few ways to use Rect:
// Create a new rectangle
const rect1 = new Rect(-5, 2, 40, 20)
// Create another rectangle from two corner points
const rect2 = new Rect(new Point(17, 23), new Point(65, 30))
// Create yet another rectangle from a center point and a size
const rect3 = new Rect(new Point(0, 0), new Size(20, 30))

// Print some general data about the first rectangle
console.log(`Top left:     ${rect1.topLeft}`)
console.log(`Top right:    ${rect1.topRight}`)
console.log(`Bottom left:  ${rect1.bottomLeft}`)
console.log(`Bottom right: ${rect1.bottomRight}`)
console.log(`Center:       ${rect1.center}`)
console.log(`Size:         ${rect1.width} x ${rect1.height}`)
console.log(`Area:         ${rect1.area}`)

// Rect instances integrate with the other geometry types in various ways:

// Enlarge a rectangle to include a given point
const p = new Point(47, 11)
const union = rect1.add(p)

// Check whether a given point is inside a rectangle
console.log(union.contains(p)) // true

// Check whether two rectangles intersect
console.log(rect1.intersects(rect2)) // true

// Enlarge or shrink a rectangle
const larger = rect2.getEnlarged(new Insets(2, 5, 2, 10))
console.log(`Original rectangle: ${rect2}`)
console.log(`Enlarged rectangle: ${larger}`)

const smaller = rect3.getReduced(5)
console.log(`Original rectangle: ${rect3}`)
console.log(`Reduced rectangle: ${smaller}`)

Members

Show:

Constructors

Creates a new instance from two points which define the bounds.
This will always result in non-empty rectangles as the coordinates of the points are sorted so that the smaller gets assigned to x and y respectively and the greater ones define the width and height.

Parameters

p1: Point
The first point to determine the bounds.
p2: Point
The second point to determine the bounds.
Creates a new instance using the given location as x and y, and the given size as width and height.

Parameters

location: Point
The top left corner.
size: Size
The size to use.
Creates a new instance for the given values.

Parameters

x: number
The x coordinate of the top left corner.
y: number
The y coordinate of the top left corner.
width: number
The width of the rectangle.
height: number
The height of the rectangle.

Properties

Gets the area of this instance which is the product of width and height.
The area of an empty rectangle is 0 (even though the product would yield another value since both width and height are negative).
readonlyfinal

Property Value

The area.
Gets the coordinates of the bottom left corner of the rectangle as a Point.
readonly
Gets the coordinates of the bottom right corner of the rectangle as a Point.
readonly
Gets the coordinates of the center of the rectangle as a Point.
readonly
Gets the center x-coordinate of the rectangle.
readonly
Gets the center y-coordinate of the rectangle.
readonly
Gets the height of the rectangle.
readonlyfinal

Property Value

The height. Negative values make the rectangle empty.

Implements

ISize.height
Determines whether the specified size is empty.
ISize instances are considered empty if their width or height is less than 0.0d.
readonly

Defined in

ISize.isEmpty
Gets a value indicating whether this instance is finite.
This means that the instance is not empty and all of x, y, width, and height are finite.
readonlyfinal

Property Value

true if this instance is finite; false otherwise.
Gets the maximum x-coordinate of the rectangle.
This is the x-coordinate of the right side of the rectangle, or the left side if the rectangle is empty.
readonly

Defined in

IRectangle.maxX
Gets the maximum y-coordinate of the rectangle.
This is the y-coordinate of the bottom side of the rectangle, or the top side if the rectangle is empty.
readonly

Defined in

IRectangle.maxY
Gets the current size of the rectangle as Size.
readonly

Defined in

IRectangle.size
Gets the coordinates of the top left corner of the rectangle as a Point.
readonly
Gets the coordinates of the top right corner of the rectangle as a Point.
readonly
Gets the width of the rectangle.
readonlyfinal

Property Value

The width. Negative values make the rectangle empty.

Implements

ISize.width
Gets the x-coordinate of the upper left corner of the rectangle.
readonlyfinal

Property Value

The x-coordinate.

Implements

IRectangle.x
Gets the y-coordinate of the upper left corner of the rectangle.
readonlyfinal

Property Value

The y-coordinate.

Implements

IRectangle.y

Methods

Returns the union of this rectangle and the given point.
final

Parameters

point: Point
The point to include in the rectangle's bounds.

Return Value

Rect
The enlarged rectangle.
Determines whether the given rectangle contains the provided point.

Parameters

point: IPoint
The point to test.
eps?: number
A positive value allows for fuzzy hit testing. If the point lies outside the given object but its distance is less than or equal to that value, it will be considered a hit.

Return Value

boolean
true iff the point lies inside the rectangle.
Determines whether the given rectangle contains the provided rectangle.

More formally, this rectangle contains the other rectangle if the coordinates of each point of the other rectangle are neither less than this rectangle's x and y minus eps nor greater than this rectangle's MaxX and MaxY plus eps.

Consequently, an empty rectangle neither contains any other rectangle nor is it contained in any other rectangle.

Note, the result of this method can be counterintuitive if this rectangle's x or y is positive or negative infinity.

Parameters

other: IRectangle
The rectangle to test.
eps?: number
A positive value allows for fuzzy hit testing.

Return Value

boolean
true if the given rectangle contains the provided rectangle, false otherwise.
Returns the Euclidean distance between this rectangle and a given point.
final

Parameters

point: Point
The point to calculate the distance to.

Return Value

number
The Euclidean distance between this rectangle and the given point.
Returns the Euclidean distance between this rectangle and a given rectangle.
final

Parameters

rect: Rect
The rectangle to calculate the distance to.

Return Value

number
The Euclidean distance between this rectangle and the given rectangle.
Indicates whether this instance and a specified object are equal.
final

Parameters

other: any
Another object to compare to.

Return Value

boolean
true if other and this instance are the same type and represent the same value; otherwise, false.
Finds the intersection between a rectangle and a line.
If one or more coordinates of the given points are infinite, the result of this method is undefined.
final

Parameters

inner: Point
The coordinates of a point lying inside the rectangle.
outer: Point
The coordinates of a point lying outside the rectangle.

Return Value

Point
The intersection point, if the inner point lies inside the rectangle and the outer point lies outside the rectangle and thus an intersection point has been found, or null otherwise.
Creates an instance enlarged by the specified insets.
final

Parameters

insets: Insets
The insets used to enlarge this instance

Return Value

Rect
An enlarged rectangle.

See Also

API
getReduced
Creates an instance reduced by the specified insets.
final

Parameters

insets: Insets
The insets used to reduce this instance.

Return Value

Rect
A reduced rectangle.

See Also

API
getEnlarged
Returns a translated instance of this instance which has modified x and y by the given amount.
final

Parameters

delta: Point
The delta to add to x and y.

Return Value

Rect
A translated instance of this instance.
Returns the hash code for this instance.
final

Return Value

number
A 32-bit signed integer that is the hash code for this instance. The hash code is computed using the x, y, width, and height properties.
Determines whether the bounds of this rectangle intersect with the bounds of the specified rectangle.
final

Parameters

rectangle: Rect
The rectangle to check.

Return Value

boolean
Whether both instances are non-empty and have an intersection with positive area.
Determines whether this rectangle intersects an IOrientedRectangle, given an epsilon.
The given oriented rectangle's anchor point has to be finite and its up vector has to have length 1 for this method to calculate correct results. If either one of those conditions is not met, the result of this method is undefined.
final

Parameters

rectangle: IOrientedRectangle
The IOrientedRectangle to test.
eps?: number
A positive value allows for fuzzy hit testing. If the point lies outside the given object but its distance is less than or equal to that value, it will be considered a hit.

Return Value

boolean
Whether they have a non-empty intersection.
Determines whether this rectangle intersects a line.
final

Parameters

start: Point
The first endpoint of the line.
end: Point
The second endpoint of the line.

Return Value

boolean
Whether the line intersects the rectangle.
Determines whether this rectangle intersects the polygonal line defined by the given points.
final

Parameters

points: IEnumerable<IPoint>
The list of points that is interpreted as a number of line segments.

Return Value

boolean
true if this rectangle intersects at least one segment of the line.
Creates a MutableRectangle using the values from this instance.
final

Return Value

MutableRectangle
An instance that has been initialized from the values of this instance.
Creates a Rect instance with the current values of this rectangle.
This method can be used to obtain a copy of the current state of the rectangle and for using the utility methods that are available for the Rect type.

Return Value

Rect
A Rect that holds the values of the rectangle at the time of the invocation.

See Also

API
toMutableRectangle
Creates a new Size instance with the values of width and height.
final

Return Value

Size
A new Size instance with the values of width and height.

Implements

ISize.toSize
Returns a that describes this instance.
final

Return Value

string
A that describes this instance.

Constants

An empty rectangle.
width and height are set to -1.0d, x and y are set to 0.0d.
static
An infinite rectangle.
static

Static Methods

Returns the union of the given rectangles.
Empty rectangles will not be considered.
static

Parameters

firstRectangle: Rect
The first rectangle to use for the union.
secondRectangle: Rect
The second rectangle to use for the union.

Return Value

Rect
A rectangle that encompasses the area of the two given rectangles.
Creates a Rect instance from the given rectangle-like object by performing automatic type conversion.
static

Parameters

rectLike: Rect
The object to convert to a Rect.

Return Value

Rect
The given rectLike if it is already a Rect, or a new instance initialized to the values found in rectLike.
Creates a new instance given the center of the rectangle and its size.
static

Parameters

center: IPoint
The center to use.
size: ISize
The size to assign.

Return Value

Rect
An instance whose center is set to center and size is size