Add new files
This commit is contained in:
@@ -87,6 +87,7 @@ myIntersectSegSeg
|
||||
-> Point2
|
||||
-> Point2
|
||||
-> Maybe Point2
|
||||
{-# INLINE myIntersectSegSeg #-}
|
||||
myIntersectSegSeg a@(V2 ax ay) b@(V2 bx by) c@(V2 cx cy) d@(V2 dx dy) = case ratIntersectLineLine a b c d of
|
||||
Nothing -> Nothing
|
||||
Just (V2 x y) -> if inbetween x && inbetween' y
|
||||
@@ -99,6 +100,7 @@ myIntersectSegSeg a@(V2 ax ay) b@(V2 bx by) c@(V2 cx cy) d@(V2 dx dy) = case rat
|
||||
&& ((cy <= y && y <= dy) || (dy <= y && y <= cy))
|
||||
-- | Polymorphic intersection of fractional line points.
|
||||
myIntersectLineLine :: (Eq a,Fractional a) => V2 a -> V2 a -> V2 a -> V2 a -> Maybe (V2 a)
|
||||
{-# INLINE myIntersectLineLine #-}
|
||||
myIntersectLineLine a@(V2 ax _) b c@(V2 cx _) d
|
||||
| isNothing (linGrad a b) = V2 ax <$> axisInt (c *-* V2 ax 0) (d *-* V2 ax 0)
|
||||
| isNothing (linGrad c d) = V2 cx <$> axisInt (a *-* V2 cx 0) (b *-* V2 cx 0)
|
||||
@@ -115,6 +117,7 @@ myIntersectLineLine a@(V2 ax _) b c@(V2 cx _) d
|
||||
(*-*) (V2 ax' ay) (V2 bx by) = V2 (ax'-bx) (ay-by)
|
||||
-- | Transforms floating points to rationals then performs line intersection.
|
||||
ratIntersectLineLine :: Point2 -> Point2 -> Point2 -> Point2 -> Maybe Point2
|
||||
{-# INLINE ratIntersectLineLine #-}
|
||||
ratIntersectLineLine a b c d = toNumPoint2
|
||||
<$> myIntersectLineLine (toRatPoint2 a) (toRatPoint2 b) (toRatPoint2 c) (toRatPoint2 d)
|
||||
where
|
||||
@@ -131,18 +134,19 @@ but is symmetric around 0:
|
||||
|
||||
>>> roundPoint2 (0.5,-0.5)
|
||||
(0.0,0.0)
|
||||
|
||||
-}
|
||||
roundPoint2 :: Point2 -> Point2
|
||||
roundPoint2 (V2 x y) = V2 (fromIntegral (round x :: Int)) (fromIntegral (round y :: Int))
|
||||
-- | Given two points, finds the linear gradient if it is non-infinite.
|
||||
linGrad :: (Eq a,Fractional a) => V2 a -> V2 a -> Maybe a
|
||||
{-# INLINE linGrad #-}
|
||||
linGrad (V2 x y) (V2 a b)
|
||||
| x-a == 0 = Nothing
|
||||
| otherwise = Just $ (y-b)/(x-a)
|
||||
-- | Given two points, finds the intersection with the y axis if it exists.
|
||||
axisInt :: (Eq a,Fractional a) => V2 a -> V2 a -> Maybe a
|
||||
axisInt p (V2 a b) = (\lg -> b - (a*lg)) <$> linGrad p (V2 a b)
|
||||
{-# INLINE axisInt #-}
|
||||
axisInt p (V2 a b) = (\lg -> b - (a*lg)) <$> linGrad p (V2 a b)
|
||||
-- | Placeholder, undefined.
|
||||
intersectSegsSeg :: [Point2] -> Point2 -> Point2 -> Maybe Point2
|
||||
intersectSegsSeg = undefined
|
||||
|
||||
Reference in New Issue
Block a user