module Data.Geometry.Line.Internal where
import Control.DeepSeq
import Control.Lens
import qualified Data.Foldable as F
import Data.Geometry.Point
import Data.Geometry.Properties
import Data.Geometry.Vector
import Data.Ord (comparing)
import qualified Data.Traversable as T
import Data.Vinyl
import Frames.CoRec
import GHC.Generics (Generic)
data Line d r = Line { _anchorPoint :: !(Point d r)
, _direction :: !(Vector d r)
} deriving Generic
makeLenses ''Line
instance (Show r, Arity d) => Show (Line d r) where
show (Line p v) = concat [ "Line (", show p, ") (", show v, ")" ]
deriving instance (Eq r, Arity d) => Eq (Line d r)
deriving instance (NFData r, Arity d) => NFData (Line d r)
deriving instance Arity d => Functor (Line d)
deriving instance Arity d => F.Foldable (Line d)
deriving instance Arity d => T.Traversable (Line d)
type instance Dimension (Line d r) = d
type instance NumType (Line d r) = r
lineThrough :: (Num r, Arity d) => Point d r -> Point d r -> Line d r
lineThrough p q = Line p (q .-. p)
verticalLine :: Num r => r -> Line 2 r
verticalLine x = Line (point2 x 0) (v2 0 1)
horizontalLine :: Num r => r -> Line 2 r
horizontalLine y = Line (point2 0 y) (v2 1 0)
perpendicularTo :: Num r => Line 2 r -> Line 2 r
perpendicularTo (Line p ~(Vector2 vx vy)) = Line p (v2 (vy) vx)
isIdenticalTo :: (Eq r, Arity d) => Line d r -> Line d r -> Bool
(Line p u) `isIdenticalTo` (Line q v) = (p,u) == (q,v)
isParallelTo :: (Eq r, Fractional r, Arity d)
=> Line d r -> Line d r -> Bool
(Line _ u) `isParallelTo` (Line _ v) = u `isScalarMultipleOf` v
onLine :: (Eq r, Fractional r, Arity d) => Point d r -> Line d r -> Bool
p `onLine` (Line q v) = p == q || (p .-. q) `isScalarMultipleOf` v
type instance IntersectionOf (Line 2 r) (Line 2 r) = [ NoIntersection
, Point 2 r
, Line 2 r
]
instance (Eq r, Fractional r) => (Line 2 r) `IsIntersectableWith` (Line 2 r) where
nonEmptyIntersection = defaultNonEmptyIntersection
l@(Line p ~(Vector2 ux uy)) `intersect` (Line q ~v@(Vector2 vx vy))
| areParallel = if q `onLine` l then coRec l
else coRec NoIntersection
| otherwise = coRec r
where
r = q .+^ alpha *^ v
denom = vy * ux vx * uy
areParallel = denom == 0
alpha = (ux * (py qy) + uy * (qx px)) / denom
Point2 px py = p
Point2 qx qy = q
sqDistanceTo :: (Fractional r, Arity d) => Point d r -> Line d r -> r
sqDistanceTo p = fst . sqDistanceToArg p
sqDistanceToArg :: (Fractional r, Arity d)
=> Point d r -> Line d r -> (r, Point d r)
sqDistanceToArg p (Line q v) = let u = q .-. p
t = (1 * (u `dot` v)) / (v `dot` v)
m = q .+^ (v ^* t)
in (qdA m p, m)
class HasSupportingLine t where
supportingLine :: t -> Line (Dimension t) (NumType t)
instance HasSupportingLine (Line d r) where
supportingLine = id
fromLinearFunction :: Num r => r -> r -> Line 2 r
fromLinearFunction a b = Line (point2 0 b) (v2 1 a)
toLinearFunction :: forall r. (Fractional r, Eq r)
=> Line 2 r -> Maybe (r,r)
toLinearFunction l@(Line _ ~(Vector2 vx vy)) = match (l `intersect` verticalLine (0 :: r)) $
(H $ \NoIntersection -> Nothing)
:& (H $ \(Point2 _ b) -> Just (vy / vx,b))
:& (H $ \_ -> Nothing)
:& RNil
data SideTest = Below | On | Above deriving (Show,Read,Eq,Ord)
onSide :: (Ord r, Num r) => Point 2 r -> Line 2 r -> SideTest
q `onSide` (Line p v) = let r = p .+^ v
f z = (z^.xCoord, z^.yCoord)
minBy g a b = F.minimumBy (comparing g) [a,b]
maxBy g a b = F.maximumBy (comparing g) [a,b]
in case ccw (minBy f p r) (maxBy f p r) q of
CCW -> Above
CW -> Below
CoLinear -> On
liesAbove :: (Ord r, Num r) => Point 2 r -> Line 2 r -> Bool
q `liesAbove` l = q `onSide` l == Above
bisector :: Fractional r => Point 2 r -> Point 2 r -> Line 2 r
bisector p q = let v = q .-. p
h = p .+^ (v ^/ 2)
in perpendicularTo (Line h v)