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