sources:


website:
more info here


screenshot:
studies/grafik/Computergrafik-Code2/Aufgabe4/vector.h
download file

  1
  2 // Vektorklasse
  3 //
  4 // dritte Version, weitere Operatoren überladen, Input/Output-Streams
  5
  6 #ifndef VECTOR_H
  7 #define VECTOR_H
  8
  9 #include <math.h>
 10 #include <iostream.h>
 11
 12 class Vector {
 13 public:
 14     Vector(double x=0, double y=0, double z=0);
 15
 16     double operator[](int i) const;
 17     double& operator[](int i);
 18
 19     Vector operator+(const Vector& w) const;
 20     Vector operator*(double f) const;
 21     friend Vector operator*(double f, const Vector& w);
 22     Vector operator*(const Vector& w) const;
 23
 24     void normalize();
 25
 26     friend double dotProduct(const Vector& v, const Vector& w);
 27     friend double length(const Vector& v);
 28
 29     friend ostream& operator<<(ostream& o, const Vector& w);
 30     friend istream& operator>>(istream& i, Vector& w);
 31
 32 private:
 33     double vec[3];
 34 };
 35
 36 inline Vector::Vector(double x, double y, double z) {
 37     vec[0] = x;
 38     vec[1] = y;
 39     vec[2] = z;
 40 }
 41
 42 inline double Vector::operator[](int i) const { return vec[i]; }
 43
 44 inline double& Vector::operator[](int i) { return vec[i]; }
 45
 46 inline Vector Vector::operator+(const Vector& w) const {
 47     return Vector(vec[0] + w[0], vec[1] + w[1], vec[2] + w[2]);
 48 }
 49
 50 inline Vector Vector::operator*(double f) const {
 51     return Vector(vec[0]*f, vec[1]*f, vec[2]*f);
 52 }
 53
 54 inline Vector Vector::operator*(const Vector& w) const {
 55     return Vector(vec[1] * w.vec[2] - vec[2] * w.vec[1],
 56                    vec[2] * w.vec[0] - vec[0] * w.vec[2],
 57                   vec[0] * w.vec[1] - vec[1] * w.vec[0])
;
 58 }
 59
 60 inline Vector operator*(double f, const Vector& w) {
 61     return Vector(w.vec[0]*f, w.vec[1]*f, w.vec[2]*f);
 62 }
 63
 64 inline void Vector::normalize() {
 65     double l = length(*this);
 66
 67     vec[0] /= l;
 68     vec[1] /= l;
 69     vec[2] /= l;
 70 }
 71
 72 inline double dotProduct(const Vector& v, const Vector& w) {
 73     return v.vec[0] * w.vec[0] + v.vec[1] * w.vec[1] + v.vec[2] * w.vec[2];
 74 }
 75
 76 inline double length(const Vector& v) {
 77     return sqrt(dotProduct(v,v));
 78 }
 79
 80 #endif // VECTOR_H
 81