home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2007 September / maximum-cd-2007-09.iso / Assets / data / AssaultCube_v0.93.exe / source / src / geom.h < prev    next >
Encoding:
C/C++ Source or Header  |  2007-04-04  |  1.6 KB  |  43 lines

  1. struct vec
  2. {
  3.     union
  4.     {
  5.         struct { float x, y, z; };
  6.         float v[3];
  7.     };
  8.  
  9.     vec() {}
  10.     vec(float a, float b, float c) : x(a), y(b), z(c) {}
  11.     vec(float *v) : x(v[0]), y(v[1]), z(v[2]) {}
  12.  
  13.     float &operator[](int i)       { return v[i]; }
  14.     float  operator[](int i) const { return v[i]; }
  15.  
  16.     bool iszero() const { return x==0 && y==0 && z==0; }
  17.  
  18.     bool operator==(const vec &o) const { return x == o.x && y == o.y && z == o.z; }
  19.     bool operator!=(const vec &o) const { return x != o.x || y != o.y || z != o.z; }
  20.  
  21.     vec &mul(float f) { x *= f; y *= f; z *= f; return *this; }
  22.     vec &div(float f) { x /= f; y /= f; z /= f; return *this; }
  23.     vec &add(float f) { x += f; y += f; z += f; return *this; }
  24.     vec &sub(float f) { x -= f; y -= f; z -= f; return *this; }
  25.  
  26.     vec &add(const vec &o) { x += o.x; y += o.y; z += o.z; return *this; }
  27.     vec &sub(const vec &o) { x -= o.x; y -= o.y; z -= o.z; return *this; }
  28.  
  29.     float squaredlen() const { return x*x + y*y + z*z; }
  30.     float dot(const vec &o) const { return x*o.x + y*o.y + z*o.z; }
  31.  
  32.     float magnitude() const { return sqrtf(squaredlen()); }
  33.     vec &normalize() { div(magnitude()); return *this; }
  34.  
  35.     float dist(const vec &e) const { vec t; return dist(e, t); }
  36.     float dist(const vec &e, vec &t) const { t = *this; t.sub(e); return t.magnitude(); }
  37.  
  38.     float distxy(const vec &e) const { float dx = e.x - x, dy = e.y - y; return sqrtf(dx*dx + dy*dy); }
  39.  
  40.     bool reject(const vec &o, float max) const { return x>o.x+max || x<o.x-max || y>o.y+max || y<o.y-max; }
  41. };
  42.  
  43.