POLYINT: first try of polygon intersection algorithm in O(n)

This commit is contained in:
2014-10-25 01:44:13 +02:00
parent a2e1e04072
commit 9a101d68a5
8 changed files with 296 additions and 7 deletions

View File

@@ -3,7 +3,10 @@
module Algebra.Vector where
import Algebra.VectorTypes
import Control.Applicative
import Diagrams.TwoD.Types
import Graphics.Gloss.Geometry.Line
import GHC.Float
import MyPrelude
@@ -72,6 +75,27 @@ det a b c =
(cx, cy) = unp2 c
-- |Computes the determinant of 2 points.
det' :: PT -> PT -> Double
det' a b =
ax * by - ay * bx
where
(ax, ay) = unp2 a
(bx, by) = unp2 b
-- |Get the point where two lines intesect, if any.
intersectSeg' :: Segment -> Segment -> Maybe PT
intersectSeg' (a, b) (c, d) =
glossToPt <$> intersectSegSeg (ptToGloss a)
(ptToGloss b)
(ptToGloss c)
(ptToGloss d)
where
ptToGloss = (\(x, y) -> (double2Float x, double2Float y)) <$> unp2
glossToPt = p2 . (\(x, y) -> (float2Double x, float2Double y))
-- |Get the orientation of 3 points which can either be
-- * clock-wise
-- * counter-clock-wise
@@ -95,3 +119,26 @@ notcw a b c = case getOrient a b c of
-- |Sort X and Y coordinates lexicographically.
sortedXY :: [PT] -> [PT]
sortedXY = fmap p2 . sortLex . fmap unp2
-- |Apply a function on the coordinates of a point.
onPT :: ((Double, Double) -> (Double, Double)) -> PT -> PT
onPT f = p2 . f . unp2
-- |Compare the y-coordinate of two points.
ptCmpY :: PT -> PT -> Ordering
ptCmpY p1' p2' = compare ((snd . unp2) p1') ((snd . unp2) p2')
-- |Compare the y-coordinate of two points.
ptCmpX :: PT -> PT -> Ordering
ptCmpX p1' p2' = compare ((fst . unp2) p1') ((fst . unp2) p2')
posInfPT :: PT
posInfPT = p2 (read "Infinity", read "Infinity")
negInfPT :: PT
negInfPT = p2 (negate . read $ "Infinity", negate . read $ "Infinity")