Represents an axis-aligned rectangle in two-dimensional Cartesian coordinates with double precision.
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: containsWithEps, intersects, intersectsLine, intersectsPolyline, 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 Object 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
- view-component
- yfiles-umd modules
- All view modules
- Legacy UMD name
- yfiles.geometry.Rect
Constructors
Initializes a new instance of the Rect class with the initial values obtained from the given rectangle.
Parameters
A map of options to pass to the method.
- rectangle - IRectangle
- The rectangle to retrieve the initial values from.
Initializes a new instance using two point to define the bounds
Properties
Gets the coordinates of the bottom left corner of the rectangle.
Overrides
Gets the coordinates of the bottom right corner of the rectangle.
Overrides
Gets the height of the rectangle.
Implements
Gets the coordinates of the right border for this instance.
Overrides
Gets the coordinates of the lower border for this instance.
Overrides
Gets the size of this instance.
Gets the coordinates of the top left corner of the rectangle.
Overrides
Gets the coordinates of the top right corner of the rectangle.
Overrides
Gets the width of the rectangle.
Implements
Gets the X coordinate of the upper left corner of the rectangle.
Implements
Gets the Y coordinate of the upper left corner of the rectangle.
Implements
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.
Returns
- ↪boolean
true
iff the point lies inside the rectangle.
Defined in
Determines whether this rectangle contains the specified point with respect to a given epsilon.
Parameters
A map of options to pass to the method.
- point - Point
- The point to test.
- eps - number
- The positive epsilon distance that the point may lie outside the rectangle and still be considered contained.
Returns
- ↪boolean
Calculates the Euclidean distance between two points.
Parameters
A map of options to pass to the method.
- point2 - IPoint
- The second point.
Returns
- ↪number
- The distance between the two points.
Overrides
Indicates whether this instance and a specified object are equal.
Parameters
A map of options to pass to the method.
- other - Object
- Another object to compare to.
Returns
- ↪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.
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, the outer point lies outside the rectangle and thus an intersection point has been found, or
null
otherwise.
Creates a reduced instance that is the same as this one but reduced by the specified insets.
Parameters
A map of options to pass to the method.
- insets - Insets
- The insets to use to subtract from the instance.
See Also
Applies the given transformation matrix to the four corner points and returns the new bounding box that includes all of them.
Parameters
A map of options to pass to the method.
- transform - Matrix
- The transform matrix to apply.
Returns
See Also
Determines whether the bounds of this rectangle intersect with the bounds of the specified rectangle.
Determines whether this rectangle intersects an IOrientedRectangle, given an epsilon.
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 it's 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 a rectangle intersects a polygonal line.
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 the 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.
Creates a Rectangle2D from a given Rect.
Returns
- ↪Rectangle2D
- The Rectangle2D.
Class.ensure(LayoutExecutor)
More information.Creates a YRectangle from a given Rect.
Returns
- ↪YRectangle
- The YRectangle.
Class.ensure(LayoutExecutor)
More information.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.
Performs an explicit conversion from MutableRectangle to Rect.
Parameters
A map of options to pass to the method.
- rectangle - MutableRectangle
- The rectangle to convert.
Returns
- ↪Rect
- The result of the conversion.
Performs an explicit conversion from Rect to MutableRectangle.
Parameters
A map of options to pass to the method.
- rectangle - Rect
- The rectangle to convert.
Returns
- ↪MutableRectangle
- The result of the conversion.
Creates a Rect instance from the given rectangle-like object by performing automatic type conversion.