author | saiam |
Fri, 28 Nov 2008 22:26:23 +0000 | |
changeset 128 | 890ac82cdcc0 |
parent 124 | 2fd698e04779 |
child 130 | 81406f8b7535 |
permissions | -rw-r--r-- |
53 | 1 |
#ifndef COOR_H |
2 |
#define COOR_H |
|
46 | 3 |
|
55 | 4 |
#include <iostream> |
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
66
diff
changeset
|
5 |
#include <cmath> |
51 | 6 |
|
57 | 7 |
/** |
8 |
* 2D Vector class. Implements standard vector operations. |
|
9 |
*/ |
|
55 | 10 |
template <typename T> |
11 |
class _Vector { |
|
51 | 12 |
public: |
55 | 13 |
T x; |
14 |
T y; |
|
15 |
||
16 |
_Vector() : x(0), y(0){} |
|
57 | 17 |
/** |
18 |
* @param x Initial x-coordinate. |
|
19 |
* @param y Initial y-coordinate. |
|
20 |
*/ |
|
55 | 21 |
_Vector(T x, T y) : x(x), y(y) {} |
57 | 22 |
/** |
23 |
* @param v Other vector to be copied. |
|
24 |
*/ |
|
25 |
_Vector(const _Vector &v) : x(v.x), y(v.y) {} |
|
55 | 26 |
|
57 | 27 |
void operator=(const _Vector &v) { |
55 | 28 |
this->x = v.x; |
29 |
this->y = v.y; |
|
53 | 30 |
} |
57 | 31 |
_Vector operator+(const _Vector &v) const { |
55 | 32 |
return _Vector(this->x+v.x, this->y+v.y); |
53 | 33 |
} |
57 | 34 |
_Vector operator-(const _Vector &v) const { |
55 | 35 |
return _Vector(this->x-v.x, this->y-v.y); |
53 | 36 |
} |
57 | 37 |
_Vector operator*(const T &d) const { |
55 | 38 |
return _Vector(this->x*d, this->y*d); |
51 | 39 |
} |
99 | 40 |
T operator*(const _Vector &v) const { |
41 |
return (this->x*v.x + this->y*v.y); |
|
42 |
} |
|
57 | 43 |
_Vector operator/(const T &d) const { |
55 | 44 |
return _Vector(this->x/d, this->y/d); |
45 |
} |
|
57 | 46 |
void operator+=(const _Vector &v) { |
55 | 47 |
this->x += v.x; |
48 |
this->y += v.y; |
|
51 | 49 |
} |
57 | 50 |
void operator-=(const _Vector &v) { |
51 |
this->x -= v.x; |
|
52 |
this->y -= v.y; |
|
53 |
} |
|
54 |
void operator*=(const T &f) { |
|
55 | 55 |
this->x *= f; |
56 |
this->y *= f; |
|
57 |
} |
|
57 | 58 |
void operator/=(const T &d) { |
59 |
this->x /= d; |
|
60 |
this->y /= d; |
|
61 |
} |
|
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
66
diff
changeset
|
62 |
T length() const { |
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
66
diff
changeset
|
63 |
return sqrt(x*x+y*y); |
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
66
diff
changeset
|
64 |
} |
124
2fd698e04779
fixed collision detection again, and put some needed rounding to getNormal
nireco
parents:
99
diff
changeset
|
65 |
_Vector roundToInt() const { |
2fd698e04779
fixed collision detection again, and put some needed rounding to getNormal
nireco
parents:
99
diff
changeset
|
66 |
return _Vector((int)x, (int)y); |
2fd698e04779
fixed collision detection again, and put some needed rounding to getNormal
nireco
parents:
99
diff
changeset
|
67 |
} |
51 | 68 |
}; |
46 | 69 |
|
53 | 70 |
template<typename T> |
57 | 71 |
bool operator==(const _Vector<T> &v1, const _Vector<T> &v2) { |
55 | 72 |
return ((v1.x == v2.x) && (v1.y == v2.y)); |
53 | 73 |
} |
57 | 74 |
template<typename T> |
75 |
bool operator!=(const _Vector<T> &v1, const _Vector<T> &v2) { |
|
76 |
return !(v1 == v2); |
|
77 |
} |
|
53 | 78 |
|
55 | 79 |
template<typename T> |
66 | 80 |
std::ostream& operator<<(std::ostream &s, const _Vector<T> &v) { |
55 | 81 |
return s<<"("<<v.x<<", "<<v.y<<")"; |
82 |
} |
|
83 |
||
60 | 84 |
typedef _Vector<float> Vector; |
55 | 85 |
|
46 | 86 |
#endif |