A BoundingBox2d is a rectangular box in 2D defined by its minimum and maximum
X and Y values. It is possible to generate bounding boxes for most geometric
objects; for example, Triangle2d.boundingBox
takes a Triangle2d and returns a BoundingBox2d that contains that triangle.
There are several use cases where it is more efficient to deal with the bounding
box of an object than the object itself, such as:
Bounding boxes can be constructed by passing a record with minX, maxX,
minY and maxY fields to the BoundingBox2d constructor, for example
exampleBox =
BoundingBox2d
{ minX = 3
, maxX = 8
, minY = 2
, maxY = 6
}If you construct a BoundingBox2d this way, you must ensure that the given
values are properly ordered: minX <= maxX,
minY <= maxY. Alternately, you can construct bounding
boxes using functions such as Point2d.hull where the
input order does not matter.
Construct a bounding box containing all points in the given list. If the
list is empty, returns Nothing.
points =
[ Point2d ( 2, 3 )
, Point2d ( -1, 5 )
, Point2d ( 6, 4 )
]
BoundingBox2d.containing points
--> Just
--> (BoundingBox2d
--> { minX = -1
--> , maxX = 6
--> , minY = 3
--> , maxY = 5
--> }
--> )
BoundingBox2d.containing []
--> NothingIf you have exactly two points, you can use Point2d.hull
instead (which returns a BoundingBox2d instead of a Maybe BoundingBox2d).
Get the minimum and maximum X and Y values of a bounding box in a single record.
BoundingBox2d.extrema exampleBox
--> { minX = 3
--> , maxX = 8
--> , minY = 2
--> , maxY = 6
--> }Can be useful when combined with record destructuring, for example
{ minX, maxX, minY, maxY } =
BoundingBox2d.extrema exampleBox
--> minX = 3
--> maxX = 8
--> minY = 2
--> maxY = 6Get the minimum X value of a bounding box.
BoundingBox2d.minX exampleBox
--> 3Get the maximum X value of a bounding box.
BoundingBox2d.maxX exampleBox
--> 8Get the minimum Y value of a bounding box.
BoundingBox2d.minY exampleBox
--> 2Get the maximum Y value of a bounding box.
BoundingBox2d.maxY exampleBox
--> 6Get the X and Y dimensions (width and height) of a bounding box.
( width, height ) =
BoundingBox2d.dimensions exampleBox
--> width = 5
--> height = 4Get the median X value of a bounding box.
BoundingBox2d.midX exampleBox
--> 5.5Get the median Y value of a bounding box.
BoundingBox2d.midY exampleBox
--> 4Get the point at the center of a bounding box.
BoundingBox2d.centroid exampleBox
--> Point2d ( 5.5, 4 )Check if a bounding box contains a particular point.
BoundingBox2d.contains (Point2d ( 4, 3 )) exampleBox
--> True
BoundingBox2d.contains Point2d.origin exampleBox
--> FalseTest if one bounding box overlaps (touches) another.
firstBox =
BoundingBox2d
{ minX = 0
, maxX = 3
, minY = 0
, maxY = 2
}
secondBox =
BoundingBox2d
{ minX = 0
, maxX = 3
, minY = 1
, maxY = 4
}
thirdBox =
BoundingBox2d
{ minX = 0
, maxX = 3
, minY = 4
, maxY = 5
}
BoundingBox2d.overlaps firstBox secondBox
--> True
BoundingBox2d.overlaps firstBox thirdBox
--> FalseTest if the second given bounding box is fully contained within the first (is a subset of it).
outerBox =
BoundingBox2d
{ minX = 0
, maxX = 10
, minY = 0
, maxY = 10
}
innerBox =
BoundingBox2d
{ minX = 1
, maxX = 5
, minY = 3
, maxY = 9
}
overlappingBox =
BoundingBox2d
{ minX = 1
, maxX = 5
, minY = 3
, maxY = 12
}
BoundingBox2d.isContainedIn outerBox innerBox
--> True
BoundingBox2d.isContainedIn outerBox overlappingBox
--> FalseBuild a bounding box that contains both given bounding boxes.
firstBox =
BoundingBox2d
{ minX = 1
, maxX = 4
, minY = 2
, maxY = 3
}
secondBox =
BoundingBox2d
{ minX = -2
, maxX = 2
, minY = 4
, maxY = 5
}
BoundingBox2d.hull firstBox secondBox
--> BoundingBox2d
--> { minX = -2
--> , maxX = 4
--> , minY = 2
--> , maxY = 5
--> }Attempt to build a bounding box that contains all points common to both
given bounding boxes. If the given boxes do not overlap, returns Nothing.
firstBox =
BoundingBox2d
{ minX = 1
, maxX = 4
, minY = 2
, maxY = 3
}
secondBox =
BoundingBox2d
{ minX = 2
, maxX = 5
, minY = 1
, maxY = 4
}
thirdBox =
BoundingBox2d
{ minX = 1
, maxX = 4
, minY = 4
, maxY = 5
}
BoundingBox2d.intersection firstBox secondBox
--> Just
--> (BoundingBox2d
--> { minX = 2
--> , maxX = 4
--> , minY = 2
--> , maxY = 3
--> }
--> )
BoundingBox2d.intersection firstBox thirdBox
--> Nothing
Construct a zero-width bounding box containing a single point.