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