--------------------------------------------------------------------------------
-- |
-- Module      :  Algorithms.Geometry.LineSegmentIntersection
-- Copyright   :  (C) Frank Staals
-- License     :  see the LICENSE file
-- Maintainer  :  Frank Staals
--------------------------------------------------------------------------------
module Algorithms.Geometry.LineSegmentIntersection
  ( hasInteriorIntersections
  , hasSelfIntersections
  , Intersections
  , Associated(..)
  , IntersectionPoint(..)
  , isEndPointIntersection
  , associated
  , Compare
  ) where

import qualified Algorithms.Geometry.LineSegmentIntersection.BentleyOttmann as BO
import           Algorithms.Geometry.LineSegmentIntersection.Types
import           Data.Geometry.LineSegment
import           Data.Geometry.Polygon

-- Tests if there are any interior intersections.
--
-- | \(O(n \log n)\)
hasInteriorIntersections :: (Ord r, Fractional r)
                         => [LineSegment 2 p r] -> Bool
hasInteriorIntersections :: [LineSegment 2 p r] -> Bool
hasInteriorIntersections = Bool -> Bool
not (Bool -> Bool)
-> ([LineSegment 2 p r] -> Bool) -> [LineSegment 2 p r] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map (Point 2 r) (Associated p r) -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (Map (Point 2 r) (Associated p r) -> Bool)
-> ([LineSegment 2 p r] -> Map (Point 2 r) (Associated p r))
-> [LineSegment 2 p r]
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [LineSegment 2 p r] -> Map (Point 2 r) (Associated p r)
forall r p.
(Ord r, Fractional r) =>
[LineSegment 2 p r] -> Intersections p r
BO.interiorIntersections

-- | \(O(n \log n)\)
hasSelfIntersections :: (Ord r, Fractional r) => Polygon t p r -> Bool
hasSelfIntersections :: Polygon t p r -> Bool
hasSelfIntersections = [LineSegment 2 p r] -> Bool
forall r p. (Ord r, Fractional r) => [LineSegment 2 p r] -> Bool
hasInteriorIntersections ([LineSegment 2 p r] -> Bool)
-> (Polygon t p r -> [LineSegment 2 p r]) -> Polygon t p r -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Polygon t p r -> [LineSegment 2 p r]
forall (t :: PolygonType) p r. Polygon t p r -> [LineSegment 2 p r]
listEdges