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:
Construct a bounding box from its minimum and maximum X and Y values:
exampleBox =
BoundingBox2d.with
{ minX = 3
, maxX = 8
, minY = 2
, maxY = 6
}If the minimum and maximum values are provided in the wrong order (for example if <code>minX > maxX</code>), then they will be swapped so that the resulting bounding box is valid.
Construct a zero-width bounding box containing a single point.
point =
Point2d.fromCoordinates ( 2, 3 )
BoundingBox2d.singleton point
--> BoundingBox2d.with
--> { minX = 2
--> , maxX = 2
--> , minY = 3
--> , maxY = 3
--> }Build a bounding box that contains both given bounding boxes.
firstBox =
BoundingBox2d.with
{ minX = 1
, maxX = 4
, minY = 2
, maxY = 3
}
secondBox =
BoundingBox2d.with
{ minX = -2
, maxX = 2
, minY = 4
, maxY = 5
}
BoundingBox2d.hull firstBox secondBox
--> BoundingBox2d.with
--> { 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.with
{ minX = 1
, maxX = 4
, minY = 2
, maxY = 3
}
secondBox =
BoundingBox2d.with
{ minX = 2
, maxX = 5
, minY = 1
, maxY = 4
}
thirdBox =
BoundingBox2d.with
{ minX = 1
, maxX = 4
, minY = 4
, maxY = 5
}
BoundingBox2d.intersection firstBox secondBox
--> Just
--> (BoundingBox2d.with
--> { minX = 2
--> , maxX = 4
--> , minY = 2
--> , maxY = 3
--> }
--> )
BoundingBox2d.intersection firstBox thirdBox
--> NothingConstruct a bounding box containing all bounding boxes in the given list. If
the list is empty, returns Nothing.
singletonBox =
BoundingBox2d.singleton
(Point2d.fromCoordinates ( 1, 3 ))
BoundingBox2d.hullOf [ exampleBox, singletonBox ]
--> Just
--> (BoundingBox2d.with
--> { minX = 1,
--> , maxX = 8
--> , minY = 2
--> , maxY = 6
--> }
--> )
BoundingBox2d.hullOf [ exampleBox ]
--> Just exampleBox
BoundingBox2d.hullOf []
--> NothingIf you have exactly two bounding boxes, you can use BoundingBox2d.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.fromCoordinates ( 5.5, 4 )Check if a bounding box contains a particular point.
point =
Point2d.fromCoordinates ( 4, 3 )
BoundingBox2d.contains point exampleBox
--> True
BoundingBox2d.contains Point2d.origin exampleBox
--> FalseTest if one bounding box overlaps (touches) another.
firstBox =
BoundingBox2d.with
{ minX = 0
, maxX = 3
, minY = 0
, maxY = 2
}
secondBox =
BoundingBox2d.with
{ minX = 0
, maxX = 3
, minY = 1
, maxY = 4
}
thirdBox =
BoundingBox2d.with
{ 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.with
{ minX = 0
, maxX = 10
, minY = 0
, maxY = 10
}
innerBox =
BoundingBox2d.with
{ minX = 1
, maxX = 5
, minY = 3
, maxY = 9
}
overlappingBox =
BoundingBox2d.with
{ minX = 1
, maxX = 5
, minY = 3
, maxY = 12
}
BoundingBox2d.isContainedIn outerBox innerBox
--> True
BoundingBox2d.isContainedIn outerBox overlappingBox
--> False