Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Vec2.h
00001 /* 00002 File: Vec2.h 00003 00004 Function: Defines a length-2 vector. 00005 00006 Author(s): Andrew Willmott 00007 00008 Copyright: (c) 1995-2001, Andrew Willmott 00009 */ 00010 00011 #ifndef __Vec2__ 00012 #define __Vec2__ 00013 #include "Constants.h" 00014 #include "Basics.h" 00015 00016 // --- Vec2 Class ------------------------------------------------------------- 00017 00018 00019 class Vec2 00020 { 00021 public: 00022 00023 // Constructors 00024 00025 Vec2(); 00026 Vec2(double x, double y); // (x, y) 00027 Vec2(const Vec2 &v); // Copy constructor 00028 Vec2(ZeroOrOne k); // v[i] = vl_zero 00029 Vec2(Axis k); // v[k] = 1 00030 00031 // Accessor functions 00032 00033 double &operator [] (Int i); 00034 const double &operator [] (Int i) const; 00035 00036 Int Elts() const { return(2); }; 00037 double *Ref() const; // Return ptr to data 00038 00039 // Assignment operators 00040 00041 Vec2 &operator = (const Vec2 &a); 00042 Vec2 &operator = (ZeroOrOne k); 00043 Vec2 &operator = (Axis k); 00044 00045 Vec2 &operator += (const Vec2 &a); 00046 Vec2 &operator -= (const Vec2 &a); 00047 Vec2 &operator *= (const Vec2 &a); 00048 Vec2 &operator *= (double s); 00049 Vec2 &operator /= (const Vec2 &a); 00050 Vec2 &operator /= (double s); 00051 00052 // Comparison operators 00053 00054 bool operator == (const Vec2 &a) const; // v == a? 00055 bool operator != (const Vec2 &a) const; // v != a? 00056 00057 // Arithmetic operators 00058 00059 Vec2 operator + (const Vec2 &a) const; // v + a 00060 Vec2 operator - (const Vec2 &a) const; // v - a 00061 Vec2 operator - () const; // -v 00062 Vec2 operator * (const Vec2 &a) const; // v * a (vx * ax, ...) 00063 Vec2 operator * (double s) const; // v * s 00064 Vec2 operator / (const Vec2 &a) const; // v / a (vx / ax, ...) 00065 Vec2 operator / (double s) const; // v / s 00066 00067 // Initialisers 00068 00069 Vec2 &MakeZero(); // Zero vector 00070 Vec2 &MakeUnit(Int i, double k = vl_one); // I[i] 00071 Vec2 &MakeBlock(double k = vl_one); // All-k vector 00072 00073 Vec2 &Normalise(); // normalise vector 00074 00075 // Private... 00076 00077 protected: 00078 00079 double elt[2]; 00080 }; 00081 00082 00083 // --- Vec operators ---------------------------------------------------------- 00084 00085 inline Vec2 operator * (double s, const Vec2 &v); // s * v 00086 inline double dot(const Vec2 &a, const Vec2 &b); // v . a 00087 inline double len(const Vec2 &v); // || v || 00088 inline double sqrlen(const Vec2 &v); // v . v 00089 inline Vec2 norm(const Vec2 &v); // v / || v || 00090 inline void normalise(Vec2 &v); // v = norm(v) 00091 inline Vec2 cross(const Vec2 &v); // cross prod. 00092 00093 //std::ostream &operator << (std::ostream &s, const Vec2 &v); 00094 //std::istream &operator >> (std::istream &s, Vec2 &v); 00095 00096 inline void printVec2(const Vec2 &v); 00097 00098 00099 // --- Inlines ---------------------------------------------------------------- 00100 00101 inline double &Vec2::operator [] (Int i) 00102 { 00103 CheckRange(i, 0, 2, "(Vec2::[i]) index out of range"); 00104 return(elt[i]); 00105 } 00106 00107 inline const double &Vec2::operator [] (Int i) const 00108 { 00109 CheckRange(i, 0, 2, "(Vec2::[i]) index out of range"); 00110 return(elt[i]); 00111 } 00112 00113 inline Vec2::Vec2() 00114 { 00115 } 00116 00117 inline Vec2::Vec2(double x, double y) 00118 { 00119 elt[0] = x; 00120 elt[1] = y; 00121 } 00122 00123 inline Vec2::Vec2(const Vec2 &v) 00124 { 00125 elt[0] = v[0]; 00126 elt[1] = v[1]; 00127 } 00128 00129 inline double *Vec2::Ref() const 00130 { 00131 return((double *) elt); 00132 } 00133 00134 inline Vec2 &Vec2::operator = (const Vec2 &v) 00135 { 00136 elt[0] = v[0]; 00137 elt[1] = v[1]; 00138 00139 return(SELF); 00140 } 00141 00142 inline Vec2 &Vec2::operator += (const Vec2 &v) 00143 { 00144 elt[0] += v[0]; 00145 elt[1] += v[1]; 00146 00147 return(SELF); 00148 } 00149 00150 inline Vec2 &Vec2::operator -= (const Vec2 &v) 00151 { 00152 elt[0] -= v[0]; 00153 elt[1] -= v[1]; 00154 00155 return(SELF); 00156 } 00157 00158 inline Vec2 &Vec2::operator *= (const Vec2 &v) 00159 { 00160 elt[0] *= v[0]; 00161 elt[1] *= v[1]; 00162 00163 return(SELF); 00164 } 00165 00166 inline Vec2 &Vec2::operator *= (double s) 00167 { 00168 elt[0] *= s; 00169 elt[1] *= s; 00170 00171 return(SELF); 00172 } 00173 00174 inline Vec2 &Vec2::operator /= (const Vec2 &v) 00175 { 00176 elt[0] /= v[0]; 00177 elt[1] /= v[1]; 00178 00179 return(SELF); 00180 } 00181 00182 inline Vec2 &Vec2::operator /= (double s) 00183 { 00184 elt[0] /= s; 00185 elt[1] /= s; 00186 00187 return(SELF); 00188 } 00189 00190 inline Vec2 Vec2::operator + (const Vec2 &a) const 00191 { 00192 Vec2 result; 00193 00194 result[0] = elt[0] + a[0]; 00195 result[1] = elt[1] + a[1]; 00196 00197 return(result); 00198 } 00199 00200 inline Vec2 Vec2::operator - (const Vec2 &a) const 00201 { 00202 Vec2 result; 00203 00204 result[0] = elt[0] - a[0]; 00205 result[1] = elt[1] - a[1]; 00206 00207 return(result); 00208 } 00209 00210 inline Vec2 Vec2::operator - () const 00211 { 00212 Vec2 result; 00213 00214 result[0] = -elt[0]; 00215 result[1] = -elt[1]; 00216 00217 return(result); 00218 } 00219 00220 inline Vec2 Vec2::operator * (const Vec2 &a) const 00221 { 00222 Vec2 result; 00223 00224 result[0] = elt[0] * a[0]; 00225 result[1] = elt[1] * a[1]; 00226 00227 return(result); 00228 } 00229 00230 inline Vec2 Vec2::operator * (double s) const 00231 { 00232 Vec2 result; 00233 00234 result[0] = elt[0] * s; 00235 result[1] = elt[1] * s; 00236 00237 return(result); 00238 } 00239 00240 inline Vec2 operator * (double s, const Vec2 &v) 00241 { 00242 return(v * s); 00243 } 00244 00245 inline Vec2 Vec2::operator / (const Vec2 &a) const 00246 { 00247 Vec2 result; 00248 00249 result[0] = elt[0] / a[0]; 00250 result[1] = elt[1] / a[1]; 00251 00252 return(result); 00253 } 00254 00255 inline Vec2 Vec2::operator / (double s) const 00256 { 00257 Vec2 result; 00258 00259 result[0] = elt[0] / s; 00260 result[1] = elt[1] / s; 00261 00262 return(result); 00263 } 00264 00265 inline double dot(const Vec2 &a, const Vec2 &b) 00266 { 00267 return(a[0] * b[0] + a[1] * b[1]); 00268 } 00269 00270 inline Vec2 cross(const Vec2 &a) 00271 { 00272 Vec2 result; 00273 00274 result[0] = a[1]; 00275 result[1] = -a[0]; 00276 00277 return(result); 00278 } 00279 00280 inline double len(const Vec2 &v) 00281 { 00282 return(sqrt(dot(v, v))); 00283 } 00284 00285 inline double sqrlen(const Vec2 &v) 00286 { 00287 return(dot(v, v)); 00288 } 00289 00290 inline Vec2 norm(const Vec2 &v) 00291 { 00292 Assert(sqrlen(v) > 0.0, "normalising length-zero vector"); 00293 return(v / len(v)); 00294 } 00295 00296 inline void normalise(Vec2 &v) 00297 { 00298 v /= len(v); 00299 } 00300 00301 inline Vec2 &Vec2::MakeUnit(Int i, double k) 00302 { 00303 if (i == 0) 00304 { elt[0] = k; elt[1] = vl_zero; } 00305 else if (i == 1) 00306 { elt[0] = vl_zero; elt[1] = k; } 00307 else 00308 _Error("(Vec2::Unit) illegal unit vector"); 00309 return(SELF); 00310 } 00311 00312 inline Vec2 &Vec2::MakeZero() 00313 { 00314 elt[0] = vl_zero; elt[1] = vl_zero; 00315 return(SELF); 00316 } 00317 00318 inline Vec2 &Vec2::MakeBlock(double k) 00319 { 00320 elt[0] = k; elt[1] = k; 00321 return(SELF); 00322 } 00323 00324 inline Vec2 &Vec2::Normalise() 00325 { 00326 Assert(sqrlen(SELF) > 0.0, "normalising length-zero vector"); 00327 SELF /= len(SELF); 00328 return(SELF); 00329 } 00330 00331 00332 inline Vec2::Vec2(ZeroOrOne k) 00333 { 00334 elt[0] = k; 00335 elt[1] = k; 00336 } 00337 00338 inline Vec2::Vec2(Axis k) 00339 { 00340 MakeUnit(k, vl_one); 00341 } 00342 00343 inline Vec2 &Vec2::operator = (ZeroOrOne k) 00344 { 00345 elt[0] = k; elt[1] = k; 00346 00347 return(SELF); 00348 } 00349 00350 inline Vec2 &Vec2::operator = (Axis k) 00351 { 00352 MakeUnit(k, vl_1); 00353 00354 return(SELF); 00355 } 00356 00357 inline bool Vec2::operator == (const Vec2 &a) const 00358 { 00359 return(elt[0] == a[0] && elt[1] == a[1]); 00360 } 00361 00362 inline bool Vec2::operator != (const Vec2 &a) const 00363 { 00364 return(elt[0] != a[0] || elt[1] != a[1]); 00365 } 00366 00367 inline void printVec2(const Vec2 &v) 00368 { 00369 printf("[%10f %10f]",v[0],v[1]); 00370 } 00371 00372 00373 #endif
Generated on Wed Jul 13 2022 19:31:42 by
1.7.2