author | Tero Marttila <terom@fixme.fi> |
Tue, 13 Jan 2009 20:21:28 +0200 | |
changeset 390 | 3c8078b96469 |
parent 377 | 01d3c340b372 |
child 423 | 947ab54de4b7 |
permissions | -rw-r--r-- |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
1 |
#ifndef VECTOR_HH |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
2 |
#define VECTOR_HH |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
3 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
4 |
#include <iostream> |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
5 |
#include <cmath> |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
6 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
7 |
/** |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
8 |
* A 2D Vector class. Implements standard vector operations. |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
9 |
*/ |
370
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
10 |
template <typename T> class VectorType { |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
11 |
public: |
370
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
12 |
/** Horizontal component */ |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
13 |
T x; |
370
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
14 |
/** Vertical component */ |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
15 |
T y; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
16 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
17 |
/** |
370
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
18 |
* Default constructor. Values are zero |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
19 |
*/ |
370
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
20 |
VectorType() : |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
21 |
x(0), y(0) |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
22 |
{ |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
23 |
|
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
24 |
} |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
25 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
26 |
/** |
370
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
27 |
* Scalar constuctor. |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
28 |
* |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
29 |
* @param x Initial x-coordinate |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
30 |
* @param y Initial y-coordinate |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
31 |
*/ |
370
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
32 |
VectorType (const T &x, const T &y) : |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
33 |
x(x), y(y) |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
34 |
{ |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
35 |
|
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
36 |
} |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
37 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
38 |
/** |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
39 |
* Copy constructor. |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
40 |
* |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
41 |
* @param v Vector to be copied. |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
42 |
*/ |
370
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
43 |
VectorType (const VectorType &v) : |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
44 |
x(v.x), |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
45 |
y(v.y) |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
46 |
{ |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
47 |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
48 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
49 |
|
370
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
50 |
/** |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
51 |
* @name Standard operators |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
52 |
* |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
53 |
* @{ |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
54 |
*/ |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
55 |
VectorType& operator= (const VectorType &v) { |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
56 |
x = v.x; |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
57 |
y = v.y; |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
58 |
|
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
59 |
return *this; |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
60 |
} |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
61 |
|
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
62 |
VectorType operator+ (const VectorType &v) const { |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
63 |
return VectorType(x + v.x, y + v.y); |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
64 |
} |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
65 |
|
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
66 |
VectorType operator- (const VectorType &v) const { |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
67 |
return VectorType(x - v.x, y - v.y); |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
68 |
} |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
69 |
|
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
70 |
/** Unary minus (v + -v = 0) */ |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
71 |
VectorType operator- (void) const { |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
72 |
return VectorType(-x, -y); |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
73 |
} |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
74 |
|
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
75 |
/** Scalar multiplication */ |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
76 |
VectorType operator* (const T &scalar) const { |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
77 |
return VectorType(x * scalar, y * scalar); |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
78 |
} |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
79 |
|
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
80 |
/** Scalar division */ |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
81 |
VectorType operator/ (const T &scalar) const { |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
82 |
return VectorType(x / scalar, y / scalar); |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
83 |
} |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
84 |
|
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
85 |
/** Dot product */ |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
86 |
T operator* (const VectorType &v) const { |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
87 |
return (x * v.x + y * v.y); |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
88 |
} |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
89 |
|
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
90 |
void operator+= (const VectorType &v) { |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
91 |
x += v.x; |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
92 |
y += v.y; |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
93 |
} |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
94 |
|
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
95 |
void operator-= (const VectorType &v) { |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
96 |
x -= v.x; |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
97 |
y -= v.y; |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
98 |
} |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
99 |
|
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
100 |
void operator*= (const T &scalar) { |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
101 |
x *= scalar; |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
102 |
y *= scalar; |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
103 |
} |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
104 |
|
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
105 |
void operator/= (const T &scalar) { |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
106 |
x /= scalar; |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
107 |
y /= scalar; |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
108 |
} |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
109 |
|
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
110 |
/** |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
111 |
* XXX: This needs to do some rounding for float-vectors |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
112 |
*/ |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
113 |
bool operator== (const VectorType &other) const { |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
114 |
return (x == other.x) && (y == other.y); |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
115 |
} |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
116 |
|
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
117 |
/** |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
118 |
* XXX: This needs to do some rounding for float-vectors |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
119 |
*/ |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
120 |
bool operator!= (const VectorType &other) const { |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
121 |
return (x != other.x) || (y != other.y); |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
122 |
} |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
123 |
|
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
124 |
// @} |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
125 |
|
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
126 |
/** |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
127 |
* Vector scalar length |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
128 |
*/ |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
129 |
T length (void) const { |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
130 |
return sqrt(sqrLength()); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
131 |
} |
370
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
132 |
|
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
133 |
/** |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
134 |
* Vector scalar length, squared |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
135 |
*/ |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
136 |
T sqrLength (void) const { |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
137 |
return (x * x) + (y * y); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
138 |
} |
370
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
139 |
|
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
140 |
/** |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
141 |
* XXX: should be replaced with a working operator== implementation |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
142 |
* |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
143 |
* This doesn't actually *round*, it *truncates* |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
144 |
*/ |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
145 |
VectorType roundToInt (void) const { |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
146 |
return VectorType((int)(x), (int)(y)); |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
147 |
} |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
148 |
|
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
149 |
/** |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
150 |
* Test for a zero-length vector. |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
151 |
* |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
152 |
* XXX: this is currently only used when testing for Vector(0, 0), probably breaks otherwise |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
153 |
*/ |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
154 |
bool zero (void) const { |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
155 |
return x == 0 && y == 0; |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
156 |
} |
221 | 157 |
|
377
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
158 |
/** |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
159 |
* Returns the corresponding unit vector, i.e. the vector divided by its own length |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
160 |
* |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
161 |
* @return Vector with length() == 1 |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
162 |
*/ |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
163 |
VectorType unitVector (void) const { |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
164 |
T len = length(); |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
165 |
|
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
166 |
return VectorType(x / len, y / len); |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
167 |
} |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
168 |
|
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
169 |
/** |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
170 |
* Returns a normalized vector such that the x/y coordinates are both either -1, 0 or 1, to indicate which |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
171 |
* direction this vector points in. |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
172 |
* |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
173 |
* @return normalized Vector |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
174 |
*/ |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
175 |
VectorType normalizeDirection (void) const; |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
176 |
|
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
177 |
/** |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
178 |
* Returns a vector's normal (one of the two) |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
179 |
*/ |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
180 |
VectorType normal (void) const { |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
181 |
return VectorType(y, -x); |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
182 |
} |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
183 |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
184 |
}; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
185 |
|
370
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
186 |
/** |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
187 |
* @name Postfix operators |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
188 |
* Operators with the vector as the second argument :) |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
189 |
* |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
190 |
* @{ |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
191 |
*/ |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
192 |
template<typename T> VectorType<T> operator* (const T &scalar, const VectorType<T> &v) { |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
193 |
return (v * scalar); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
194 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
195 |
|
370
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
196 |
/** |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
197 |
* Make vectors printable |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
198 |
*/ |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
199 |
template<typename T> std::ostream& operator<< (std::ostream &s, const VectorType<T> &v) { |
39e59dd36b6e
clean up Vector a bit, remove unused Terrain -> direction function
terom
parents:
300
diff
changeset
|
200 |
return s << "(" << v.x << ", " << v.y << ")"; |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
201 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
202 |
|
377
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
203 |
// Templates function implementations |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
204 |
#include <cmath> |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
205 |
#include <cstdlib> |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
206 |
|
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
207 |
/** |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
208 |
* Float/int absolute value |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
209 |
*/ |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
210 |
template<typename T> static T absType (T value); |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
211 |
template<> static float absType<float> (float value) { return fabs(value); } |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
212 |
template<> static long absType<long> (long value) { return labs(value); } |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
213 |
|
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
214 |
/** |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
215 |
* Direction-normalize the given coordinate against the tangent coordinate. |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
216 |
* |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
217 |
* Returns 1 if the coordinate is positive and greater than the tangent, -1 if the coordinate is less than and |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
218 |
* negative, else zero. |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
219 |
*/ |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
220 |
template<typename T> static T normalizeCoordinate (T coord, T tangent) { |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
221 |
if (coord > absType(tangent)) |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
222 |
return 1; |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
223 |
|
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
224 |
else if (coord < -absType(tangent)) |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
225 |
return -1; |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
226 |
|
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
227 |
else |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
228 |
return 0; |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
229 |
} |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
230 |
|
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
231 |
template<typename T> VectorType<T> VectorType<T>::normalizeDirection (void) const { |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
232 |
return VectorType<T>( |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
233 |
normalizeCoordinate(x, y), |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
234 |
normalizeCoordinate(y, x) |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
235 |
); |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
236 |
} |
01d3c340b372
direction normalization functions in vector, change rope color, misc comments+whitespace
terom
parents:
370
diff
changeset
|
237 |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
238 |
#endif |