| author | saiam |
| Sat, 29 Nov 2008 17:29:53 +0000 | |
| changeset 133 | c05e84ccc4b3 |
| parent 130 | 81406f8b7535 |
| child 158 | 0215ace86018 |
| permissions | -rw-r--r-- |
|
130
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
1 |
#ifndef VECTOR_HH |
|
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
2 |
#define VECTOR_HH |
| 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 |
/** |
|
130
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
8 |
* A 2D Vector class. Implements standard vector operations. |
| 57 | 9 |
*/ |
| 55 | 10 |
template <typename T> |
11 |
class _Vector {
|
|
| 51 | 12 |
public: |
| 55 | 13 |
T x; |
14 |
T y; |
|
|
130
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
15 |
|
|
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
16 |
/** |
|
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
17 |
* Default constructor. |
|
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
18 |
*/ |
|
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
19 |
_Vector() : x(0), y(0) {}
|
| 55 | 20 |
|
| 57 | 21 |
/** |
|
130
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
22 |
* Constuctor. |
|
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
23 |
* |
|
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
24 |
* @param x Initial x-coordinate |
|
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
25 |
* @param y Initial y-coordinate |
| 57 | 26 |
*/ |
| 55 | 27 |
_Vector(T x, T y) : x(x), y(y) {}
|
|
130
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
28 |
|
| 57 | 29 |
/** |
|
130
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
30 |
* Copy constructor. |
|
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
31 |
* |
|
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
32 |
* @param v Vector to be copied. |
| 57 | 33 |
*/ |
34 |
_Vector(const _Vector &v) : x(v.x), y(v.y) {}
|
|
| 55 | 35 |
|
|
130
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
36 |
// Operator declarations |
| 57 | 37 |
void operator=(const _Vector &v) {
|
| 55 | 38 |
this->x = v.x; |
39 |
this->y = v.y; |
|
| 53 | 40 |
} |
| 57 | 41 |
_Vector operator+(const _Vector &v) const {
|
| 55 | 42 |
return _Vector(this->x+v.x, this->y+v.y); |
| 53 | 43 |
} |
| 57 | 44 |
_Vector operator-(const _Vector &v) const {
|
| 55 | 45 |
return _Vector(this->x-v.x, this->y-v.y); |
| 53 | 46 |
} |
|
130
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
47 |
_Vector operator*(const T &scalar) const {
|
|
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
48 |
return _Vector(this->x*scalar, this->y*scalar); |
| 51 | 49 |
} |
| 99 | 50 |
T operator*(const _Vector &v) const {
|
51 |
return (this->x*v.x + this->y*v.y); |
|
52 |
} |
|
| 57 | 53 |
_Vector operator/(const T &d) const {
|
| 55 | 54 |
return _Vector(this->x/d, this->y/d); |
|
130
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
55 |
} |
| 57 | 56 |
void operator+=(const _Vector &v) {
|
|
130
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
57 |
*this = *this + v; |
| 51 | 58 |
} |
| 57 | 59 |
void operator-=(const _Vector &v) {
|
|
130
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
60 |
*this = *this - v; |
| 57 | 61 |
} |
|
130
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
62 |
void operator*=(const T &scalar) {
|
|
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
63 |
*this = *this * scalar; |
| 55 | 64 |
} |
|
130
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
65 |
void operator/=(const T &scalar) {
|
|
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
66 |
*this = *this / scalar; |
| 57 | 67 |
} |
|
130
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
68 |
|
|
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
69 |
// Other operations |
|
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
66
diff
changeset
|
70 |
T length() const {
|
|
130
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
71 |
return sqrt((this->x * this->x) + (this->y * this->y)); |
|
77
98dc9008d15f
changed collision detection, remove old if content with new
nireco
parents:
66
diff
changeset
|
72 |
} |
|
124
2fd698e04779
fixed collision detection again, and put some needed rounding to getNormal
nireco
parents:
99
diff
changeset
|
73 |
_Vector roundToInt() const {
|
|
130
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
74 |
return _Vector(round(x), round(y)); |
|
124
2fd698e04779
fixed collision detection again, and put some needed rounding to getNormal
nireco
parents:
99
diff
changeset
|
75 |
} |
| 51 | 76 |
}; |
| 46 | 77 |
|
|
130
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
78 |
// Unary operators |
|
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
79 |
template<typename T> |
|
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
80 |
_Vector<T> operator*(const T &scalar, const _Vector<T> v) {
|
|
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
81 |
return (v * scalar); |
|
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
82 |
} |
|
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
83 |
|
|
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
84 |
// Comparison operators |
| 53 | 85 |
template<typename T> |
| 57 | 86 |
bool operator==(const _Vector<T> &v1, const _Vector<T> &v2) {
|
| 55 | 87 |
return ((v1.x == v2.x) && (v1.y == v2.y)); |
| 53 | 88 |
} |
| 57 | 89 |
template<typename T> |
90 |
bool operator!=(const _Vector<T> &v1, const _Vector<T> &v2) {
|
|
91 |
return !(v1 == v2); |
|
92 |
} |
|
| 53 | 93 |
|
|
130
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
94 |
// Output operator |
| 55 | 95 |
template<typename T> |
| 66 | 96 |
std::ostream& operator<<(std::ostream &s, const _Vector<T> &v) {
|
| 55 | 97 |
return s<<"("<<v.x<<", "<<v.y<<")";
|
98 |
} |
|
99 |
||
|
130
81406f8b7535
New Vector.hh with some more documentation and unary operator for * defined. Also modified operators +=, -=, *= and /= to use
saiam
parents:
124
diff
changeset
|
100 |
// Standard vector |
| 60 | 101 |
typedef _Vector<float> Vector; |
| 55 | 102 |
|
| 46 | 103 |
#endif |