summaryrefslogtreecommitdiffstats
path: root/nebu/include/base/nebu_Vector3.h
blob: cdfe54b720eab557f44f7a782201772549113ff1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#ifndef NEBU_Vector3_H
#define NEBU_Vector3_H

#ifdef DEBUG
#include "iostream.h"
#endif

#include "math.h"

class Vector3 {
 public:
  Vector3() { };
  Vector3(float a, float b, float c) {
    x = a; y = b; z = c;
  };
  Vector3(const float *v) {
    x = v[0]; y = v[1]; z = v[2];
  };
  float x, y, z;

  const Vector3 operator+ (const Vector3& v) const {
    return Vector3(x + v.x, y + v.y, z + v.z);
  };

  const Vector3 operator- (const Vector3& v) const {
    return Vector3(x - v.x, y - v.y, z - v.z);
  };

  float operator* (const Vector3& v) const {
    return x * v.x + y * v.y + z * v.z;
  };

  const Vector3 operator* (float f) const {
    return Vector3(x * f, y * f, z * f);
  }

  void operator*= (float f) {
    x *= f; y *= f; z *= f;
  }

  void operator/= (float f) {
    x /= f; y /= f; z /= f;
  }

  float Length() const {
    return (float) sqrt(*this * *this);
  }

  float Length2() {
    return *this * *this;
  }

  Vector3& Normalize() {
    float length = Length();
    if(length != 0)
      *this /= length;
    return *this;
  }

  Vector3 Cross(const Vector3& v) const {
    return Vector3(y * v.z - z * v.y,
		   z * v.x - x * v.z, 
		   x * v.y - y * v.z);
  }

  #ifdef DEBUG
  friend ostream& 
    operator<<(ostream& os, const Vector3& v);

  friend Vector3
    operator*(float f, const Vector3& v);
  #endif
};

inline Vector3 operator*(float f, const Vector3& v) {
  return v * f;
}

#ifdef DEBUG
inline ostream& operator<<(ostream& os, const Vector3& v) {
  os << "(" << v.x << ", " << v.y << ", " << v.z << ")";
  return os;
}
#endif
#endif