// Vektorklasse // // dritte Version, weitere Operatoren überladen, Input/Output-Streams #ifndef VECTOR_H #define VECTOR_H #include #include class Vector { public: Vector(double x=0, double y=0, double z=0); double operator[](int i) const; double& operator[](int i); Vector operator+(const Vector& w) const; Vector operator*(double f) const; friend Vector operator*(double f, const Vector& w); Vector operator*(const Vector& w) const; void normalize(); friend double dotProduct(const Vector& v, const Vector& w); friend double length(const Vector& v); friend ostream& operator<<(ostream& o, const Vector& w); friend istream& operator>>(istream& i, Vector& w); private: double vec[3]; }; inline Vector::Vector(double x, double y, double z) { vec[0] = x; vec[1] = y; vec[2] = z; } inline double Vector::operator[](int i) const { return vec[i]; } inline double& Vector::operator[](int i) { return vec[i]; } inline Vector Vector::operator+(const Vector& w) const { return Vector(vec[0] + w[0], vec[1] + w[1], vec[2] + w[2]); } inline Vector Vector::operator*(double f) const { return Vector(vec[0]*f, vec[1]*f, vec[2]*f); } inline Vector Vector::operator*(const Vector& w) const { return Vector(vec[1] * w.vec[2] - vec[2] * w.vec[1], vec[2] * w.vec[0] - vec[0] * w.vec[2], vec[0] * w.vec[1] - vec[1] * w.vec[0]); } inline Vector operator*(double f, const Vector& w) { return Vector(w.vec[0]*f, w.vec[1]*f, w.vec[2]*f); } inline void Vector::normalize() { double l = length(*this); vec[0] /= l; vec[1] /= l; vec[2] /= l; } inline double dotProduct(const Vector& v, const Vector& w) { return v.vec[0] * w.vec[0] + v.vec[1] * w.vec[1] + v.vec[2] * w.vec[2]; } inline double length(const Vector& v) { return sqrt(dotProduct(v,v)); } #endif // VECTOR_H