License Public Domain
Lines 216
Keywords
2d (2) vector (5)
Permissions
Viewable by Everyone
Editable by All Siafoo Users
Hide
Easily highlight source code for your blog with our Syntax Highlighter. Join Siafoo Now or Learn More

vector2.h Atom Feed 0

In Brief This is a 2D vector library that uses operators. It is required for a lot of the other libraries I have written.
# 's
  1#ifndef VECTOR2_H
2#define VECTOR2_H
3
4// this class defines a 2D vector in different coordinate systems
5
6#include <cmath>
7#include <iostream>
8#include <assert>
9#include "angle.h"
10
11namespace Math {
12
13// use this to address the vector
14enum Coord2D {
15 X = 0,
16 Y
17};
18
19class Vector2
20{
21
22 private:
23 // the actual data
24 double data[2];
25
26 public:
27 Vector2()
28 {
29 data[X] = 0;
30 data[Y] = 0;
31 }
32
33 Vector2(double x, double y)
34 {
35 data[X] = x;
36 data[Y] = y;
37 }
38
39 /// accessors and setters
40 double& operator[] (unsigned index)
41 {
42 return data[index];
43 }
44
45 const double& operator[] (unsigned index) const
46 {
47 return data[index];
48 }
49
50 // sets the radius, keeping the angle the same
51 void setRadius(double radius)
52 {
53 double curRadius = magnitude();
54 if (0 == curRadius)
55 {
56 // lets set the angle along the x-axis as the default
57 data[X] = radius;
58 } else
59 {
60 // rescale the vector to the new radius
61 *this *= radius / curRadius;
62 }
63 }
64
65 // returns the radius in polar coordinates
66 double radius() const
67 {
68 return magnitude();
69 }
70
71 // sets the angle, keeping the radius constant
72 void setTheta(const Angle& angle)
73 {
74 setPolar(magnitude(), angle);
75 }
76
77 double theta() const
78 {
79 return atan2(data[Y], data[X]);
80 }
81
82 // set the vector using polar coordinates
83 void setPolar(double radius, const Angle& angle)
84 {
85 data[X] = cos(angle.radians());
86 data[Y] = sin(angle.radians());
87 *this *= radius;
88 }
89
90 // returns the polar components of the vector
91 void getPolar(double& radius, Angle& angle)
92 {
93 radius = radius();
94 angle = theta();
95 }
96
97 /// operators
98 Vector2 operator - ()
99 {
100 return Vector2(-data[X], -data[Y]);
101 }
102
103 Vector2& operator = (const Vector2 &rhs)
104 {
105 data[X] = rhs.data[X];
106 data[Y] = rhs.data[Y];
107 return *this;
108 }
109
110 Vector2& operator += (const Vector2& rhs)
111 {
112 data[X] += rhs.data[X];
113 data[Y] += rhs.data[Y];
114 return *this;
115 }
116
117 Vector2& operator -= (const Vector2& rhs)
118 {
119 data[X] -= rhs.data[X];
120 data[Y] -= rhs.data[Y];
121 return *this;
122 }
123
124 Vector2& operator *= (const double rhs)
125 {
126 data[X] *= rhs;
127 data[Y] *= rhs;
128 return *this;
129 }
130
131 Vector2& operator /= (const double rhs)
132 {
133 data[X] /= rhs;
134 data[Y] /= rhs;
135 return *this;
136 }
137
138 const double* v() const
139 {
140 return data;
141 }
142
143 double magnitudeSquared() const
144 {
145 return data[X]*data[X] + data[Y]*data[Y];
146 }
147
148 double magnitude() const
149 {
150 return std::sqrt(data[X]*data[X] + data[Y]*data[Y]);
151 }
152
153 Vector2& normalize()
154 {
155 double scale = magnitude();
156
157 // make sure we don't divide by zero
158 // normalizing zero vector not really defined...
159 assert(scale != 0.0);
160
161 *this /= scale;
162 return *this;
163 }
164};
165
166//////////////////////////
167/// Vector2 functions ///
168//////////////////////////
169
170
171inline std::ostream& operator << (std::ostream &os, const Vector2 &v)
172{
173 return os << '(' << v[X] << ", " << v[Y] << ')';
174}
175
176inline bool operator == (const Vector2 &lhs, const Vector2 &rhs)
177{
178 return lhs[X] == rhs[X]
179 && lhs[Y] == rhs[Y];
180}
181
182inline bool operator != (const Vector2 &lhs, const Vector2 &rhs)
183{
184 return lhs[X] != rhs[X] || lhs[Y] != rhs[Y];
185}
186
187inline Vector2 operator + (const Vector2 &lhs, const Vector2 &rhs)
188{
189 Vector2 ret(lhs);
190 ret += rhs;
191 return ret;
192}
193
194inline Vector2 operator - (const Vector2 &lhs, const Vector2 &rhs)
195{
196 Vector2 ret(lhs);
197 ret -= rhs;
198 return ret;
199}
200
201inline Vector2 operator * (const Vector2 &lhs, const double rhs)
202{
203 Vector2 ret(lhs);
204 ret *= rhs;
205 return ret;
206}
207
208inline Vector2 operator * (const double lhs, const Vector2 &rhs)
209{
210 Vector2 ret(rhs);
211 ret *= lhs;
212 return ret;
213}
214
215inline Vector2 operator / (const Vector2 &lhs, const double rhs)
216{
217 Vector2 ret(lhs);
218 ret /= rhs;
219 return ret;
220}
221
222inline double abs(const Vector2 &orig)
223{
224 return orig.magnitude();
225}
226
227inline Vector2 normal(const Vector2 &orig)
228{
229 Vector2 ret(orig);
230 ret.normalize();
231 return ret;
232}
233
234inline double dot(const Vector2 &u, const Vector2 &v)
235{
236 return u[X]*v[X] + u[Y]*v[Y];
237}
238
239inline double cross(const Vector2 &u, const Vector2 &v)
240{
241 return u[X]*v[Y] - u[Y]*v[X];
242}
243
244inline Vector2 polar2xy(double radius, const Angle& angle)
245{
246 return Vector2(radius * cos(angle.radians()), radius * sin(angle.radians()));
247}
248
249
250} // namespace Math
251
252#endif

This is a 2D vector library that uses operators. It is required for a lot of the other libraries I have written.