module Data.Geometry.Triangle where
import Data.Bifunctor
import Control.Lens
import Data.Ext
import Data.Geometry.Point
import Data.Geometry.Ball
import Data.Geometry.Properties
import Data.Geometry.Transformation
data Triangle p r = Triangle (Point 2 r :+ p)
(Point 2 r :+ p)
(Point 2 r :+ p)
deriving (Show,Eq)
instance Functor (Triangle p) where
fmap f (Triangle p q r) = let f' = first (fmap f) in Triangle (f' p) (f' q) (f' r)
type instance NumType (Triangle p r) = r
type instance Dimension (Triangle p r) = 2
instance PointFunctor (Triangle p) where
pmap f (Triangle p q r) = Triangle (p&core %~ f) (q&core %~ f) (r&core %~ f)
instance Num r => IsTransformable (Triangle d r) where
transformBy = transformPointFunctor
area :: Fractional r => Triangle p r -> r
area t = doubleArea t / 2
doubleArea :: Num r => Triangle p r -> r
doubleArea (Triangle a b c) = abs $ ax*by ax*cy
+ bx*cy bx*ay
+ cx*ay cx*by
where
Point2 ax ay = a^.core
Point2 bx by = b^.core
Point2 cx cy = c^.core
inscribedDisk :: (Eq r, Fractional r) => Triangle p r -> Maybe (Disk () r)
inscribedDisk (Triangle p q r) = disk (p^.core) (q^.core) (r^.core)