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.
9 import Prelude hiding (LT)
11 data Cardinal = F -- ^ Front
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
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
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
55 x - y = Difference x y
57 negate x = Product (Scalar (-1)) x
60 fromInteger x = Scalar (fromIntegral x)
63 -- | Like the Num instance, the Fractional instance allows us to
64 -- take quotients of directions.
65 instance Fractional Cardinal where
67 recip x = Quotient (Scalar 1) x
68 fromRational x = Scalar (fromRational x)
71 -- | Rotate a cardinal direction counter-clockwise about the x-axis.
72 ccwx :: Cardinal -> Cardinal
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)
106 -- | Rotate a cardinal direction clockwise about the x-axis.
107 cwx :: Cardinal -> Cardinal
108 cwx = ccwx . ccwx . ccwx
111 -- | Rotate a cardinal direction counter-clockwise about the y-axis.
112 ccwy :: Cardinal -> Cardinal
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)
146 -- | Rotate a cardinal direction clockwise about the y-axis.
147 cwy :: Cardinal -> Cardinal
148 cwy = ccwy . ccwy . ccwy
151 -- | Rotate a cardinal direction counter-clockwise about the z-axis.
152 ccwz :: Cardinal -> Cardinal
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)
186 -- | Rotate a cardinal direction clockwise about the z-axis.
187 cwz :: Cardinal -> Cardinal
188 cwz = ccwz . ccwz . ccwz