annotate 2dvector.h @ 49:178642d134a7 tip

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