1    type Vector2<T> = struct
2        x : T
3        y : T
4    
5    type Vector3<T> = struct
6        x : T
7        y : T
8        z : T
9    
10   type Vector4<T> = struct
11       x : T
12       y : T
13       z : T
14       w : T
15   
16   type X<T> where T : Number = T
17   
18   type Vector3<T> where T : Number
19       def (+) rhs = Vector3 (x + rhs.x) (y + rhs.y) (z + rhs.z)
20       def (-) rhs = Vector3 (x - rhs.x) (y - rhs.y) (z - rhs.z)
21       def (*) rhs = Vector3 (x * rhs.x) (y * rhs.y) (z * rhs.z)
22       def (/) rhs = Vector3 (x / rhs.x) (y / rhs.y) (z / rhs.z)
23   
24       def (*) (rhs : T) = Vector3 (x * rhs) (y * rhs) (z * rhs)
25       def (/) (rhs : T) = Vector3 (x / rhs) (y / rhs) (z / rhs)
26   
27       def (+) (X<T> rhs) = Vector3 (x + rhs) y z
28   
29       def dot (rhs : Vector3<T>) = x * rhs.x + y * rhs.y + z * rhs.z
30   
31       def cross (rhs : Vector3<T>) = Vector3 (y * rhs.z - z * rhs.y)
32                                              (z * rhs.x - x * rhs.z)
33                                              (x * rhs.y - y * rhs.x)
34   
35       def with_x self (x : T) = Vector3 x self.y self.z
36       def with_y self (y : T) = Vector3 self.x y self.z
37       def with_z self (z : T) = Vector3 self.x self.y z
38   
39   type Vector4<T> where T : Number
40       def (+) rhs = Vector4 (x + rhs.x) (y + rhs.y) (z + rhs.z) (w + rhs.w)
41       def (-) rhs = Vector4 (x - rhs.x) (y - rhs.y) (z - rhs.z) (w - rhs.w)
42       def (*) rhs = Vector4 (x * rhs.x) (y * rhs.y) (z * rhs.z) (w * rhs.w)
43       def (/) rhs = Vector4 (x / rhs.x) (y / rhs.y) (z / rhs.z) (w / rhs.w)
44       def (*) (rhs : T) = Vector4 (x * rhs) (y * rhs) (z * rhs) (w * rhs)
45       def (/) (rhs : T) = Vector4 (x / rhs) (y / rhs) (z / rhs) (w / rhs)
46   
47   type Vector3<T>
48       def with_w (w : T) = Vector4 x y z w
49   
50   type Vector3<T> where T : Real
51       def norm = x * x + y * y + z * z |> sqrt
52       def normalized = self / norm
53   
54   type Vector4<T> where T : Real
55       def norm = x * x + y * y + z * z + w * w |> sqrt
56       def normalized = self / norm
57   
58   object Vector3<T> where T : Real =
59       def color (s : String) = Vector3<T>
60           x = u32.parse (s.substring 0 2) 16 |> as<T> / 255
61           y = u32.parse (s.substring 2 4) 16 |> as<T> / 255
62           z = u32.parse (s.substring 4 6) 16 |> as<T> / 255
63   
64       def gray (x : T) = Vector3<T> x x x
65   
66   typealias ByteVector4 = Vector4<u8>
67   
68   module vector_i32
69   
70   typealias Vector2i = Vector2<i32>
71   typealias Vector3i = Vector3<i32>
72   
73   module vector_f32
74   
75   typealias Vector2 = Vector2<f32>
76   typealias Vector3 = Vector3<f32>
77   typealias Vector4 = Vector4<f32>
78