Debian Codegraph @

src/Diagrams/TwoD/Types.hs

raw symbols SCIP
1 {-# LANGUAGE TypeFamilies #-}
2
3 {-# OPTIONS_GHC -fno-warn-orphans #-}
4
5 -----------------------------------------------------------------------------
6 -- |
7 -- Module : Diagrams.TwoD.Types
8 -- Copyright : (c) 2011 diagrams-lib team (see LICENSE)
9 -- License : BSD-style (see LICENSE)
10 -- Maintainer : [email protected]
11 --
12 -- Basic types for two-dimensional Euclidean space.
13 --
14 -----------------------------------------------------------------------------
15
16 module Diagrams.TwoD.Types
17 ( -- * 2D Euclidean space
18 V2 (..), R1 (..), R2 (..)
19 , P2, T2
20 , r2, unr2, mkR2, r2Iso
21 , p2, mkP2, unp2, p2Iso
22 , r2PolarIso
23 , HasR (..)
24 ) where
25
26 import Control.Lens (Iso', Lens', iso, _1, _2)
27
28 import Diagrams.Angle
29 import Diagrams.Points
30
31 import Diagrams.Core.Transform
32 import Diagrams.Core.V
33 import Linear.Metric
34 import Linear.V2
35
36 type P2 = Point V2
37 type T2 = Transformation V2
38
39 type instance V (V2 n) = V2
40 type instance N (V2 n) = n
41
42 -- | Construct a 2D vector from a pair of components. See also '&'.
43 r2 :: (n, n) -> V2 n
44 r2 = uncurry V2
45
46 -- | Convert a 2D vector back into a pair of components. See also 'coords'.
47 unr2 :: V2 n -> (n, n)
48 unr2 (V2 x y) = (x, y)
49
50 -- | Curried form of `r2`.
51 mkR2 :: n -> n -> V2 n
52 mkR2 = V2
53
54 r2Iso :: Iso' (V2 n) (n, n)
55 r2Iso = iso unr2 r2
56
57 -- | Construct a 2D point from a pair of coordinates. See also '^&'.
58 p2 :: (n, n) -> P2 n
59 p2 = P . uncurry V2
60
61 -- | Convert a 2D point back into a pair of coordinates. See also 'coords'.
62 unp2 :: P2 n -> (n,n)
63 unp2 (P (V2 x y)) = (x,y)
64
65 -- | Curried form of `p2`.
66 mkP2 :: n -> n -> P2 n
67 mkP2 x y = P (V2 x y)
68
69 p2Iso :: Iso' (Point V2 n) (n, n)
70 p2Iso = iso unp2 p2
71
72 instance Transformable (V2 n) where
73 transform = apply
74
75 r2PolarIso :: RealFloat n => Iso' (V2 n) (n, Angle n)
76 r2PolarIso = iso (\v@(V2 x y) -> (norm v, atan2A y x))
77 (\(r,θ) -> V2 (r * cosA θ) (r * sinA θ))
78 {-# INLINE r2PolarIso #-}
79
80 -- | A space which has magnitude '_r' that can be calculated numerically.
81 class HasR t where
82 _r :: RealFloat n => Lens' (t n) n
83
84 instance HasR v => HasR (Point v) where
85 _r = lensP . _r
86 {-# INLINE _r #-}
87
88 instance HasR V2 where
89 _r = r2PolarIso . _1
90 {-# INLINE _r #-}
91
92 instance HasTheta V2 where
93 _theta = r2PolarIso . _2
94 {-# INLINE _theta #-}