Mercurial > hg > tweakathon2ios
comparison 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:a223551fdc1f |
---|---|
1 /* | |
2 * 2dvector.h | |
3 * simplespring | |
4 * | |
5 * Created by Robert Tubb on 01/06/2011. | |
6 * Copyright 2011 __MyCompanyName__. All rights reserved. | |
7 * | |
8 */ | |
9 #ifndef _2DVECTORH | |
10 #define _2DVECTORH | |
11 #include <iostream> | |
12 | |
13 class TwoVector{ | |
14 public: | |
15 double x, y; | |
16 TwoVector(); | |
17 TwoVector(double ax, double ay); | |
18 | |
19 // public methods | |
20 double norm(); | |
21 void setCoord(double ax, double ay); | |
22 TwoVector minus(TwoVector otherPoint); | |
23 TwoVector operator-(TwoVector otherPoint); | |
24 TwoVector operator+(TwoVector otherPoint); | |
25 | |
26 TwoVector operator*(TwoVector otherPoint); | |
27 TwoVector operator*(const double& scalar); // scalar is right operand | |
28 | |
29 //TwoVector operator=(TwoVector otherPoint); | |
30 | |
31 double distanceTo(TwoVector otherPoint); | |
32 | |
33 }; | |
34 using namespace std; | |
35 // output text formatting: (x,y) in super precise output | |
36 inline ostream& operator<<(ostream& ostr, const TwoVector& tvec){ | |
37 ostr.setf(ios_base::fixed,ios_base::floatfield); | |
38 ostr.precision(1); | |
39 | |
40 ostr << "(" << tvec.x << "," << tvec.y << ")"; | |
41 return ostr; | |
42 } | |
43 inline istream& operator>>(istream& istr, TwoVector& tvec){ | |
44 // um | |
45 | |
46 char l_paren , comma, r_paren; | |
47 | |
48 | |
49 if(istr.bad()){ | |
50 cout << "BAD INPUT"; | |
51 return istr; | |
52 } | |
53 | |
54 istr.setf(ios_base::fixed,ios_base::floatfield); | |
55 istr.precision(1); | |
56 | |
57 istr >> l_paren >> tvec.x >> comma >> tvec.y >> r_paren; | |
58 if(l_paren != '('){ | |
59 cout << "BAD INPUT ("; | |
60 return istr; | |
61 } | |
62 | |
63 if(comma != ','){ | |
64 cout << "BAD INPUT ,"; | |
65 return istr; | |
66 } | |
67 | |
68 if(r_paren != ')'){ | |
69 cout << "BAD INPUT )"; | |
70 return istr; | |
71 } | |
72 return istr; | |
73 } | |
74 #endif // #ifndef _2DVECTORH |