module HGamer3D.Data.Geometry2D
(
Point (..),
Rectangle (..),
ctScreenRect,
rectFromPoints,
pointsFromRect
) where
import Data.MessagePack
import Fresco
data Num a => Point a = Point {
ptX :: a,
ptY :: a
} deriving (Eq, Show)
instance ComponentClass (Point Float) where
toObj (Point a b) = ObjectArray [ObjectFloat a, ObjectFloat b]
fromObj (ObjectArray [ObjectFloat a, ObjectFloat b]) = (Point a b)
instance ComponentClass (Point Int) where
toObj (Point a b) = ObjectArray [ObjectInt (fromIntegral a), ObjectInt (fromIntegral b)]
fromObj (ObjectArray [ObjectInt a, ObjectInt b]) = (Point (fromIntegral a) (fromIntegral b))
data Num a => Rectangle a = Rectangle {
xpos :: a,
ypos :: a,
width :: a,
height :: a } deriving (Eq, Show)
ctScreenRect :: ComponentType (Rectangle Int)
ctScreenRect = ComponentType 0x16877957e32da6b1
instance ComponentClass (Rectangle Float) where
toObj (Rectangle x y w h) = ObjectArray [ObjectFloat x, ObjectFloat y, ObjectFloat w, ObjectFloat h]
fromObj (ObjectArray [ObjectFloat x, ObjectFloat y, ObjectFloat w, ObjectFloat h]) = (Rectangle x y w h)
instance ComponentClass (Rectangle Int) where
toObj (Rectangle x y w h) = ObjectArray [ObjectInt (fromIntegral x), ObjectInt (fromIntegral y), ObjectInt (fromIntegral w), ObjectInt (fromIntegral h)]
fromObj (ObjectArray [ObjectInt x, ObjectInt y, ObjectInt w, ObjectInt h]) = Rectangle (fromIntegral x) (fromIntegral y) (fromIntegral w) (fromIntegral h)
rectFromPoints :: Num a => Point a -> Point a -> Rectangle a
rectFromPoints upperLeft lowerRight = Rectangle rx ry rw rh where
rx = ptX upperLeft
ry = ptY upperLeft
rw = (ptY lowerRight) rx
rh = (ptY lowerRight) ry
pointsFromRect :: Num a => Rectangle a -> (Point a, Point a)
pointsFromRect rect = (ul, lr) where
rx = xpos rect
ry = ypos rect
rx' = rx + (width rect)
ry' = ry + (height rect)
ul = Point rx ry
lr = Point rx' ry'