{-# LANGUAGE RebindableSyntax #-}
{-# LANGUAGE TypeFamilies #-}
module NumHask.Space.Point
( Point (..),
rotateP,
gridP,
dotP,
(<.>),
crossP,
flipY,
Line (..),
lineSolve,
lineDistance,
closestPoint,
lineIntersect,
translate,
scaleT,
skew,
)
where
import Data.Distributive
import Data.Functor.Classes
import Data.Functor.Rep
import NumHask.Prelude hiding (Distributive)
import NumHask.Space.Range
import NumHask.Space.Types
import System.Random
import System.Random.Stateful
data Point a = Point
{ forall a. Point a -> a
_x :: a,
forall a. Point a -> a
_y :: a
}
deriving (Point a -> Point a -> Bool
forall a. Eq a => Point a -> Point a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Point a -> Point a -> Bool
$c/= :: forall a. Eq a => Point a -> Point a -> Bool
== :: Point a -> Point a -> Bool
$c== :: forall a. Eq a => Point a -> Point a -> Bool
Eq, forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (Point a) x -> Point a
forall a x. Point a -> Rep (Point a) x
$cto :: forall a x. Rep (Point a) x -> Point a
$cfrom :: forall a x. Point a -> Rep (Point a) x
Generic)
instance Eq1 Point where
liftEq :: forall a b. (a -> b -> Bool) -> Point a -> Point b -> Bool
liftEq a -> b -> Bool
f (Point a
a a
b) (Point b
c b
d) = a -> b -> Bool
f a
a b
c Bool -> Bool -> Bool
&& a -> b -> Bool
f a
b b
d
instance (Ord a, Additive a, Show a) => Show (Point a) where
show :: Point a -> String
show (Point a
a a
b) = String
"Point " forall a. Semigroup a => a -> a -> a
<> forall {a}. (Show a, Ord a, Additive a) => a -> String
wrap a
a forall a. Semigroup a => a -> a -> a
<> String
" " forall a. Semigroup a => a -> a -> a
<> forall {a}. (Show a, Ord a, Additive a) => a -> String
wrap a
b
where
wrap :: a -> String
wrap a
x = forall a. a -> a -> Bool -> a
bool (forall a. Show a => a -> String
show a
x) (String
"(" forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show a
x forall a. Semigroup a => a -> a -> a
<> String
")") (a
x forall a. Ord a => a -> a -> Bool
< forall a. Additive a => a
zero)
instance Functor Point where
fmap :: forall a b. (a -> b) -> Point a -> Point b
fmap a -> b
f (Point a
a a
b) = forall a. a -> a -> Point a
Point (a -> b
f a
a) (a -> b
f a
b)
instance Applicative Point where
pure :: forall a. a -> Point a
pure a
a = forall a. a -> a -> Point a
Point a
a a
a
(Point a -> b
fa a -> b
fb) <*> :: forall a b. Point (a -> b) -> Point a -> Point b
<*> Point a
a a
b = forall a. a -> a -> Point a
Point (a -> b
fa a
a) (a -> b
fb a
b)
instance Monad Point where
Point a
a a
b >>= :: forall a b. Point a -> (a -> Point b) -> Point b
>>= a -> Point b
f = forall a. a -> a -> Point a
Point b
a' b
b'
where
Point b
a' b
_ = a -> Point b
f a
a
Point b
_ b
b' = a -> Point b
f a
b
instance Foldable Point where
foldMap :: forall m a. Monoid m => (a -> m) -> Point a -> m
foldMap a -> m
f (Point a
a a
b) = a -> m
f a
a forall a. Monoid a => a -> a -> a
`mappend` a -> m
f a
b
instance Traversable Point where
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Point a -> f (Point b)
traverse a -> f b
f (Point a
a a
b) = forall a. a -> a -> Point a
Point forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
a forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> f b
f a
b
instance (Semigroup a) => Semigroup (Point a) where
(Point a
a0 a
b0) <> :: Point a -> Point a -> Point a
<> (Point a
a1 a
b1) = forall a. a -> a -> Point a
Point (a
a0 forall a. Semigroup a => a -> a -> a
<> a
a1) (a
b0 forall a. Semigroup a => a -> a -> a
<> a
b1)
instance (Monoid a) => Monoid (Point a) where
mempty :: Point a
mempty = forall a. a -> a -> Point a
Point forall a. Monoid a => a
mempty forall a. Monoid a => a
mempty
mappend :: Point a -> Point a -> Point a
mappend = forall a. Semigroup a => a -> a -> a
(<>)
instance (Bounded a) => Bounded (Point a) where
minBound :: Point a
minBound = forall a. a -> a -> Point a
Point forall a. Bounded a => a
minBound forall a. Bounded a => a
minBound
maxBound :: Point a
maxBound = forall a. a -> a -> Point a
Point forall a. Bounded a => a
maxBound forall a. Bounded a => a
maxBound
instance (Additive a) => Additive (Point a) where
(Point a
a0 a
b0) + :: Point a -> Point a -> Point a
+ (Point a
a1 a
b1) = forall a. a -> a -> Point a
Point (a
a0 forall a. Additive a => a -> a -> a
+ a
a1) (a
b0 forall a. Additive a => a -> a -> a
+ a
b1)
zero :: Point a
zero = forall a. a -> a -> Point a
Point forall a. Additive a => a
zero forall a. Additive a => a
zero
instance (Subtractive a) => Subtractive (Point a) where
negate :: Point a -> Point a
negate = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Subtractive a => a -> a
negate
instance (Multiplicative a) => Multiplicative (Point a) where
(Point a
a0 a
b0) * :: Point a -> Point a -> Point a
* (Point a
a1 a
b1) = forall a. a -> a -> Point a
Point (a
a0 forall a. Multiplicative a => a -> a -> a
* a
a1) (a
b0 forall a. Multiplicative a => a -> a -> a
* a
b1)
one :: Point a
one = forall a. a -> a -> Point a
Point forall a. Multiplicative a => a
one forall a. Multiplicative a => a
one
instance (Divisive a) => Divisive (Point a) where
recip :: Point a -> Point a
recip = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Divisive a => a -> a
recip
instance Distributive Point where
collect :: forall (f :: * -> *) a b.
Functor f =>
(a -> Point b) -> f a -> Point (f b)
collect a -> Point b
f f a
x = forall a. a -> a -> Point a
Point (forall a. Point a -> a
getL forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> Point b
f forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a
x) (forall a. Point a -> a
getR forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> Point b
f forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a
x)
where
getL :: Point a -> a
getL (Point a
l a
_) = a
l
getR :: Point a -> a
getR (Point a
_ a
r) = a
r
instance (Additive a) => AdditiveAction (Point a) where
type AdditiveScalar (Point a) = a
|+ :: Point a -> AdditiveScalar (Point a) -> Point a
(|+) (Point a
x a
y) AdditiveScalar (Point a)
a = forall a. a -> a -> Point a
Point (AdditiveScalar (Point a)
a forall a. Additive a => a -> a -> a
+ a
x) (AdditiveScalar (Point a)
a forall a. Additive a => a -> a -> a
+ a
y)
instance (Subtractive a) => SubtractiveAction (Point a) where
|- :: Point a -> AdditiveScalar (Point a) -> Point a
(|-) (Point a
x a
y) AdditiveScalar (Point a)
a = forall a. a -> a -> Point a
Point (a
x forall a. Subtractive a => a -> a -> a
- AdditiveScalar (Point a)
a) (a
y forall a. Subtractive a => a -> a -> a
- AdditiveScalar (Point a)
a)
instance (Multiplicative a) => MultiplicativeAction (Point a) where
type Scalar (Point a) = a
|* :: Point a -> Scalar (Point a) -> Point a
(|*) (Point a
x a
y) Scalar (Point a)
a = forall a. a -> a -> Point a
Point (Scalar (Point a)
a forall a. Multiplicative a => a -> a -> a
* a
x) (Scalar (Point a)
a forall a. Multiplicative a => a -> a -> a
* a
y)
instance (Divisive a) => DivisiveAction (Point a) where
|/ :: Point a -> Scalar (Point a) -> Point a
(|/) (Point a
x a
y) Scalar (Point a)
a = forall a. a -> a -> Point a
Point (a
x forall a. Divisive a => a -> a -> a
/ Scalar (Point a)
a) (a
y forall a. Divisive a => a -> a -> a
/ Scalar (Point a)
a)
instance Representable Point where
type Rep Point = Bool
tabulate :: forall a. (Rep Point -> a) -> Point a
tabulate Rep Point -> a
f = forall a. a -> a -> Point a
Point (Rep Point -> a
f Bool
False) (Rep Point -> a
f Bool
True)
index :: forall a. Point a -> Rep Point -> a
index (Point a
l a
_) Bool
Rep Point
False = a
l
index (Point a
_ a
r) Bool
Rep Point
True = a
r
instance (Ord a) => JoinSemiLattice (Point a) where
\/ :: Point a -> Point a -> Point a
(\/) (Point a
x a
y) (Point a
x' a
y') = forall a. a -> a -> Point a
Point (forall a. Ord a => a -> a -> a
max a
x a
x') (forall a. Ord a => a -> a -> a
max a
y a
y')
instance (Ord a) => MeetSemiLattice (Point a) where
/\ :: Point a -> Point a -> Point a
(/\) (Point a
x a
y) (Point a
x' a
y') = forall a. a -> a -> Point a
Point (forall a. Ord a => a -> a -> a
min a
x a
x') (forall a. Ord a => a -> a -> a
min a
y a
y')
instance
(ExpField a, Eq a) =>
Basis (Point a)
where
type Mag (Point a) = a
type Base (Point a) = Point a
magnitude :: Point a -> Mag (Point a)
magnitude (Point a
x a
y) = forall a. ExpField a => a -> a
sqrt (a
x forall a. Multiplicative a => a -> a -> a
* a
x forall a. Additive a => a -> a -> a
+ a
y forall a. Multiplicative a => a -> a -> a
* a
y)
basis :: Point a -> Base (Point a)
basis Point a
p = let m :: Mag (Point a)
m = forall a. Basis a => a -> Mag a
magnitude Point a
p in forall a. a -> a -> Bool -> a
bool (Point a
p forall m. DivisiveAction m => m -> Scalar m -> m
|/ Mag (Point a)
m) forall a. Additive a => a
zero (Mag (Point a)
m forall a. Eq a => a -> a -> Bool
== forall a. Additive a => a
zero)
instance (TrigField a) => Direction (Point a) where
type Dir (Point a) = a
angle :: Point a -> Dir (Point a)
angle (Point a
x a
y) = forall a. TrigField a => a -> a -> a
atan2 a
y a
x
ray :: Dir (Point a) -> Point a
ray Dir (Point a)
x = forall a. a -> a -> Point a
Point (forall a. TrigField a => a -> a
cos Dir (Point a)
x) (forall a. TrigField a => a -> a
sin Dir (Point a)
x)
instance (UniformRange a) => UniformRange (Point a) where
uniformRM :: forall g (m :: * -> *).
StatefulGen g m =>
(Point a, Point a) -> g -> m (Point a)
uniformRM (Point a
x a
y, Point a
x' a
y') g
g =
forall a. a -> a -> Point a
Point forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a g (m :: * -> *).
(UniformRange a, StatefulGen g m) =>
(a, a) -> g -> m a
uniformRM (a
x, a
x') g
g forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a g (m :: * -> *).
(UniformRange a, StatefulGen g m) =>
(a, a) -> g -> m a
uniformRM (a
y, a
y') g
g
instance (Multiplicative a, Additive a) => Affinity (Point a) a where
transform :: Transform a -> Point a -> Point a
transform (Transform a
a a
b a
c a
d a
e a
f) (Point a
x a
y) =
forall a. a -> a -> Point a
Point (a
a forall a. Multiplicative a => a -> a -> a
* a
x forall a. Additive a => a -> a -> a
+ a
b forall a. Multiplicative a => a -> a -> a
* a
y forall a. Additive a => a -> a -> a
+ a
c) (a
d forall a. Multiplicative a => a -> a -> a
* a
x forall a. Additive a => a -> a -> a
+ a
e forall a. Multiplicative a => a -> a -> a
* a
y forall a. Additive a => a -> a -> a
+ a
f)
translate :: (TrigField a) => Point a -> Transform a
translate :: forall a. TrigField a => Point a -> Transform a
translate (Point a
x a
y) = forall a. a -> a -> a -> a -> a -> a -> Transform a
Transform forall a. Multiplicative a => a
one forall a. Additive a => a
zero a
x forall a. Additive a => a
zero forall a. Multiplicative a => a
one a
y
scaleT :: (TrigField a) => Point a -> Transform a
scaleT :: forall a. TrigField a => Point a -> Transform a
scaleT (Point a
x a
y) = forall a. a -> a -> a -> a -> a -> a -> Transform a
Transform a
x forall a. Additive a => a
zero forall a. Additive a => a
zero a
y forall a. Additive a => a
zero forall a. Additive a => a
zero
skew :: (TrigField a) => Point a -> Transform a
skew :: forall a. TrigField a => Point a -> Transform a
skew (Point a
x a
y) = forall a. a -> a -> a -> a -> a -> a -> Transform a
Transform forall a. Multiplicative a => a
one (forall a. TrigField a => a -> a
tan a
x) forall a. Additive a => a
zero (forall a. TrigField a => a -> a
tan a
y) forall a. Multiplicative a => a
one forall a. Additive a => a
zero
rotateP :: (TrigField a) => a -> Point a -> Point a
rotateP :: forall a. TrigField a => a -> Point a -> Point a
rotateP a
d Point a
p = forall a. TrigField a => a -> Transform a
rotate a
d forall a b. Affinity a b => Transform b -> a -> a
|. Point a
p
gridP :: (FieldSpace (Range a)) => (a -> a) -> Range a -> Grid (Range a) -> [Point a]
gridP :: forall a.
FieldSpace (Range a) =>
(a -> a) -> Range a -> Grid (Range a) -> [Point a]
gridP a -> a
f Range a
r Grid (Range a)
g = (\a
x -> forall a. a -> a -> Point a
Point a
x (a -> a
f a
x)) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s. FieldSpace s => Pos -> s -> Grid s -> [Element s]
grid Pos
OuterPos Range a
r Grid (Range a)
g
dotP :: (Multiplicative a, Additive a) => Point a -> Point a -> a
dotP :: forall a. (Multiplicative a, Additive a) => Point a -> Point a -> a
dotP (Point a
x a
y) (Point a
x' a
y') = a
x forall a. Multiplicative a => a -> a -> a
* a
x' forall a. Additive a => a -> a -> a
+ a
y forall a. Multiplicative a => a -> a -> a
* a
y'
infix 4 <.>
(<.>) :: (Multiplicative a, Additive a) => Point a -> Point a -> a
<.> :: forall a. (Multiplicative a, Additive a) => Point a -> Point a -> a
(<.>) = forall a. (Multiplicative a, Additive a) => Point a -> Point a -> a
dotP
crossP :: (Multiplicative a, Subtractive a) => Point a -> Point a -> a
crossP :: forall a.
(Multiplicative a, Subtractive a) =>
Point a -> Point a -> a
crossP (Point a
x a
y) (Point a
x' a
y') = a
x forall a. Multiplicative a => a -> a -> a
* a
y' forall a. Subtractive a => a -> a -> a
- a
y forall a. Multiplicative a => a -> a -> a
* a
x'
flipY :: (Subtractive a) => Point a -> Point a
flipY :: forall a. Subtractive a => Point a -> Point a
flipY (Point a
x a
y) = forall a. a -> a -> Point a
Point a
x (-a
y)
data Line a = Line
{ forall a. Line a -> Point a
lineStart :: Point a,
forall a. Line a -> Point a
lineEnd :: Point a
}
deriving (Int -> Line a -> ShowS
forall a. (Ord a, Additive a, Show a) => Int -> Line a -> ShowS
forall a. (Ord a, Additive a, Show a) => [Line a] -> ShowS
forall a. (Ord a, Additive a, Show a) => Line a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Line a] -> ShowS
$cshowList :: forall a. (Ord a, Additive a, Show a) => [Line a] -> ShowS
show :: Line a -> String
$cshow :: forall a. (Ord a, Additive a, Show a) => Line a -> String
showsPrec :: Int -> Line a -> ShowS
$cshowsPrec :: forall a. (Ord a, Additive a, Show a) => Int -> Line a -> ShowS
Show, Line a -> Line a -> Bool
forall a. Eq a => Line a -> Line a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Line a -> Line a -> Bool
$c/= :: forall a. Eq a => Line a -> Line a -> Bool
== :: Line a -> Line a -> Bool
$c== :: forall a. Eq a => Line a -> Line a -> Bool
Eq, forall a b. a -> Line b -> Line a
forall a b. (a -> b) -> Line a -> Line b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Line b -> Line a
$c<$ :: forall a b. a -> Line b -> Line a
fmap :: forall a b. (a -> b) -> Line a -> Line b
$cfmap :: forall a b. (a -> b) -> Line a -> Line b
Functor, forall a. Eq a => a -> Line a -> Bool
forall a. Num a => Line a -> a
forall a. Ord a => Line a -> a
forall m. Monoid m => Line m -> m
forall a. Line a -> Bool
forall a. Line a -> Int
forall a. Line a -> [a]
forall a. (a -> a -> a) -> Line a -> a
forall m a. Monoid m => (a -> m) -> Line a -> m
forall b a. (b -> a -> b) -> b -> Line a -> b
forall a b. (a -> b -> b) -> b -> Line a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: forall a. Num a => Line a -> a
$cproduct :: forall a. Num a => Line a -> a
sum :: forall a. Num a => Line a -> a
$csum :: forall a. Num a => Line a -> a
minimum :: forall a. Ord a => Line a -> a
$cminimum :: forall a. Ord a => Line a -> a
maximum :: forall a. Ord a => Line a -> a
$cmaximum :: forall a. Ord a => Line a -> a
elem :: forall a. Eq a => a -> Line a -> Bool
$celem :: forall a. Eq a => a -> Line a -> Bool
length :: forall a. Line a -> Int
$clength :: forall a. Line a -> Int
null :: forall a. Line a -> Bool
$cnull :: forall a. Line a -> Bool
toList :: forall a. Line a -> [a]
$ctoList :: forall a. Line a -> [a]
foldl1 :: forall a. (a -> a -> a) -> Line a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Line a -> a
foldr1 :: forall a. (a -> a -> a) -> Line a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> Line a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> Line a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Line a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Line a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Line a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Line a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Line a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Line a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> Line a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> Line a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Line a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Line a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Line a -> m
fold :: forall m. Monoid m => Line m -> m
$cfold :: forall m. Monoid m => Line m -> m
Foldable, Functor Line
Foldable Line
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => Line (m a) -> m (Line a)
forall (f :: * -> *) a. Applicative f => Line (f a) -> f (Line a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Line a -> m (Line b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Line a -> f (Line b)
sequence :: forall (m :: * -> *) a. Monad m => Line (m a) -> m (Line a)
$csequence :: forall (m :: * -> *) a. Monad m => Line (m a) -> m (Line a)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Line a -> m (Line b)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Line a -> m (Line b)
sequenceA :: forall (f :: * -> *) a. Applicative f => Line (f a) -> f (Line a)
$csequenceA :: forall (f :: * -> *) a. Applicative f => Line (f a) -> f (Line a)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Line a -> f (Line b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Line a -> f (Line b)
Traversable)
instance (Multiplicative a, Additive a) => Affinity (Line a) a where
transform :: Transform a -> Line a -> Line a
transform Transform a
t (Line Point a
s Point a
e) = forall a. Point a -> Point a -> Line a
Line (forall a b. Affinity a b => Transform b -> a -> a
transform Transform a
t Point a
s) (forall a b. Affinity a b => Transform b -> a -> a
transform Transform a
t Point a
e)
lineSolve :: (ExpField a, Eq a) => Line a -> (a, a, a)
lineSolve :: forall a. (ExpField a, Eq a) => Line a -> (a, a, a)
lineSolve (Line Point a
p1 Point a
p2) = (-a
my, a
mx, a
c)
where
m :: Base (Point a)
m@(Point a
mx a
my) = forall a. Basis a => a -> Base a
basis (Point a
p2 forall a. Subtractive a => a -> a -> a
- Point a
p1)
c :: a
c = forall a.
(Multiplicative a, Subtractive a) =>
Point a -> Point a -> a
crossP Point a
p1 Base (Point a)
m
lineDistance :: (ExpField a) => Line a -> Point a -> a
lineDistance :: forall a. ExpField a => Line a -> Point a -> a
lineDistance (Line (Point a
x1 a
y1) (Point a
x2 a
y2)) =
let dy :: a
dy = a
y1 forall a. Subtractive a => a -> a -> a
- a
y2
dx :: a
dx = a
x2 forall a. Subtractive a => a -> a -> a
- a
x1
d :: a
d = forall a. ExpField a => a -> a
sqrt (a
dx forall a. Multiplicative a => a -> a -> a
* a
dx forall a. Additive a => a -> a -> a
+ a
dy forall a. Multiplicative a => a -> a -> a
* a
dy)
in a
dy seq :: forall a b. a -> b -> b
`seq`
a
dx seq :: forall a b. a -> b -> b
`seq`
a
d seq :: forall a b. a -> b -> b
`seq`
\(Point a
x a
y) -> (a
x forall a. Subtractive a => a -> a -> a
- a
x1) forall a. Multiplicative a => a -> a -> a
* a
dy forall a. Divisive a => a -> a -> a
/ a
d forall a. Additive a => a -> a -> a
+ (a
y forall a. Subtractive a => a -> a -> a
- a
y1) forall a. Multiplicative a => a -> a -> a
* a
dx forall a. Divisive a => a -> a -> a
/ a
d
closestPoint :: (Field a) => Line a -> Point a -> Point a
closestPoint :: forall a. Field a => Line a -> Point a -> Point a
closestPoint (Line Point a
p1 Point a
p2) Point a
p3 = forall a. a -> a -> Point a
Point a
px a
py
where
d :: Point a
d@(Point a
dx a
dy) = Point a
p2 forall a. Subtractive a => a -> a -> a
- Point a
p1
u :: a
u = a
dy forall a. Multiplicative a => a -> a -> a
* forall a. Point a -> a
_y Point a
p3 forall a. Additive a => a -> a -> a
+ a
dx forall a. Multiplicative a => a -> a -> a
* forall a. Point a -> a
_x Point a
p3
v :: a
v = forall a. Point a -> a
_x Point a
p1 forall a. Multiplicative a => a -> a -> a
* forall a. Point a -> a
_y Point a
p2 forall a. Subtractive a => a -> a -> a
- forall a. Point a -> a
_x Point a
p2 forall a. Multiplicative a => a -> a -> a
* forall a. Point a -> a
_y Point a
p1
m :: a
m = Point a
d forall a. (Multiplicative a, Additive a) => Point a -> Point a -> a
<.> Point a
d
px :: a
px = (a
dx forall a. Multiplicative a => a -> a -> a
* a
u forall a. Additive a => a -> a -> a
+ a
dy forall a. Multiplicative a => a -> a -> a
* a
v) forall a. Divisive a => a -> a -> a
/ a
m
py :: a
py = (a
dy forall a. Multiplicative a => a -> a -> a
* a
u forall a. Subtractive a => a -> a -> a
- a
dx forall a. Multiplicative a => a -> a -> a
* a
v) forall a. Divisive a => a -> a -> a
/ a
m
lineIntersect :: (Ord a, Epsilon a, Absolute a, Field a) => Line a -> Line a -> Maybe (Point a)
lineIntersect :: forall a.
(Ord a, Epsilon a, Absolute a, Field a) =>
Line a -> Line a -> Maybe (Point a)
lineIntersect (Line Point a
p1 Point a
p2) (Line Point a
p3 Point a
p4)
| forall a. Absolute a => a -> a
abs a
det forall a. Ord a => a -> a -> Bool
<= forall a. Epsilon a => a
epsilon = forall a. Maybe a
Nothing
| Bool
otherwise = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ (a
a forall m. MultiplicativeAction m => Scalar m -> m -> m
*| Point a
d2 forall a. Subtractive a => a -> a -> a
- a
b forall m. MultiplicativeAction m => Scalar m -> m -> m
*| Point a
d1) forall m. DivisiveAction m => m -> Scalar m -> m
|/ a
det
where
d1 :: Point a
d1 = Point a
p1 forall a. Subtractive a => a -> a -> a
- Point a
p2
d2 :: Point a
d2 = Point a
p3 forall a. Subtractive a => a -> a -> a
- Point a
p4
det :: a
det = forall a.
(Multiplicative a, Subtractive a) =>
Point a -> Point a -> a
crossP Point a
d1 Point a
d2
a :: a
a = forall a.
(Multiplicative a, Subtractive a) =>
Point a -> Point a -> a
crossP Point a
p1 Point a
p2
b :: a
b = forall a.
(Multiplicative a, Subtractive a) =>
Point a -> Point a -> a
crossP Point a
p3 Point a
p4