Mercurial > hg > tweakathon2ios
diff 2dvector.h @ 0:a223551fdc1f
First commit - copy from tweakathlon.
author | Robert Tubb <rt300@eecs.qmul.ac.uk> |
---|---|
date | Fri, 10 Oct 2014 11:46:42 +0100 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/2dvector.h Fri Oct 10 11:46:42 2014 +0100 @@ -0,0 +1,74 @@ +/* + * 2dvector.h + * simplespring + * + * Created by Robert Tubb on 01/06/2011. + * Copyright 2011 __MyCompanyName__. All rights reserved. + * + */ +#ifndef _2DVECTORH +#define _2DVECTORH +#include <iostream> + +class TwoVector{ +public: + double x, y; + TwoVector(); + TwoVector(double ax, double ay); + +// public methods + double norm(); + void setCoord(double ax, double ay); + TwoVector minus(TwoVector otherPoint); + TwoVector operator-(TwoVector otherPoint); + TwoVector operator+(TwoVector otherPoint); + + TwoVector operator*(TwoVector otherPoint); + TwoVector operator*(const double& scalar); // scalar is right operand + + //TwoVector operator=(TwoVector otherPoint); + + double distanceTo(TwoVector otherPoint); + +}; +using namespace std; +// output text formatting: (x,y) in super precise output +inline ostream& operator<<(ostream& ostr, const TwoVector& tvec){ + ostr.setf(ios_base::fixed,ios_base::floatfield); + ostr.precision(1); + + ostr << "(" << tvec.x << "," << tvec.y << ")"; + return ostr; +} +inline istream& operator>>(istream& istr, TwoVector& tvec){ + // um + + char l_paren , comma, r_paren; + + + if(istr.bad()){ + cout << "BAD INPUT"; + return istr; + } + + istr.setf(ios_base::fixed,ios_base::floatfield); + istr.precision(1); + + istr >> l_paren >> tvec.x >> comma >> tvec.y >> r_paren; + if(l_paren != '('){ + cout << "BAD INPUT ("; + return istr; + } + + if(comma != ','){ + cout << "BAD INPUT ,"; + return istr; + } + + if(r_paren != ')'){ + cout << "BAD INPUT )"; + return istr; + } + return istr; +} +#endif // #ifndef _2DVECTORH \ No newline at end of file