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