1 //
2 // Computergraphik II
3 // Prof. Dr. Juergen Doellner
4 // Wintersemester 2001/02
5 //
6 // Rahmenprogramm zu Aufgabenzettel 9
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
62 s >> u[0];
63 if(!s.good()) { return s; }
64
65 s >> u[1];
66 if(!s.good()) { return s; }
67
68 s >> u[2];
69 if(!s.good()) { return s; }
70
71 return s;
72 }
73