-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vec3.hs
57 lines (43 loc) · 815 Bytes
/
Vec3.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
46
47
48
49
50
51
52
53
54
55
56
57
module Vec3 where
--
-- Construct a 3D vector.
--
-- TODO: Revisit once I learn how to access tuple elements.
--
new x y z =
(x, y, z)
--
-- Compute the magnitude of a vector.
--
-- Because of lazy evaluation, you should beta-reduce the `$` first.
--
magnitude x y z =
sqrt $ x^2 + y^2 + z^2
--
-- Normalize a vector.
--
normalize x y z =
let m = magnitude x y z
in (x/m, y/m, z/m)
--
-- Normalize a vector, but using a `where` clause.
--
-- (I like the first one more.)
--
normalize2 x y z = (x/m, y/m, z/m)
where m = magnitude x y z
--
-- Construct a zero vector.
--
zero =
(0, 0, 0)
--
-- Scale a vector by some factor `s`.
--
scale x y z s =
(s*x, s*y, s*z)
--
-- Compute the cross product of two vectors.
--
cross ax ay az bx by bz =
(ay*bz - az*by, az*bx - ax*bz, ax*by - ay*bx)