summaryrefslogtreecommitdiffstats
path: root/Vector.h
blob: cb55637228e00c969c9264fded58495e7da60c6b (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
#ifndef _VECTOR_H_
#define _VECTOR_H_

#include "Vertex.h"
#include <math.h>

class Vector : public Vertex {
  public:
    Vector(float x0 = 0, float y0 = 0, float z0 = 0) : Vertex(x0, y0, z0) {}
    Vector(const Vertex &v) : Vertex(v) {}
    
    Vector operator+(const Vector &v) const {
      return Vector(x+v.x, y+v.y, z+v.z);
    }
    
    Vector operator*(float f) const {
      return Vector(x*f, y*f, z*f);
    }
    
    Vector operator/(float f) const {
      return (*this)*(1/f);
    }
    
    Vector operator-() const {
      return Vector(-x, -y, -z);
    }
    
    Vector cross(const Vector &v) const {
      return Vector(y*v.z - z*v.y,
                    z*v.x - x*v.z,
                    x*v.y - y*v.x);
    }
    
    float dot(const Vector &v) const {
      return x*v.x + y*v.y + z*v.z;
    }

    float length() const {
      return sqrtf(x*x+y*y+z*z);
    }

    Vector normalize() const {
      return *this/length();
    }
};

static inline Vector operator-(const Vertex &v1, const Vertex &v2) {
  return Vector(v1.getX()-v2.getX(), v1.getY()-v2.getY(), v1.getZ()-v2.getZ());
}

#endif /*_VECTOR_H_*/