// This may look like C code, but it is really -*- C++ -*- // Point3Df.cc // (C) 2002 Bill Lenhart #include #include "Point3Df.h" ////////////////////// // class Point3Df // ////////////////////// // Default constructor: Initializes point to (0,0,0) // Note use of initializer syntax instead of assignment Point3Df::Point3Df() : x(0), y(0), z(0) {} // Initializes point to (x,y,z) Point3Df::Point3Df(float xval, float yval, float zval) : x(xval), y(yval), z(zval) {} // Copy Constructor Point3Df::Point3Df(const Point3Df& other) { // Warning: Avoid this "lazy constructor" trick if you have // dynamically allocated member values *this = other; } // Copies other to this Point3Df& Point3Df::operator=(const Point3Df& other) { int i; if (this != &other) { x = other.x; y = other.y; z = other.z; } return *this; } // Destructor: Does nothing Point3Df::~Point3Df() {} // coordinate-wise addition of self with other Point3Df& Point3Df::operator+=(const Point3Df& other) { x += other.x; y += other.y; z += other.z; return *this; } // coordinate-wise subtraction of self with other Point3Df& Point3Df::operator-=(const Point3Df& other) { x -= other.x; y -= other.y; z -= other.z; return *this; } // coordinate-wise multiplication of self with other Point3Df& Point3Df::operator*=(const Point3Df& other) { x *= other.x; y *= other.y; z *= other.z; return *this; } // add, subtract, multiply, every element by scalar Point3Df& Point3Df::operator+=(const float scalar) { x += scalar; y += scalar; z += scalar; return *this; } Point3Df& Point3Df::operator-=(const float scalar) { x -= scalar; y -= scalar; z -= scalar; return *this; } Point3Df& Point3Df::operator*=(const float scalar) { x *= scalar; y *= scalar; z *= scalar; return *this; } // Vector component-wise addition, subtraction and multiplication Point3Df Point3Df::operator+(const Point3Df& other) const { Point3Df temp = *this; temp += other; return temp; } Point3Df Point3Df::operator-(const Point3Df& other) const { Point3Df temp = *this; temp -= other; return temp; } Point3Df Point3Df::operator*(const Point3Df& other) const { Point3Df temp = *this; temp *= other; return temp; } // dot product float Point3Df::dot(const Point3Df& other) const { return this->x*other.x + this->y*other.y + this->z*other.z; } // magnitude of vector float Point3Df::norm(void) const { return sqrt((*this).dot(*this)); } // scale point to unit length Point3Df& Point3Df::normalize(void) { (*this)*=1.0/this->norm(); // scale point down return (*this); } // operators for addition, subtraction, multiplication by scalar // always put vector on the left Point3Df Point3Df::operator+(const float scalar) const { Point3Df temp = *this; temp += scalar; return temp; } Point3Df Point3Df::operator-(const float scalar) const { Point3Df temp = *this; temp -= scalar; return temp; } Point3Df Point3Df::operator*(const float scalar) const { Point3Df temp = *this; temp *= scalar; return temp; } // I/O functions // append point to end of stream ostream& Point3Df::out(ostream& o) const { o << '('; o << x << ',' << y << ',' << z << ')'; return o; } // read array from stream istream& Point3Df::in(istream& is) { int i; char c; is >> c; if (c != '(') { cout << "Bad format for Point3Df" << endl; exit(1); } is >> x >> c; if (c != ',') { cout << "Bad format for Point3Df" << endl; exit(1); } is >> y >> c; if (c != ',') { cout << "Bad format for Point3Df" << endl; exit(1); } is >> z >> c; if (c != ')') { cout << "Bad format for Point3Df" << endl; exit(1); } return is; } // friends // read a point: a binary operator istream& operator>>(istream& is, Point3Df& p) {return p.in(is);} // print a point: a binary operator ostream& operator<<(ostream& o, const Point3Df& p) {return p.out(o);}