rt300@0: /* rt300@0: * 2dvector.h rt300@0: * simplespring rt300@0: * rt300@0: * Created by Robert Tubb on 01/06/2011. rt300@0: * Copyright 2011 __MyCompanyName__. All rights reserved. rt300@0: * rt300@0: */ rt300@0: #ifndef _2DVECTORH rt300@0: #define _2DVECTORH rt300@3: #include rt300@0: rt300@0: class TwoVector{ rt300@0: public: rt300@0: double x, y; rt300@0: TwoVector(); rt300@0: TwoVector(double ax, double ay); rt300@0: rt300@0: // public methods rt300@0: double norm(); rt300@0: void setCoord(double ax, double ay); rt300@0: TwoVector minus(TwoVector otherPoint); rt300@0: TwoVector operator-(TwoVector otherPoint); rt300@0: TwoVector operator+(TwoVector otherPoint); rt300@0: rt300@0: TwoVector operator*(TwoVector otherPoint); rt300@3: TwoVector operator*(const double& scalar); // scalar is right operand rt300@3: rt300@3: //TwoVector operator=(TwoVector otherPoint); rt300@3: rt300@0: rt300@3: rt300@3: rt300@0: double distanceTo(TwoVector otherPoint); rt300@0: rt300@0: rt300@3: rt300@0: }; rt300@3: using namespace std; rt300@3: // output text formatting: (x,y) in super precise output rt300@3: inline ostream& operator<<(ostream& ostr, const TwoVector& tvec){ rt300@3: ostr.setf(ios_base::fixed,ios_base::floatfield); rt300@4: ostr.precision(1); rt300@4: rt300@3: ostr << "(" << tvec.x << "," << tvec.y << ")"; rt300@3: return ostr; rt300@3: } rt300@3: inline istream& operator>>(istream& istr, TwoVector& tvec){ rt300@3: // um rt300@0: rt300@3: char l_paren , comma, r_paren; rt300@3: rt300@3: rt300@3: if(istr.bad()){ rt300@3: cout << "BAD INPUT"; rt300@3: return istr; rt300@3: } rt300@4: rt300@3: istr.setf(ios_base::fixed,ios_base::floatfield); rt300@4: istr.precision(1); rt300@3: rt300@4: istr >> l_paren >> tvec.x >> comma >> tvec.y >> r_paren; rt300@3: if(l_paren != '('){ rt300@4: cout << "BAD INPUT ("; rt300@3: return istr; rt300@3: } rt300@4: rt300@3: if(comma != ','){ rt300@4: cout << "BAD INPUT ,"; rt300@3: return istr; rt300@3: } rt300@4: rt300@3: if(r_paren != ')'){ rt300@4: cout << "BAD INPUT )"; rt300@3: return istr; rt300@3: } rt300@3: return istr; rt300@3: } rt300@0: #endif // #ifndef _2DVECTORH