2015-01-08 00:41:14 +00:00
|
|
|
{-# OPTIONS_HADDOCK ignore-exports #-}
|
|
|
|
|
|
|
|
module Algebra.Polygon where
|
|
|
|
|
|
|
|
import Algebra.Vector
|
2015-01-09 02:24:09 +00:00
|
|
|
import Data.Maybe
|
2015-01-14 17:17:35 +00:00
|
|
|
import Diagrams.TwoD.Types
|
2015-01-08 00:41:14 +00:00
|
|
|
import MyPrelude
|
|
|
|
|
|
|
|
|
2015-01-09 03:01:43 +00:00
|
|
|
-- |Split a polygon by a given segment which must be vertices of the
|
|
|
|
-- polygon (returns empty array otherwise).
|
2015-05-21 00:14:15 +00:00
|
|
|
splitPoly :: [P2 Double]
|
|
|
|
-> (P2 Double, P2 Double)
|
|
|
|
-> [[P2 Double]]
|
2015-01-08 00:41:14 +00:00
|
|
|
splitPoly pts (a, b)
|
|
|
|
| elem a pts && elem b pts =
|
|
|
|
[b : takeWhile (/= b) shiftedPoly, a : dropWhile (/= b) shiftedPoly]
|
|
|
|
| otherwise = [[]]
|
|
|
|
where
|
|
|
|
shiftedPoly = shiftM' a pts
|
|
|
|
|
|
|
|
|
2015-01-09 03:01:43 +00:00
|
|
|
-- |Get all edges of a polygon.
|
2015-05-21 00:14:15 +00:00
|
|
|
polySegments :: [P2 Double] -> [(P2 Double, P2 Double)]
|
2015-01-08 00:41:14 +00:00
|
|
|
polySegments p@(x':_:_:_) = go p ++ [(last p, x')]
|
|
|
|
where
|
|
|
|
go (x:y:xs) = (x, y) : go (y:xs)
|
|
|
|
go _ = []
|
|
|
|
polySegments _ = []
|
2015-01-09 02:20:13 +00:00
|
|
|
|
|
|
|
|
2015-01-09 03:01:43 +00:00
|
|
|
-- |Check whether the given segment is inside the polygon.
|
|
|
|
-- This doesn't check for segments that are completely outside
|
|
|
|
-- of the polygon yet.
|
2015-05-21 00:14:15 +00:00
|
|
|
isInsidePoly :: [P2 Double] -> (P2 Double, P2 Double) -> Bool
|
2015-01-09 02:20:13 +00:00
|
|
|
isInsidePoly pts seg =
|
|
|
|
null
|
|
|
|
. catMaybes
|
2015-11-25 20:49:48 +00:00
|
|
|
. fmap (intersectSeg seg)
|
2015-01-09 02:20:13 +00:00
|
|
|
$ polySegments pts
|
|
|
|
|
2015-01-09 02:24:09 +00:00
|
|
|
|
2015-01-09 03:01:43 +00:00
|
|
|
-- |Check whether two points are adjacent vertices of a polygon.
|
2015-05-21 00:14:15 +00:00
|
|
|
adjacent :: P2 Double -> P2 Double -> [P2 Double] -> Bool
|
2015-01-09 02:24:09 +00:00
|
|
|
adjacent u v = any (\x -> x == (u, v) || x == (v, u)) . polySegments
|
|
|
|
|
|
|
|
|
2015-01-09 03:01:43 +00:00
|
|
|
-- |Check whether the polygon is a triangle polygon.
|
2015-05-21 00:14:15 +00:00
|
|
|
isTrianglePoly :: [P2 Double] -> Bool
|
2015-01-09 02:24:09 +00:00
|
|
|
isTrianglePoly [_, _, _] = True
|
|
|
|
isTrianglePoly _ = False
|
|
|
|
|
|
|
|
|
2015-01-09 03:01:43 +00:00
|
|
|
-- |Get all triangle polygons.
|
2015-05-21 00:14:15 +00:00
|
|
|
triangleOnly :: [[P2 Double]] -> [[P2 Double]]
|
2015-01-09 02:24:09 +00:00
|
|
|
triangleOnly = filter isTrianglePoly
|
|
|
|
|
|
|
|
|
2015-01-09 03:01:43 +00:00
|
|
|
-- |Get all non-triangle polygons.
|
2015-05-21 00:14:15 +00:00
|
|
|
nonTriangleOnly :: [[P2 Double]] -> [[P2 Double]]
|
2015-01-09 02:24:09 +00:00
|
|
|
nonTriangleOnly = filter (not . isTrianglePoly)
|