Represents an axis-aligned rectangle in two-dimensional Cartesian coordinates.
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
// 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}`)
Type Details
- yFiles module
- core
Constructors
Creates a new instance from two points which define the bounds.
Properties
Gets the coordinates of the bottom left corner of the rectangle as a Point.
Defined in
Gets the coordinates of the bottom right corner of the rectangle as a Point.
Defined in
Gets the coordinates of the center of the rectangle as a Point.
Defined in
Gets the center x-coordinate of the rectangle.
Defined in
Gets the center y-coordinate of the rectangle.
Defined in
Determines whether the specified size is empty.
Remarks
Overrides
Gets the maximum x-coordinate of the rectangle.
Remarks
Defined in
Gets the maximum y-coordinate of the rectangle.
Remarks
Defined in
Gets the current size of the rectangle as Size.
Defined in
Gets the coordinates of the top left corner of the rectangle as a Point.
Defined in
Gets the coordinates of the top right corner of the rectangle as a Point.
Defined in
Gets the x-coordinate of the upper left corner of the rectangle.
Gets the y-coordinate of the upper left corner of the rectangle.
Methods
Determines whether the given rectangle contains the provided point.
Parameters
A map of options to pass to the method.
- 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.
Returns
- ↪boolean
true
iff the point lies inside the rectangle.
Defined in
Determines whether the given rectangle contains the provided rectangle.
Remarks
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
A map of options to pass to the method.
- other - IRectangle
- The rectangle to test.
- eps - number
- A positive value allows for fuzzy hit testing.
Returns
- ↪boolean
true
if the given rectangle contains the provided rectangle,false
otherwise.
Defined in
Returns the Euclidean distance between this rectangle and a given point.
Parameters
A map of options to pass to the method.
- point - Point
- The point to calculate the distance to.
Returns
- ↪number
- The Euclidean distance between this rectangle and the given point.
Returns the Euclidean distance between this rectangle and a given rectangle.
Parameters
A map of options to pass to the method.
- rect - Rect
- The rectangle to calculate the distance to.
Returns
- ↪number
- The Euclidean distance between this rectangle and the given rectangle.
Indicates whether this instance and a specified object are equal.
Parameters
A map of options to pass to the method.
- other - any
- Another object to compare to.
Returns
- ↪boolean
true
ifother
and this instance are the same type and represent the same value; otherwise,false
.
Finds the intersection between a rectangle and a line.
Remarks
Parameters
A map of options to pass to the method.
- inner - Point
- The coordinates of a point lying inside the rectangle.
- outer - Point
- The coordinates of a point lying outside the rectangle.
Returns
- ↪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.
Determines whether the bounds of this rectangle intersect with the bounds of the specified rectangle.
Parameters
A map of options to pass to the method.
- rectangle - Rect
- The rectangle to check.
Returns
- ↪boolean
- Whether both instances are non-empty and have an intersection with positive area.
Determines whether this rectangle intersects an IOrientedRectangle, given an epsilon.
Remarks
1
for this method to calculate correct results. If either one of those conditions is not met, the result of this method is undefined.Parameters
A map of options to pass to the method.
- 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.
Returns
- ↪boolean
- Whether they have a non-empty intersection.
Determines whether this rectangle intersects the polygonal line defined by the given points.
Parameters
A map of options to pass to the method.
- points - IEnumerable<IPoint>
- The list of points that is interpreted as a number of line segments.
Returns
- ↪boolean
true
if this rectangle intersects at least one segment of the line.
Creates a MutableRectangle using the values from this instance.
Returns
- ↪MutableRectangle
- An instance that has been initialized from the values of this instance.
Constants
An infinite rectangle.
Remarks
Static Methods
Returns the union of the given rectangles.
Remarks
Parameters
A map of options to pass to the method.
- firstRectangle - Rect
- The first rectangle to use for the union.
- secondRectangle - Rect
- The second rectangle to use for the union.
Returns
- ↪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.