module Data.BoundingBox.B2 where
import Data.Vector.Class
import Data.Vector.V2
import Data.BoundingBox.Range as R
data BBox2 = BBox2 {minX, minY, maxX, maxY :: !Scalar} deriving (Eq, Show)
rangeX :: BBox2 -> Range
rangeX b = Range (minX b) (maxX b)
rangeY :: BBox2 -> Range
rangeY b = Range (minY b) (maxY b)
rangeXY :: Range -> Range -> BBox2
rangeXY (Range x0 x1) (Range y0 y1) = BBox2 x0 y0 x1 y1
bounds :: Vector2 -> Vector2 -> BBox2
bounds (Vector2 xa ya) (Vector2 xb yb) = BBox2 (min xa xb) (min ya yb) (max xa xb) (max ya yb)
points_bounds :: [Vector2] -> BBox2
points_bounds ps =
let
xs = map v2x ps
ys = map v2y ps
in BBox2 (minimum xs) (minimum ys) (maximum xs) (maximum ys)
within_bounds :: Vector2 -> BBox2 -> Bool
within_bounds (Vector2 x y) b =
x `R.within_bounds` (rangeX b) &&
y `R.within_bounds` (rangeY b)
min_bound :: BBox2 -> Vector2
min_bound (BBox2 x0 y0 x1 y1) = Vector2 x0 y0
max_bound :: BBox2 -> Vector2
max_bound (BBox2 x0 y0 x1 y1) = Vector2 x1 y1
union :: BBox2 -> BBox2 -> BBox2
union b0 b1 =
let
rx = (rangeX b0) `R.union` (rangeX b1)
ry = (rangeY b0) `R.union` (rangeY b1)
in rangeXY rx ry
isect :: BBox2 -> BBox2 -> Maybe BBox2
isect b0 b1 = do
rx <- (rangeX b0) `R.isect` (rangeX b1)
ry <- (rangeY b0) `R.isect` (rangeY b1)
return (rangeXY rx ry)