#ifndef VEC_H #define VEC_H /////////////////////////////////////////////////////////////// // vec.h - declares a 3 Dimensional vector // // ver 1.1 // // Language: Visual C++, ver 7.0 // // Platform: Dell Dimension 8100, Windows 2000, SP 2 // // Application: Demonstration for CSE687, Spring 2003 // // Author: Jim Fawcett, CST 2-187, Syracuse Univ // // (315) 443-3948, jfawcett@twcny.rr.com // /////////////////////////////////////////////////////////////// /* Class Operations: ================= This class represents 3D mathematical vectors. That is, they reside in a Linear Vector Space of 3 dimensions. This implies that they must support vector addition and subtraction and scalar multiplication. Public Interface: ================= vec v(1.5, -2, 3.3333); if v1, v2, v3 already exist: vec v4 = v1 + v2 - v3; vec v5 = 3.3333 * v4; v4 *= 0.3333; v5 += v6; double innerProd = (v1,v2); */ /////////////////////////////////////////////////////////////// // Build Process // /////////////////////////////////////////////////////////////// // Required Files: // // vec.h, vec.cpp // // // // Compiler command: // // cl /DTEST_VEC vec.cpp // /////////////////////////////////////////////////////////////// /* Maintenance History: ==================== ver 1.1 : 29 Jan 06 - cosmetic changes to test stub ver 1.0 : 26 Jan 03 - first release */ // namespace LVS_3D { //----< 3D vector class >------------------------------------------------ template class vec { public: vec(const T& x=0, const T& y=0, const T& z=0); // vec(const vec& v); // vec& operator=(const vec& v); vec& operator+=(const vec& v); vec& operator-=(const vec& v); vec& operator*=(const T &t); T operator,(const vec& v); T size(); void show(const char* name=0); private: T _x, _y, _z; }; //----< parameterized constructor >-------------------------------------- template vec::vec(const T& x, const T& y, const T& z) : _x(x), _y(y), _z(z) {} //----< copy constructor : correct compiler generated copy >------------- // // template // vec::vec(const vec& v) // : _x(v._x), _y(v._y), _z(v._z) {} //----< assignment operator : correct compiler generated assignment >---- // // template // vec& vec::operator =(const vec& v) // { // if(this == &v) return *this; // _x = v._x; // _y = v._y; // _z = v._z; // return *this; // } //---< additive assignment >--------------------------------------------- template vec& vec::operator+=(const vec& v) { _x += v._x; _y += v._y; _z += v._z; return *this; } //----< addition >------------------------------------------------------- template vec operator+(const vec& v1, const vec& v2) { vec temp = v1; return (temp += v2); } // //----< subtractive assignment >----------------------------------------- template vec& vec::operator-=(const vec& v) { _x -= v._x; _y -= v._y; _z -= v._z; return *this; } //----< subtraction >---------------------------------------------------- template vec operator-(const vec& v1, const vec& v2) { vec temp = v1; return (temp -= v2); } //----< scalar multiplicative assignment >------------------------------- template vec& vec::operator*=(const T &t) { _x *= t; _y *= t; _z *= t; return *this; } //----< scalar multiplication >------------------------------------------ template vec operator*(const T &t, const vec& v) { vec temp = v; temp *= t; return temp; } // //----< inner product >-------------------------------------------------- template T vec::operator,(const vec& v) { return (_x*v._x + _y*v._y + _z*v._z); } //----< Euclidean Norm >------------------------------------------------- template T vec::size() { return static_cast(std::sqrt(static_cast(_x*_x + _y*_y + _z*_z))); } //----< display vector contents >---------------------------------------- template void vec::show(const char* name) { cout << "\n "; if(name != 0) cout << name; cout << "(" << _x << ", " << _y << ", " << _z << ")"; } } #endif