1 #include "vector.h"
2 #include <math.h>
3 #include <string.h>
4
5 bool Vector::operator<(const Vector& u) const {
6 return((v_[0]<u.v_[0]) ||
7 (v_[0]==u.v_[0] && v_[1]<u.v_[1]) ||
8 (v_[0]==u.v_[0] && v_[1]==u.v_[1] && v_[2]<u.v_[2])
9 );
10 }
11
12 Vector& Vector::operator*=(const Vector& u) { // cross product
13 double x = v_[0];
14 double y = v_[1];
15 double z = v_[2];
16 v_[0] = y*u.v_[2] - z*u.v_[1];
17 v_[1] = z*u.v_[0] - x*u.v_[2];
18 v_[2] = x*u.v_[1] - y*u.v_[0];
19 return *this;
20 }
21
22 Vector Vector::normalized() const {
23 double L = sqrt(v_[0]*v_[0] + v_[1]*v_[1] + v_[2]*v_[2]);
24 if(fabs(L) == 0.0) {
25 return Vector(0,0,0);
26 } else {
27 return Vector(v_[0]/L, v_[1]/L, v_[2]/L);
28 }
29 }
30
31 bool Vector::normalize() {
32 double L = sqrt(v_[0]*v_[0] + v_[1]*v_[1] + v_[2]*v_[2]);
33 if(fabs(L) == 0.0) {
34 return false;
35 } else {
36 v_[0] /= L;
37 v_[1] /= L;
38 v_[2] /= L;
39 return true;
40 }
41 }
42
43 //
44 // IO
45 //
46
47 ostream& operator<<(ostream& s, const Vector& u) {
48 s << "(" << u[0] << "," << u[1] << "," << u[2] << ")";
49 return s;
50 }
51
52 istream& operator>>(istream& s, Vector& u) {
53 char ch;
54
55 s >> ch;
56 if(ch!='(') { s.clear(ios::badbit); return s; }
57
58 s >> u[0];
59 if(!s.good()) { return s; }
60
61 s >> ch;
62 if(ch!=',') { s.clear(ios::badbit); return s; }
63
64 s >> u[1];
65 if(!s.good()) { return s; }
66
67 s >> ch;
68 if(ch!=',') { s.clear(ios::badbit); return s; }
69
70 s >> u[2];
71 if(!s.good()) { return s; }
72
73 s >> ch;
74 if(ch!=')') { s.clear(ios::badbit); return s; }
75
76 return s;
77 }
78