Debian Codegraph @

src/Diagrams/TwoD/Vector.hs

raw symbols SCIP
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module : Diagrams.TwoD.Vector
4 -- Copyright : (c) 2011 diagrams-lib team (see LICENSE)
5 -- License : BSD-style (see LICENSE)
6 -- Maintainer : [email protected]
7 --
8 -- Two-dimensional vectors.
9 --
10 -----------------------------------------------------------------------------
11 module Diagrams.TwoD.Vector
12 ( -- * Special 2D vectors
13 unitX, unitY, unit_X, unit_Y
14 , xDir, yDir
15
16 -- * Converting between vectors and angles
17 , angleV, angleDir, e, signedAngleBetween, signedAngleBetweenDirs
18
19 -- * 2D vector utilities
20 , perp, leftTurn, cross2
21
22 ) where
23
24 import Control.Lens (view, (&), (.~), (^.))
25
26 import Diagrams.Angle
27 import Diagrams.Direction
28 import Diagrams.TwoD.Types
29
30 import Linear.Metric
31 import Linear.V2
32 import Linear.Vector
33
34 -- | The unit vector in the positive X direction.
35 unitX :: (R1 v, Additive v, Num n) => v n
36 unitX = zero & _x .~ 1
37
38 -- | The unit vector in the negative X direction.
39 unit_X :: (R1 v, Additive v, Num n) => v n
40 unit_X = zero & _x .~ (-1)
41
42 -- | The unit vector in the positive Y direction.
43 unitY :: (R2 v, Additive v, Num n) => v n
44 unitY = zero & _y .~ 1
45
46 -- | The unit vector in the negative Y direction.
47 unit_Y :: (R2 v, Additive v, Num n) => v n
48 unit_Y = zero & _y .~ (-1)
49
50 -- | A 'Direction' pointing in the X direction.
51 xDir :: (R1 v, Additive v, Num n) => Direction v n
52 xDir = dir unitX
53
54 -- | A 'Direction' pointing in the Y direction.
55 yDir :: (R2 v, Additive v, Num n) => Direction v n
56 yDir = dir unitY
57
58 -- | A direction at a specified angle counter-clockwise from the 'xDir'.
59 angleDir :: Floating n => Angle n -> Direction V2 n
60 angleDir = dir . angleV
61
62 -- | A unit vector at a specified angle counter-clockwise from the
63 -- positive x-axis
64 angleV :: Floating n => Angle n -> V2 n
65 angleV = angle . view rad
66
67 -- | A unit vector at a specified angle counter-clockwise from the
68 -- positive X axis.
69 e :: Floating n => Angle n -> V2 n
70 e = angleV
71
72 -- | @leftTurn v1 v2@ tests whether the direction of @v2@ is a left
73 -- turn from @v1@ (that is, if the direction of @v2@ can be obtained
74 -- from that of @v1@ by adding an angle 0 <= theta <= tau/2).
75 leftTurn :: (Num n, Ord n) => V2 n -> V2 n -> Bool
76 leftTurn v1 v2 = (v1 `dot` perp v2) < 0
77
78 -- | Cross product on vectors in R2.
79 cross2 :: Num n => V2 n -> V2 n -> n
80 cross2 (V2 x1 y1) (V2 x2 y2) = x1 * y2 - y1 * x2
81
82 -- | Signed angle between two vectors. Currently defined as
83 --
84 -- @
85 -- signedAngleBetween u v = (u ^. _theta) ^-^ (v ^. _theta)
86 -- @
87 signedAngleBetween :: RealFloat n => V2 n -> V2 n -> Angle n
88 signedAngleBetween u v = (u ^. _theta) ^-^ (v ^. _theta)
89
90 -- | Same as 'signedAngleBetween' but for 'Directions's.
91 signedAngleBetweenDirs :: RealFloat n => Direction V2 n -> Direction V2 n -> Angle n
92 signedAngleBetweenDirs u v = (u ^. _theta) ^-^ (v ^. _theta)