geom2d-0.2.1: Geom2d/Intersect.hs
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
-- This module provides basic algebra with 2d-vectors and geomtric shapes
-- Copyright (C) 2015, Sebastian Jordan
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
module Geom2d.Intersect
where
import Geom2d.Point
import Geom2d.Line
import Geom2d.Line.Internal
class Intersect a b where
intersect :: a -> b -> Bool
instance (Eq a) => Intersect (Point' a) (Point' a) where
intersect p q = p == q
instance (Eq (p a), Num (p a), Num a, RealFloat a, Point p) =>
Intersect (InfLine p a) (InfLine p a) where
intersect a b | a == b = True
| a `parallel` b = False
| otherwise = True
instance (Point p, Eq a, Fractional a) =>
Intersect (InfLine p a) (p a) where
intersect line@(InfLine a _) q =
maybe
( x a == x q )
( \f -> f (x q) == y q )
( lineF line )
instance (Point p, Eq a, Fractional a) =>
Intersect (p a) (InfLine p a) where
intersect = flip intersect
instance (Eq (p a), Point p, Num (p a), RealFloat a) =>
Intersect (FinLine p a) (FinLine p a) where
intersect (FinLine a1 b1) (FinLine a2 b2) =
case intersection (InfLine a1 b1) (InfLine a2 b2) of
Nothing -> False
Just p ->
let minx = max (min (x a1) (x b1)) (min (x a2) (x b2))
maxx = min (max (x a1) (x b1)) (max (x a2) (x b2))
miny = max (min (y a1) (y b1)) (min (y a2) (y b2))
maxy = min (max (y a1) (y b1)) (max (y a2) (y b2))
in and [ x p <= maxx
, x p >= minx
, y p <= maxy
, y p >= miny
]
instance (Eq (p a), Point p, Num (p a), RealFloat a) =>
Intersect (InfLine p a) (FinLine p a) where
intersect infLine (FinLine f1 f2) =
case intersection infLine (InfLine f1 f2) of
Nothing -> False
Just p ->
let minx = min (x f1) (x f2)
maxx = max (x f1) (x f2)
miny = min (y f1) (y f2)
maxy = max (y f1) (y f2)
in and [ x p <= maxx
, x p >= minx
, y p <= maxy
, y p >= miny
]
instance (Eq (p a), Point p, Num (p a), RealFloat a) =>
Intersect (FinLine p a) (InfLine p a) where
intersect = flip intersect