-
Notifications
You must be signed in to change notification settings - Fork 1
/
R2.hs
45 lines (31 loc) · 758 Bytes
/
R2.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module R2 where
type R = Double
type R2 = (R,R)
type Z2 = (Int, Int)
type N2 = Z2
(|+|) :: R2 -> R2 -> R2
(a,b) |+| (c,d) = (a+c, b+d)
(|-|) :: R2 -> R2 -> R2
(a,b) |-| (c,d) = (a-c, b-d)
(|*) :: R2 -> Double -> R2
(a,b) |* s = (a*s, b*s)
(*|) :: Double -> R2 -> R2
xy *| s = s |* xy
(|/) :: R2 -> Double -> R2
(a,b) |/ s = (a/s, b/s)
norm :: R2 -> Double
norm (a,b) = sqrt (a*a + b*b)
(.+.) :: Z2 -> Z2 -> Z2
(a,b) .+. (c,d) = (a+c, b+d)
(.-.) :: Z2 -> Z2 -> Z2
(a,b) .-. (c,d) = (a-c, b-d)
(.*) :: Z2 -> Int -> Z2
(a,b) .* s = (a*s, b*s)
(*.) :: Int -> Z2 -> Z2
xy *. s = s .* xy
normZ2 :: Z2 -> Double
normZ2 = norm . fromZ2
fromR2 :: R2 -> Z2
fromR2 (a,b) = (floor a, floor b)
fromZ2 :: Z2 -> R2
fromZ2 (a,b) = (realToFrac a, realToFrac b)