This commit is contained in:
2025-10-11 11:46:39 +01:00
parent 4949d98789
commit 75e93dfcd5
2 changed files with 39 additions and 33 deletions
+39 -27
View File
@@ -17,12 +17,20 @@ import Linear
-- | If two lines intersect, return 'Just' that point.
intersectLineLine :: Point2 -> Point2 -> Point2 -> Point2 -> Maybe Point2
{-# INLINE intersectLineLine #-}
intersectLineLine (V2 x1 y1) (V2 x2 y2) (V2 x3 y3) (V2 x4 y4)
| den == 0 = Nothing
| otherwise = Just $ V2 (x1 + (x2 - x1) * t' / den) (y1 + (y2 - y1) * t' / den)
where
den = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
t' = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)
--intersectLineLine a@(V2 x1 y1) b@(V2 x2 y2) c@(V2 x3 y3) d@(V2 x4 y4)
intersectLineLine a b c d = do
let den = detV (a-b) (c-d)
guard $ den /= 0
let t = detV (a-c) (c-d)
return $ a + (t/den) *^ (b-a)
-- | den == 0 = Nothing
-- | otherwise = Just $ a + (t/den) *^ (b - a)
---- | otherwise = Just $ V2 (x1 + (x2 - x1) * t / den) (y1 + (y2 - y1) * t / den)
-- where
-- den = detV (a-b) (c-d)
---- den = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
-- t = detV (a-c) (c-d) --x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)
---- t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)
---- note that the second argument is a vector, not the second point of the line
--intersectLinePlane :: Point3 -> Point3 -> Point3 -> Point3 -> Maybe Point3
@@ -38,13 +46,17 @@ intersectLinePlaneAlong x y p n = do
-- this needs to be checked
intersectSegPlane :: Point3 -> Point3 -> Point3 -> Point3 -> Maybe Point3
intersectSegPlane sp ep p n = case dot v n of
0 -> Nothing
x -> case dot (p - sp) n / x of
d | d >= 0 && d <= 1 -> Just $ sp + d *.*.* v
_ -> Nothing
where
v = ep - sp
intersectSegPlane x y p n = do
d <- intersectLinePlaneAlong x y p n
guard $ d >= 0 && d < 1
return $ x + d *^ (y - x)
--case dot v n of
-- 0 -> Nothing
-- x -> case dot (p - sp) n / x of
-- d | d >= 0 && d <= 1 -> Just $ sp + d *.*.* v
-- _ -> Nothing
-- where
-- v = ep - sp
intersectSegSurface ::
Point3 ->
@@ -112,20 +124,20 @@ intersectSegRay (V2 x1 y1) (V2 x2 y2) (V2 x3 y3) (V2 x4 y4)
t' = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)
u' = (y1 - y2) * (x1 - x3) - (x1 - x2) * (y1 - y3)
-- | Similar to 'intersectSegLineFrom'', but this version is probably not correct...
intersectSegLineext :: Point2 -> Point2 -> Point2 -> Point2 -> Maybe Point2
{-# INLINE intersectSegLineext #-}
intersectSegLineext (V2 x1 y1) (V2 x2 y2) (V2 x3 y3) (V2 x4 y4)
| den == 0 = Nothing
| den > 0 && (t' < 0 || u' < den || t' > den) =
Nothing
| den < 0 && (t' > 0 || u' > - den || t' < den) =
Nothing
| otherwise = Just $ V2 (x1 + (x2 - x1) * t' / den) (y1 + (y2 - y1) * t' / den)
where
den = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
t' = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)
u' = (y1 - y2) * (x1 - x3) - (x1 - x2) * (y1 - y3)
---- | Similar to 'intersectSegLineFrom'', but this version is probably not correct...
--intersectSegLineext :: Point2 -> Point2 -> Point2 -> Point2 -> Maybe Point2
--{-# INLINE intersectSegLineext #-}
--intersectSegLineext (V2 x1 y1) (V2 x2 y2) (V2 x3 y3) (V2 x4 y4)
-- | den == 0 = Nothing
-- | den > 0 && (t' < 0 || u' < den || t' > den) =
-- Nothing
-- | den < 0 && (t' > 0 || u' > - den || t' < den) =
-- Nothing
-- | otherwise = Just $ V2 (x1 + (x2 - x1) * t' / den) (y1 + (y2 - y1) * t' / den)
-- where
-- den = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
-- t' = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)
-- u' = (y1 - y2) * (x1 - x3) - (x1 - x2) * (y1 - y3)
{- | Intersect a segment with a line.
the line intersects with the first endpoint of the segment
-6
View File
@@ -173,12 +173,6 @@ magV :: Point2 -> Float
{-# INLINE magV #-}
magV (V2 x y) = sqrt $ x ^ (2 :: Int) + y ^ (2 :: Int)
{- | Magnitude of the cross product of two vectors.
Identical to detV.
-}
crossV :: Point2 -> Point2 -> Float
crossV (V2 ax ay) (V2 bx by) = ax * by - ay * bx
-- | TO CHECK Orthographic projection of one vector onto another.
projV :: Point2 -> Point2 -> Point2
projV fromv onv