1 | #pragma once |
---|
2 | |
---|
3 | #include <iostream.hfa> |
---|
4 | #include "vec.hfa" |
---|
5 | |
---|
6 | forall (otype T) { |
---|
7 | struct vec2 { |
---|
8 | T x, y; |
---|
9 | }; |
---|
10 | } |
---|
11 | |
---|
12 | |
---|
13 | forall (otype T) { |
---|
14 | static inline { |
---|
15 | |
---|
16 | void ?{}(vec2(T)& v, T x, T y) { |
---|
17 | v.[x, y] = [x, y]; |
---|
18 | } |
---|
19 | |
---|
20 | forall(| zero_assign(T)) |
---|
21 | void ?{}(vec2(T)& vec, zero_t) with (vec) { |
---|
22 | x = y = 0; |
---|
23 | } |
---|
24 | |
---|
25 | void ?{}(vec2(T)& vec, T val) with (vec) { |
---|
26 | x = y = val; |
---|
27 | } |
---|
28 | |
---|
29 | void ?{}(vec2(T)& vec, vec2(T) other) with (vec) { |
---|
30 | [x,y] = other.[x,y]; |
---|
31 | } |
---|
32 | |
---|
33 | // Assignment |
---|
34 | void ?=?(vec2(T)& vec, vec2(T) other) with (vec) { |
---|
35 | [x,y] = other.[x,y]; |
---|
36 | } |
---|
37 | forall(| zero_assign(T)) |
---|
38 | void ?=?(vec2(T)& vec, zero_t) with (vec) { |
---|
39 | x = y = 0; |
---|
40 | } |
---|
41 | |
---|
42 | // Primitive mathematical operations |
---|
43 | |
---|
44 | // Subtraction |
---|
45 | |
---|
46 | forall(| subtract(T)) { |
---|
47 | vec2(T) ?-?(vec2(T) u, vec2(T) v) { // TODO( can't make this const ref ) |
---|
48 | return [u.x - v.x, u.y - v.y]; |
---|
49 | } |
---|
50 | vec2(T)& ?-=?(vec2(T)& u, vec2(T) v) { |
---|
51 | u = u - v; |
---|
52 | return u; |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | forall(| negate(T)) { |
---|
57 | vec2(T) -?(vec2(T) v) with (v) { |
---|
58 | return [-x, -y]; |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | // Addition |
---|
63 | forall(| add(T)) { |
---|
64 | vec2(T) ?+?(vec2(T) u, vec2(T) v) { // TODO( can't make this const ref ) |
---|
65 | return [u.x + v.x, u.y + v.y]; |
---|
66 | } |
---|
67 | vec2(T)& ?+=?(vec2(T)& u, vec2(T) v) { |
---|
68 | u = u + v; |
---|
69 | return u; |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | // Scalar Multiplication |
---|
74 | forall(| multiply(T)) { |
---|
75 | vec2(T) ?*?(vec2(T) v, T scalar) with (v) { // TODO (can't make this const ref) |
---|
76 | return [x * scalar, y * scalar]; |
---|
77 | } |
---|
78 | vec2(T) ?*?(T scalar, vec2(T) v) { // TODO (can't make this const ref) |
---|
79 | return v * scalar; |
---|
80 | } |
---|
81 | vec2(T)& ?*=?(vec2(T)& v, T scalar) { |
---|
82 | v = v * scalar; |
---|
83 | return v; |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | // Scalar Division |
---|
88 | forall(| divide(T)) { |
---|
89 | vec2(T) ?/?(vec2(T) v, T scalar) with (v) { |
---|
90 | return [x / scalar, y / scalar]; |
---|
91 | } |
---|
92 | vec2(T)& ?/=?(vec2(T)& v, T scalar) with (v) { |
---|
93 | v = v / scalar; |
---|
94 | return v; |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | // Relational Operators |
---|
99 | forall(| equality(T)) { |
---|
100 | bool ?==?(vec2(T) u, vec2(T) v) with (u) { |
---|
101 | return x == v.x && y == v.y; |
---|
102 | } |
---|
103 | bool ?!=?(vec2(T) u, vec2(T) v) { |
---|
104 | return !(u == v); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | // Geometric functions |
---|
109 | forall(| add(T) | multiply(T)) |
---|
110 | T dot(vec2(T) u, vec2(T) v) { |
---|
111 | return u.x * v.x + u.y * v.y; |
---|
112 | } |
---|
113 | |
---|
114 | } // static inline |
---|
115 | } |
---|
116 | |
---|
117 | forall(dtype ostype, otype T | writeable(T, ostype)) { |
---|
118 | ostype & ?|?(ostype & os, vec2(T) v) with (v) { |
---|
119 | return os | '<' | x | ',' | y | '>'; |
---|
120 | } |
---|
121 | void ?|?(ostype & os, vec2(T) v ) with (v) { |
---|
122 | (ostype &)(os | v); ends(os); |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|