- I
- I
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}`)Members
Constructors
Parameters
Properties
Defined in
IRectangle.bottomLeftDefined in
IRectangle.bottomRightDefined in
IRectangle.centerDefined in
IRectangle.centerXDefined in
IRectangle.centerYProperty Value
Implements
ISize.heightDefined in
ISize.isEmptyDefined in
IRectangle.maxXDefined in
IRectangle.maxYDefined in
IRectangle.sizeDefined in
IRectangle.topLeftDefined in
IRectangle.topRightProperty Value
Implements
ISize.widthMethods
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
trueiff the point lies inside the rectangle.
Defined in
IRectangle.containsDetermines 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
trueif the given rectangle contains the provided rectangle,falseotherwise.
Defined in
IRectangle.containsRectangleReturns the Euclidean distance between this rectangle and a given point.
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.
Parameters
- rect: Rect
- The rectangle to calculate the distance to.
Return Value
- number
- The Euclidean distance between this rectangle and the given rectangle.
Parameters
- other: any
- Another object to compare to.
Return Value
- boolean
trueifotherand this instance are the same type and represent the same value; otherwise,false.
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
nullotherwise.
Parameters
- insets: Insets
- The insets used to enlarge this instance
Return Value
- Rect
- An enlarged rectangle.
See Also
API
- getReduced
Parameters
- insets: Insets
- The insets used to reduce this instance.
Return Value
- Rect
- A reduced rectangle.
See Also
API
- getEnlarged
Determines whether the bounds of this rectangle intersect with the bounds of the specified rectangle.
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.
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
- 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 the polygonal line defined by the given points.
Parameters
- points: IEnumerable<IPoint>
- The list of points that is interpreted as a number of line segments.
Return Value
- boolean
trueif this rectangle intersects at least one segment of the line.
Creates a MutableRectangle using the values from this instance.
Return Value
- MutableRectangle
- An instance that has been initialized from the values of this instance.