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@0: #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@0: TwoVector operator*(const double& scalar); // scalar is right operand rt300@0: rt300@0: //TwoVector operator=(TwoVector otherPoint); rt300@0: rt300@0: double distanceTo(TwoVector otherPoint); rt300@0: rt300@0: }; rt300@0: using namespace std; rt300@0: // output text formatting: (x,y) in super precise output rt300@0: inline ostream& operator<<(ostream& ostr, const TwoVector& tvec){ rt300@0: ostr.setf(ios_base::fixed,ios_base::floatfield); rt300@0: ostr.precision(1); rt300@0: rt300@0: ostr << "(" << tvec.x << "," << tvec.y << ")"; rt300@0: return ostr; rt300@0: } rt300@0: inline istream& operator>>(istream& istr, TwoVector& tvec){ rt300@0: // um rt300@0: rt300@0: char l_paren , comma, r_paren; rt300@0: rt300@0: rt300@0: if(istr.bad()){ rt300@0: cout << "BAD INPUT"; rt300@0: return istr; rt300@0: } rt300@0: rt300@0: istr.setf(ios_base::fixed,ios_base::floatfield); rt300@0: istr.precision(1); rt300@0: rt300@0: istr >> l_paren >> tvec.x >> comma >> tvec.y >> r_paren; rt300@0: if(l_paren != '('){ rt300@0: cout << "BAD INPUT ("; rt300@0: return istr; rt300@0: } rt300@0: rt300@0: if(comma != ','){ rt300@0: cout << "BAD INPUT ,"; rt300@0: return istr; rt300@0: } rt300@0: rt300@0: if(r_paren != ')'){ rt300@0: cout << "BAD INPUT )"; rt300@0: return istr; rt300@0: } rt300@0: return istr; rt300@0: } rt300@0: #endif // #ifndef _2DVECTORH