2014-10-07 17:12:07 +00:00
|
|
|
{-# OPTIONS_HADDOCK ignore-exports #-}
|
2014-12-17 23:31:43 +00:00
|
|
|
{-# LANGUAGE ViewPatterns #-}
|
2014-10-07 17:12:07 +00:00
|
|
|
|
2014-10-10 15:40:08 +00:00
|
|
|
module Algebra.Vector where
|
2014-10-07 17:12:07 +00:00
|
|
|
|
2014-10-24 23:44:13 +00:00
|
|
|
import Control.Applicative
|
2014-12-13 03:05:28 +00:00
|
|
|
import Control.Arrow ((***))
|
2014-11-29 04:08:54 +00:00
|
|
|
import Data.List (sortBy)
|
2014-12-17 23:31:43 +00:00
|
|
|
import Diagrams.Coordinates
|
2014-10-08 14:31:57 +00:00
|
|
|
import Diagrams.TwoD.Types
|
2014-10-24 23:44:13 +00:00
|
|
|
import Graphics.Gloss.Geometry.Line
|
|
|
|
import GHC.Float
|
2014-10-13 20:06:12 +00:00
|
|
|
import MyPrelude
|
2014-10-07 17:12:07 +00:00
|
|
|
|
|
|
|
|
2014-12-03 20:41:00 +00:00
|
|
|
data Alignment = CW
|
|
|
|
| CCW
|
|
|
|
| CL
|
|
|
|
deriving (Eq)
|
|
|
|
|
|
|
|
|
2014-12-17 02:35:33 +00:00
|
|
|
-- |Convert two dimensions such as (xmin, xmax) and (ymin, ymax)
|
|
|
|
-- to proper square coordinates, as in:
|
|
|
|
-- ((xmin, ymin), (xmax, ymax))
|
|
|
|
dimToSquare :: (Double, Double) -- ^ x dimension
|
|
|
|
-> (Double, Double) -- ^ y dimension
|
2015-01-14 17:17:35 +00:00
|
|
|
-> ((Double, Double), (Double, Double)) -- ^ square describing those dimensions
|
2014-12-17 02:35:33 +00:00
|
|
|
dimToSquare (x1, x2) (y1, y2) = ((x1, y1), (x2, y2))
|
|
|
|
|
|
|
|
|
|
|
|
-- |Checks whether the Point is in a given Square.
|
2015-01-14 17:17:35 +00:00
|
|
|
inRange :: ((Double, Double), (Double, Double)) -- ^ the square: ((xmin, ymin), (xmax, ymax))
|
|
|
|
-> P2 -- ^ Coordinate
|
2014-11-13 22:04:26 +00:00
|
|
|
-> Bool -- ^ result
|
2014-12-17 23:31:43 +00:00
|
|
|
inRange ((xmin, ymin), (xmax, ymax)) (coords -> x :& y)
|
2014-12-17 02:35:33 +00:00
|
|
|
= x >= min xmin xmax
|
|
|
|
&& x <= max xmin xmax
|
|
|
|
&& y >= min ymin ymax
|
|
|
|
&& y <= max ymin ymax
|
2014-10-07 17:12:07 +00:00
|
|
|
|
|
|
|
|
2014-10-09 01:15:27 +00:00
|
|
|
-- |Get the angle between two vectors.
|
2015-01-14 17:17:35 +00:00
|
|
|
getAngle :: R2 -> R2 -> Double
|
2014-10-09 22:19:05 +00:00
|
|
|
getAngle a b =
|
2014-12-17 23:33:24 +00:00
|
|
|
acos
|
|
|
|
. flip (/) (vecLength a * vecLength b)
|
|
|
|
. scalarProd a
|
|
|
|
$ b
|
2014-10-08 14:31:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Get the length of a vector.
|
2015-01-14 17:17:35 +00:00
|
|
|
vecLength :: R2 -> Double
|
2014-10-13 17:15:09 +00:00
|
|
|
vecLength v = sqrt (x^(2 :: Int) + y^(2 :: Int))
|
2014-10-08 14:31:57 +00:00
|
|
|
where
|
|
|
|
(x, y) = unr2 v
|
|
|
|
|
|
|
|
|
|
|
|
-- |Compute the scalar product of two vectors.
|
2015-01-14 17:17:35 +00:00
|
|
|
scalarProd :: R2 -> R2 -> Double
|
2014-12-17 18:36:40 +00:00
|
|
|
scalarProd (R2 a1 a2) (R2 b1 b2) = a1 * b1 + a2 * b2
|
2014-10-08 14:31:57 +00:00
|
|
|
|
|
|
|
|
2014-12-17 18:41:15 +00:00
|
|
|
-- |Multiply a scalar with a vector.
|
2015-01-14 17:17:35 +00:00
|
|
|
scalarMul :: Double -> R2 -> R2
|
2014-12-17 18:41:15 +00:00
|
|
|
scalarMul d (R2 a b) = R2 (a * d) (b * d)
|
|
|
|
|
|
|
|
|
2014-10-08 14:31:57 +00:00
|
|
|
-- |Construct a vector that points to a point from the origin.
|
2015-01-14 17:17:35 +00:00
|
|
|
pt2Vec :: P2 -> R2
|
2014-10-08 14:31:57 +00:00
|
|
|
pt2Vec = r2 . unp2
|
|
|
|
|
|
|
|
|
|
|
|
-- |Give the point which is at the coordinates the vector
|
|
|
|
-- points to from the origin.
|
2015-01-14 17:17:35 +00:00
|
|
|
vec2Pt :: R2 -> P2
|
2014-10-08 14:31:57 +00:00
|
|
|
vec2Pt = p2 . unr2
|
|
|
|
|
|
|
|
|
|
|
|
-- |Construct a vector between two points.
|
2015-01-14 17:17:35 +00:00
|
|
|
vp2 :: P2 -- ^ vector origin
|
|
|
|
-> P2 -- ^ vector points here
|
|
|
|
-> R2
|
2014-10-13 17:14:25 +00:00
|
|
|
vp2 a b = pt2Vec b - pt2Vec a
|
2014-10-08 14:31:57 +00:00
|
|
|
|
|
|
|
|
2014-10-12 18:37:24 +00:00
|
|
|
-- |Computes the determinant of 3 points.
|
2015-01-14 17:17:35 +00:00
|
|
|
det :: P2 -> P2 -> P2 -> Double
|
2014-12-17 23:31:43 +00:00
|
|
|
det (coords -> ax :& ay) (coords -> bx :& by) (coords -> cx :& cy) =
|
|
|
|
(bx - ax) * (cy - ay) - (by - ay) * (cx - ax)
|
2014-10-12 18:37:24 +00:00
|
|
|
|
|
|
|
|
2014-10-24 23:44:13 +00:00
|
|
|
-- |Get the point where two lines intesect, if any.
|
2015-01-14 17:17:35 +00:00
|
|
|
intersectSeg' :: (P2, P2) -- ^ first segment
|
|
|
|
-> (P2, P2) -- ^ second segment
|
|
|
|
-> Maybe P2
|
2014-10-24 23:44:13 +00:00
|
|
|
intersectSeg' (a, b) (c, d) =
|
|
|
|
glossToPt <$> intersectSegSeg (ptToGloss a)
|
|
|
|
(ptToGloss b)
|
|
|
|
(ptToGloss c)
|
|
|
|
(ptToGloss d)
|
|
|
|
where
|
2014-12-13 03:05:28 +00:00
|
|
|
ptToGloss = (double2Float *** double2Float) <$> unp2
|
|
|
|
glossToPt = p2 . (float2Double *** float2Double)
|
2014-10-24 23:44:13 +00:00
|
|
|
|
|
|
|
|
2015-01-08 00:39:39 +00:00
|
|
|
-- |Get the point where two lines intesect, if any. Excludes the
|
|
|
|
-- case of end-points intersecting.
|
2015-01-14 17:17:35 +00:00
|
|
|
intersectSeg'' :: (P2, P2) -> (P2, P2) -> Maybe P2
|
2015-01-08 00:39:39 +00:00
|
|
|
intersectSeg'' (a, b) (c, d) = case intersectSeg' (a, b) (c, d) of
|
|
|
|
Just x -> if x `notElem` [a,b,c,d] then Just a else Nothing
|
|
|
|
Nothing -> Nothing
|
|
|
|
|
|
|
|
|
2014-10-12 18:37:24 +00:00
|
|
|
-- |Get the orientation of 3 points which can either be
|
|
|
|
-- * clock-wise
|
|
|
|
-- * counter-clock-wise
|
|
|
|
-- * collinear
|
2015-01-14 17:17:35 +00:00
|
|
|
getOrient :: P2 -> P2 -> P2 -> Alignment
|
2014-10-12 18:37:24 +00:00
|
|
|
getOrient a b c = case compare (det a b c) 0 of
|
2014-10-13 00:30:11 +00:00
|
|
|
LT -> CW
|
|
|
|
GT -> CCW
|
2014-10-12 18:37:24 +00:00
|
|
|
EQ -> CL
|
|
|
|
|
|
|
|
|
|
|
|
--- |Checks if 3 points a,b,c do not build a clockwise triangle by
|
|
|
|
--- connecting a-b-c. This is done by computing the determinant and
|
|
|
|
--- checking the algebraic sign.
|
2015-01-14 17:17:35 +00:00
|
|
|
notcw :: P2 -> P2 -> P2 -> Bool
|
2014-10-12 18:37:24 +00:00
|
|
|
notcw a b c = case getOrient a b c of
|
|
|
|
CW -> False
|
|
|
|
_ -> True
|
2014-10-13 20:06:12 +00:00
|
|
|
|
|
|
|
|
2015-01-07 17:55:16 +00:00
|
|
|
--- |Checks if 3 points a,b,c do build a clockwise triangle by
|
|
|
|
--- connecting a-b-c. This is done by computing the determinant and
|
|
|
|
--- checking the algebraic sign.
|
2015-01-14 17:17:35 +00:00
|
|
|
cw :: P2 -> P2 -> P2 -> Bool
|
2015-01-07 17:55:16 +00:00
|
|
|
cw a b c = not . notcw a b $ c
|
|
|
|
|
|
|
|
|
2014-10-13 20:06:12 +00:00
|
|
|
-- |Sort X and Y coordinates lexicographically.
|
2015-01-14 17:17:35 +00:00
|
|
|
sortedXY :: [P2] -> [P2]
|
2014-10-13 20:06:12 +00:00
|
|
|
sortedXY = fmap p2 . sortLex . fmap unp2
|
2014-10-24 23:44:13 +00:00
|
|
|
|
|
|
|
|
2015-01-08 00:39:23 +00:00
|
|
|
-- |Sort Y and X coordinates lexicographically.
|
2015-01-14 17:17:35 +00:00
|
|
|
sortedYX :: [P2] -> [P2]
|
2015-01-08 00:39:23 +00:00
|
|
|
sortedYX = fmap p2 . sortLexSwapped . fmap unp2
|
|
|
|
|
|
|
|
|
2014-11-29 04:08:54 +00:00
|
|
|
-- |Sort all points according to their X-coordinates only.
|
2015-01-14 17:17:35 +00:00
|
|
|
sortedX :: [P2] -> [P2]
|
2014-11-29 04:08:54 +00:00
|
|
|
sortedX xs =
|
|
|
|
fmap p2
|
|
|
|
. sortBy (\(a1, _) (a2, _) -> compare a1 a2)
|
|
|
|
$ fmap unp2 xs
|
|
|
|
|
|
|
|
|
|
|
|
-- |Sort all points according to their Y-coordinates only.
|
2015-01-14 17:17:35 +00:00
|
|
|
sortedY :: [P2] -> [P2]
|
2014-11-29 04:08:54 +00:00
|
|
|
sortedY xs =
|
|
|
|
fmap p2
|
|
|
|
. sortBy (\(_, b1) (_, b2) -> compare b1 b2)
|
|
|
|
$ fmap unp2 xs
|
|
|
|
|
|
|
|
|
2014-10-24 23:44:13 +00:00
|
|
|
-- |Apply a function on the coordinates of a point.
|
2015-01-14 17:17:35 +00:00
|
|
|
onPT :: ((Double, Double) -> (Double, Double)) -> P2 -> P2
|
2014-10-24 23:44:13 +00:00
|
|
|
onPT f = p2 . f . unp2
|
|
|
|
|
|
|
|
|
|
|
|
-- |Compare the y-coordinate of two points.
|
2015-01-14 17:17:35 +00:00
|
|
|
ptCmpY :: P2 -> P2 -> Ordering
|
2014-12-17 23:31:43 +00:00
|
|
|
ptCmpY (coords -> _ :& y1) (coords -> _ :& y2) =
|
|
|
|
compare y1 y2
|
2014-10-24 23:44:13 +00:00
|
|
|
|
|
|
|
|
2014-10-25 12:50:09 +00:00
|
|
|
-- |Compare the x-coordinate of two points.
|
2015-01-14 17:17:35 +00:00
|
|
|
ptCmpX :: P2 -> P2 -> Ordering
|
2014-12-17 23:31:43 +00:00
|
|
|
ptCmpX (coords -> x1 :& _) (coords -> x2 :& _) =
|
|
|
|
compare x1 x2
|
2014-10-24 23:44:13 +00:00
|
|
|
|
|
|
|
|
2015-01-14 17:17:35 +00:00
|
|
|
posInfPT :: P2
|
2014-10-24 23:44:13 +00:00
|
|
|
posInfPT = p2 (read "Infinity", read "Infinity")
|
|
|
|
|
|
|
|
|
2015-01-14 17:17:35 +00:00
|
|
|
negInfPT :: P2
|
2014-10-24 23:44:13 +00:00
|
|
|
negInfPT = p2 (negate . read $ "Infinity", negate . read $ "Infinity")
|