remove +x from source
[Tsunagari.git] / src / vec.h
blob76e058f7fcd49e6159bba9dfa04607b141e36997
1 /*********************************
2 ** Tsunagari Tile Engine **
3 ** vec.h **
4 ** Copyright 2011-2012 OmegaSDG **
5 *********************************/
7 #ifndef VEC_H
8 #define VEC_H
10 /**
11 * Virtual integer coordinate.
13 * x and y are the same as a physical integer coordinate.
14 * z is a virtual layer depth within an Area.
16 struct vicoord
18 vicoord() {}
19 vicoord(int x, int y, double z): x(x), y(y), z(z) {}
21 int x, y;
22 double z;
25 struct icube {
26 icube(int x1, int y1, int z1, int x2, int y2, int z2)
27 : x1(x1), y1(y1), z1(z1), x2(x2), y2(y2), z2(z2) {}
29 int x1, y1, z1;
30 int x2, y2, z2;
33 template<class T>
34 class vec2
36 public:
37 T x, y;
39 vec2()
43 vec2(const vec2<T>& other)
44 : x(other.x), y(other.y)
48 vec2(T x, T y)
49 : x(x), y(y)
53 vec2<T>& operator +=(const vec2<T>& other)
55 x += other.x;
56 y += other.y;
57 return *this;
60 vec2<T>& operator -=(const vec2<T>& other)
62 x -= other.x;
63 y -= other.y;
64 return *this;
67 vec2<T>& operator *=(const vec2<T>& other)
69 x *= other.x;
70 y *= other.y;
71 return *this;
74 vec2<T>& operator *=(T coefficient)
76 x *= coefficient;
77 y *= coefficient;
78 return *this;
81 vec2<T>& operator /=(const vec2<T>& other)
83 x /= other.x;
84 y /= other.y;
85 return *this;
88 vec2<T>& operator /=(T coefficient)
90 x /= coefficient;
91 y /= coefficient;
92 return *this;
95 operator bool()
97 return x || y;
102 template<class T>
103 class vec3
105 public:
106 T x, y, z;
108 vec3()
112 vec3(const vec3<T>& other)
113 : x(other.x), y(other.y), z(other.z)
117 vec3(T x, T y, T z)
118 : x(x), y(y), z(z)
122 vec3<T>& operator +=(const vec3<T>& other)
124 x += other.x;
125 y += other.y;
126 z += other.z;
127 return *this;
130 vec3<T>& operator -=(const vec3<T>& other)
132 x -= other.x;
133 y -= other.y;
134 z -= other.z;
135 return *this;
138 vec3<T>& operator *=(const vec3<T>& other)
140 x *= other.x;
141 y *= other.y;
142 z *= other.z;
143 return *this;
146 vec3<T>& operator *=(T coefficient)
148 x *= coefficient;
149 y *= coefficient;
150 z *= coefficient;
151 return *this;
154 vec3<T>& operator /=(const vec3<T>& other)
156 x /= other.x;
157 y /= other.y;
158 z /= other.z;
159 return *this;
162 vec3<T>& operator /=(T coefficient)
164 x /= coefficient;
165 y /= coefficient;
166 z /= coefficient;
167 return *this;
170 operator bool()
172 return x || y || z;
176 template<class T>
177 vec2<T> operator+(const vec2<T>& a, const vec2<T>& b)
179 vec2<T> c;
180 c.x = a.x + b.x;
181 c.y = a.y + b.y;
182 return c;
185 template<class T>
186 vec3<T> operator+(const vec3<T>& a, const vec3<T>& b)
188 vec3<T> c;
189 c.x = a.x + b.x;
190 c.y = a.y + b.y;
191 c.z = a.z + b.z;
192 return c;
195 template<class T>
196 vec2<T> operator-(const vec2<T>& a, const vec2<T>& b)
198 vec2<T> c;
199 c.x = a.x - b.x;
200 c.y = a.y - b.y;
201 return c;
204 template<class T>
205 vec3<T> operator-(const vec3<T>& a, const vec3<T>& b)
207 vec3<T> c;
208 c.x = a.x - b.x;
209 c.y = a.y - b.y;
210 c.z = a.z - b.z;
211 return c;
214 template<class T>
215 vec2<T> operator*(const vec2<T>& a, const vec2<T>& b)
217 vec2<T> c;
218 c.x = a.x * b.x;
219 c.y = a.y * b.y;
220 return c;
223 template<class T>
224 vec3<T> operator*(const vec3<T>& a, const vec3<T>& b)
226 vec3<T> c;
227 c.x = a.x * b.x;
228 c.y = a.y * b.y;
229 c.z = a.z * b.z;
230 return c;
233 template<class T, class CO>
234 vec2<T> operator*(const vec2<T>& a, CO co)
236 vec2<T> c;
237 c.x = a.x * (T)co;
238 c.y = a.y * (T)co;
239 return c;
242 template<class T, class CO>
243 vec3<T> operator*(const vec3<T>& a, CO co)
245 vec3<T> c;
246 c.x = a.x * (T)co;
247 c.y = a.y * (T)co;
248 c.z = a.z * (T)co;
249 return c;
252 template<class T, class CO>
253 vec2<T> operator*(CO co, const vec2<T>& a)
255 return a * co;
258 template<class T, class CO>
259 vec3<T> operator*(CO co, const vec3<T>& a)
261 return a * co;
264 template<class T>
265 vec2<T> operator/(const vec2<T>& a, const vec2<T>& b)
267 vec2<T> c;
268 c.x = a.x / b.x;
269 c.y = a.y / b.y;
270 return c;
273 template<class T>
274 vec3<T> operator/(const vec3<T>& a, const vec3<T>& b)
276 vec3<T> c;
277 c.x = a.x / b.x;
278 c.y = a.y / b.y;
279 c.z = a.z / b.z;
280 return c;
283 template<class T, class CO>
284 vec2<T> operator/(const vec2<T>& a, CO co)
286 vec2<T> c;
287 c.x = a.x / (T)co;
288 c.y = a.y / (T)co;
289 return c;
292 template<class T, class CO>
293 vec3<T> operator/(const vec3<T>& a, CO co)
295 vec3<T> c;
296 c.x = a.x / (T)co;
297 c.y = a.y / (T)co;
298 c.z = a.z / (T)co;
299 return c;
302 template<class T, class CO>
303 vec2<T> operator/(CO co, const vec2<T>& a)
305 vec2<T> c;
306 c.x = (T)co / a.x;
307 c.y = (T)co / a.y;
308 return c;
311 template<class T, class CO>
312 vec3<T> operator/(CO co, const vec3<T>& a)
314 vec3<T> c;
315 c.x = (T)co / a.x;
316 c.y = (T)co / a.y;
317 c.z = (T)co / a.z;
318 return c;
321 template<class T>
322 bool operator==(const vec2<T>& a, const vec2<T>& b)
324 return a.x == b.x && a.y == b.y;
327 template<class T>
328 bool operator==(const vec3<T>& a, const vec3<T>& b)
330 return a.x == b.x && a.y == b.y && a.z == b.z;
333 //! Integer vector.
334 typedef vec2<int> ivec2;
335 typedef vec3<int> ivec3;
337 //! Real vector.
338 typedef vec2<double> rvec2;
339 typedef vec3<double> rvec3;
341 //! Coordinates.
342 typedef ivec3 icoord;
343 typedef rvec3 rcoord;
346 void exportVecs();
348 #endif