#include "stdstuff.h" #include "Vector.h" Vector::Vector () { i = j = k = 0; } Vector::Vector (double i, double j, double k) { Vector::i = i; Vector::j = j; Vector::k = k; } double Vector::magnitude () const { return sqrt ((i * i) + (j * j) + (k * k)); } double Vector::dot (const Vector &otherVector) const { return (i * otherVector.i) + (j * otherVector.j) + (k * otherVector.k); } Vector Vector::cross (const Vector &otherVector) const { Vector result; result.i = (j * otherVector.k) - (k * otherVector.j); result.j = (k * otherVector.i) - (i * otherVector.k); result.k = (i * otherVector.j) - (j * otherVector.i); return result; } void Vector::add (const Vector &otherVector) { i += otherVector.i; j += otherVector.j; k += otherVector.k; } void Vector::unitize () { // it would be a serious mistake to use magnitude in the /= // statemente. the magnitude would change as we went along. double m = magnitude(); if (m == 0) { throw domain_error ("cannot unitize zero vector"); } i /= m; j /= m; k /= m; } void Vector::read (istream &is) { is >> i >> j >> k; } void Vector::write (ostream &os) { // this is ok but the below is nicer! // os << i << "i + " << j << "j + " << k << "k"; os << i << "i "; if (j<0) { os << "- " << -j; } else { os << "+ " << j; } os << "j "; if (k<0) { os << "- " << -k; } else { os << "+ " << k; } os << "k"; }