#include #include #include "geometry.h" // copy constructor Tuple::Tuple (const Tuple& toCopy) { data[0] = toCopy.data[0]; data[1] = toCopy.data[1]; } // assignment operator Tuple& Tuple::operator = (const Tuple& toCopy) { if (this != &toCopy) { data[0] = toCopy.data[0]; data[1] = toCopy.data[1]; } return *this; } // comparison const bool Tuple::operator == (const Tuple& compare) const { if (data[0]==compare.data[0] && data[1] == compare.data[1] ) return true; return false; } // comparison - not const bool Tuple::operator != (const Tuple& compare) const { return !(*this==compare); } // addition const Tuple Tuple::operator + (const Tuple& toAdd) const { Tuple sum = *this; sum += toAdd; return sum; } // unary addition Tuple& Tuple::operator += (const Tuple& toAdd) { data[0] += toAdd.data[0]; data[1] += toAdd.data[1]; return *this; } // subtraction const Tuple Tuple::operator - (const Tuple& toSub) const { Tuple diff=*this; diff -= toSub; return diff; } // unary subtraction Tuple& Tuple::operator -= (const Tuple& toSub) { data[0] -= toSub.data[0]; data[1] -= toSub.data[1]; return *this; } // negation const Tuple Tuple::operator - () const { Tuple neg = *this; neg *= -1; return neg; } // unary scalar multiplication Tuple& Tuple::operator *= (double scaleFactor) { data[0] *= scaleFactor; data[1] *= scaleFactor; return *this; } // unary scalar multiplication Tuple& Tuple::operator /= (double scaleFactor) { assert(scaleFactor !=0); data[0] /= scaleFactor; data[1] /= scaleFactor; return *this; } // index double& Tuple::operator [] (const int i) { assert(i>=0 && i < 2); return data[i]; } // read-only index const double& Tuple::operator [] (const int i) const { assert(i>=0 && i < 2); return data[i]; } // dot product const double Tuple::dot (const Tuple& toDot) const { double dotProduct = data[0]*toDot.data[0] + data[1]*toDot.data[1]; return dotProduct; } // normalize Tuple& Tuple::normalize () { double len = this->length(); assert(len!=0); data[0] /= len; data[1] /= len; return *this; } Tuple Tuple::getPerpendicular() { Tuple tmp0=Tuple(0,1); Tuple tmp1; tmp1[0]=data[0]; tmp1[1]=data[1]; tmp1.normalize(); // this should not be a problem unless we have 0 length line segments tmp0 = tmp0-tmp0.dot(tmp1)*tmp1; if (tmp0.length()==0) { // tmp0=(0,1)is in same direction as tmp1=this tmp0=Tuple(1,0); // so this will be perpendicular } // tmp0 is perpendicular to tmp1=this but we need consistency in the direction double direction=tmp0[0]*tmp1[1]-tmp0[1]*tmp1[0]; if (direction>0) return tmp0/tmp0.length(); else return -tmp0/tmp0.length(); } // get length const double Tuple::length() const { double sumSquares = data[0]*data[0] + data[1]*data[1]; return (double) sqrt(sumSquares); } // scalar multiplication const Tuple operator * (const Tuple& toMultiply, const double& factor) { Tuple product = toMultiply; product *= factor; return product; } // scalar multiplication const Tuple operator * (const double& factor,const Tuple& toMultiply) { return toMultiply*factor; } // scalar division const Tuple operator / (const Tuple& toDivide, const double& factor) { Tuple quotient = toDivide; quotient /= factor; return quotient; } // write ostream &operator<<(ostream &out_file, const Tuple& theTuple) { out_file << theTuple[0] << ", " << theTuple[1]; return out_file; }