]> gitweb.michael.orlitzky.com - spline3.git/blob - src/Cardinal.hs
Add a bunch of documentation.
[spline3.git] / src / Cardinal.hs
1 -- | The Cardinal module contains the Cardinal data type, representing
2 -- a cardinal direction (one of the 27 directions surrounding the
3 -- center of a cube. In addition to those 27 directions, we also
4 -- include the interior point and a number of composite types that
5 -- allow us to perform arithmetic on directions.
6 module Cardinal
7 where
8
9 import Prelude hiding (LT)
10
11 data Cardinal = F -- ^ Front
12 | B -- ^ Back
13 | L -- ^ Left
14 | R -- ^ Right
15 | D -- ^ Down
16 | T -- ^ Top
17 | FL -- ^ Front Left
18 | FR -- ^ Front Right
19 | FD -- ^ Front Down
20 | FT -- ^ Front Top
21 | BL -- ^ Back Left
22 | BR -- ^ Back Right
23 | BD -- ^ Back Down
24 | BT -- ^ Back Top
25 | LD -- ^ Left Down
26 | LT -- ^ Left Top
27 | RD -- ^ Right Down
28 | RT -- ^ Right Top
29 | FLD -- ^ Front Left Down
30 | FLT -- ^ Front Left Top
31 | FRD -- ^ Front Right Down
32 | FRT -- ^ Front Right Top
33 | BLD -- ^ Back Left Down
34 | BLT -- ^ Back Left Top
35 | BRD -- ^ Back Right Down
36 | BRT -- ^ Back Right Top
37 | I -- ^ Interior
38 | Scalar Double -- ^ A wrapper around a scalar value.
39 | Sum Cardinal Cardinal -- ^ The sum of two directions.
40 | Difference Cardinal Cardinal
41 -- ^ The difference of two directions, the first minus the second.
42 | Product Cardinal Cardinal -- ^ The product of two directions.
43 | Quotient Cardinal Cardinal
44 -- ^ The quotient of two directions, the first divided by the
45 -- second.
46 deriving (Show, Eq)
47
48
49 -- | By making Cardinal an instance of 'Num', we gain the ability to
50 -- add, subtract, and multiply directions. The results of these
51 -- operations are never actually calculated; the types just keep
52 -- track of which operations were performed in which order.
53 instance Num Cardinal where
54 x + y = Sum x y
55 x - y = Difference x y
56 x * y = Product x y
57 negate = Product (Scalar (-1))
58 abs x = x
59 signum x = x
60 fromInteger x = Scalar (fromIntegral x)
61
62
63 -- | Like the Num instance, the Fractional instance allows us to
64 -- take quotients of directions.
65 instance Fractional Cardinal where
66 x / y = Quotient x y
67 recip = Quotient (Scalar 1)
68 fromRational x = Scalar (fromRational x)
69
70
71 -- | Rotate a cardinal direction counter-clockwise about the x-axis.
72 ccwx :: Cardinal -> Cardinal
73 ccwx F = F
74 ccwx B = B
75 ccwx L = T
76 ccwx R = D
77 ccwx D = L
78 ccwx T = R
79 ccwx FL = FT
80 ccwx FR = FD
81 ccwx FD = FL
82 ccwx FT = FR
83 ccwx BL = BT
84 ccwx BR = BD
85 ccwx BD = BL
86 ccwx BT = BR
87 ccwx LD = LT
88 ccwx LT = RT
89 ccwx RD = LD
90 ccwx RT = RD
91 ccwx FLD = FLT
92 ccwx FLT = FRT
93 ccwx FRD = FLD
94 ccwx FRT = FRD
95 ccwx BLD = BLT
96 ccwx BLT = BRT
97 ccwx BRD = BLD
98 ccwx BRT = BRD
99 ccwx I = I
100 ccwx (Scalar s) = (Scalar s)
101 ccwx (Sum c0 c1) = Sum (ccwx c0) (ccwx c1)
102 ccwx (Difference c0 c1) = Difference (ccwx c0) (ccwx c1)
103 ccwx (Product c0 c1) = Product (ccwx c0) (ccwx c1)
104 ccwx (Quotient c0 c1) = Quotient (ccwx c0) (ccwx c1)
105
106 -- | Rotate a cardinal direction clockwise about the x-axis.
107 cwx :: Cardinal -> Cardinal
108 cwx = ccwx . ccwx . ccwx
109
110
111 -- | Rotate a cardinal direction counter-clockwise about the y-axis.
112 ccwy :: Cardinal -> Cardinal
113 ccwy F = D
114 ccwy B = T
115 ccwy L = L
116 ccwy R = R
117 ccwy D = B
118 ccwy T = F
119 ccwy FL = LD
120 ccwy FR = RD
121 ccwy FD = BD
122 ccwy FT = FD
123 ccwy BL = LT
124 ccwy BR = RT
125 ccwy BD = BT
126 ccwy BT = FT
127 ccwy LD = BL
128 ccwy LT = FL
129 ccwy RD = BR
130 ccwy RT = FR
131 ccwy FLD = BLD
132 ccwy FLT = FLD
133 ccwy FRD = BRD
134 ccwy FRT = FRD
135 ccwy BLD = BLT
136 ccwy BLT = FLT
137 ccwy BRD = BRT
138 ccwy BRT = FRT
139 ccwy I = I
140 ccwy (Scalar s) = (Scalar s)
141 ccwy (Sum c0 c1) = Sum (ccwy c0) (ccwy c1)
142 ccwy (Difference c0 c1) = Difference (ccwy c0) (ccwy c1)
143 ccwy (Product c0 c1) = Product (ccwy c0) (ccwy c1)
144 ccwy (Quotient c0 c1) = Quotient (ccwy c0) (ccwy c1)
145
146 -- | Rotate a cardinal direction clockwise about the y-axis.
147 cwy :: Cardinal -> Cardinal
148 cwy = ccwy . ccwy . ccwy
149
150
151 -- | Rotate a cardinal direction counter-clockwise about the z-axis.
152 ccwz :: Cardinal -> Cardinal
153 ccwz F = L
154 ccwz B = R
155 ccwz L = B
156 ccwz R = F
157 ccwz D = D
158 ccwz T = T
159 ccwz FL = BL
160 ccwz FR = FL
161 ccwz FD = LD
162 ccwz FT = LT
163 ccwz BL = BR
164 ccwz BR = FR
165 ccwz BD = RD
166 ccwz BT = RT
167 ccwz LD = BD
168 ccwz LT = BT
169 ccwz RD = FD
170 ccwz RT = FT
171 ccwz FLD = BLD
172 ccwz FLT = BLT
173 ccwz FRD = FLD
174 ccwz FRT = FLT
175 ccwz BLD = BRD
176 ccwz BLT = BRT
177 ccwz BRD = FRD
178 ccwz BRT = FRT
179 ccwz I = I
180 ccwz (Scalar s) = (Scalar s)
181 ccwz (Sum c0 c1) = Sum (ccwz c0) (ccwz c1)
182 ccwz (Difference c0 c1) = Difference (ccwz c0) (ccwz c1)
183 ccwz (Product c0 c1) = Product (ccwz c0) (ccwz c1)
184 ccwz (Quotient c0 c1) = Quotient (ccwz c0) (ccwz c1)
185
186 -- | Rotate a cardinal direction clockwise about the z-axis.
187 cwz :: Cardinal -> Cardinal
188 cwz = ccwz . ccwz . ccwz