/************************************************************************ * * * File: vector3v.h * * * * Author: Robb T. Koether * * * * Date: Sep 9, 2003 * * * * Purpose: This file contains the definition of the Vector3v class * * * ************************************************************************/ #ifndef VECTOR3_H #define VECTOR3_H // Header files #include #include #include #include "point3v.h" using namespace std; class Point3v; /************************************************************************ * * * The definition of the Vector3v class * * * ************************************************************************/ class Vector3v { // Public member functions public: // Constructors Vector3v() {coord[0] = 0.0; coord[1] = 0.0; coord[2] = 0.0; coord[3] = 0.0;} Vector3v(const Vector3v& v) { coord[0] = v.coord[0]; coord[1] = v.coord[1]; coord[2] = v.coord[2]; coord[3] = v.coord[3]; } Vector3v(float x, float y, float z) {coord[0] = x; coord[1] = y; coord[2] = z; coord[3] = 0.0;} // Inspectors float x() const {return coord[0];} float y() const {return coord[1];} float z() const {return coord[2];} float* v() const {return (float*)coord;} // Mutators void x(float x) {coord[0] = x;} void y(float y) {coord[1] = y;} void z(float z) {coord[2] = z;} void set(float x, float y, float z) {coord[0] = x; coord[1] = y; coord[2] = z;} // Other functions double length() const {return sqrt(x()*x() + y()*y() + z()*z());} Vector3v normalize() { float len = 1.0/length(); coord[0] *= len; coord[1] *= len; coord[2] *= len; return *this; } void setDiff(const Point3v& p, const Point3v& q); void Output(ostream& out) const {out << "<" << x() << ", " << y() << ", " << z() << ">";} float dot(const Vector3v& v) {return x()*v.x() + y()*v.y() + z()*v.z();} Vector3v cross(const Vector3v& v) { Vector3v u; u.coord[0] = y() * v.z() - z() * v.y(); u.coord[1] = z() * v.x() - x() * v.z(); u.coord[2] = x() * v.y() - y() * v.x(); return u; } Vector3v operator-() const {return Vector3v(-x(), -y(), -z());} Vector3v addV(const Vector3v& v) const; Vector3v subtractV(const Vector3v& v) const; Vector3v multiplyS(const double& s) const; Point3v addP(const Point3v& p) const; // Public data members public: float coord[4]; }; // Operators ostream& operator<<(ostream& out, const Vector3v& v); Vector3v operator-(const Vector3v& u, const Vector3v& v); Vector3v operator-(const Point3v& p, const Point3v& q); Vector3v operator+(const Vector3v& u, const Vector3v& v); Vector3v operator*(const double s, const Vector3v& v); Point3v operator+(const Vector3v& v, const Point3v& p); Point3v operator+(const Vector3v& v, const Point3v& p); #endif