1
+ #include < Vec3.hpp>
2
+
3
+ #include < math.h>
4
+ #include < iostream>
5
+
6
+ Vec3::Vec3 () : c{0.0 , 0.0 , 0.0 } {}
7
+
8
+ Vec3::Vec3 (double c1, double c2, double c3) : c{c1, c2, c3} {}
9
+
10
+ double Vec3::x () const { return c[0 ]; }
11
+
12
+ double Vec3::y () const { return c[1 ]; }
13
+
14
+ double Vec3::z () const { return c[2 ]; }
15
+
16
+ double Vec3::operator [](int i) const { return c.at (i); }
17
+
18
+ double &Vec3::operator [](int i) { return c.at (i); }
19
+
20
+ Vec3 &Vec3::operator +=(const Vec3 &otherV)
21
+ {
22
+ c[0 ] += otherV.x ();
23
+ c[1 ] += otherV.y ();
24
+ c[2 ] += otherV.z ();
25
+ return *this ;
26
+ }
27
+
28
+ Vec3 &Vec3::operator *=(double s)
29
+ {
30
+ c[0 ] *= s;
31
+ c[1 ] *= s;
32
+ c[2 ] *= s;
33
+ return *this ;
34
+ }
35
+
36
+ Vec3 &Vec3::operator /=(double s)
37
+ {
38
+ c[0 ] /= s;
39
+ c[1 ] /= s;
40
+ c[2 ] /= s;
41
+ return *this ;
42
+ }
43
+
44
+ Vec3 Vec3::operator -() const { return Vec3 (-c[0 ], -c[1 ], -c[2 ]); }
45
+
46
+ double Vec3::len () const { return sqrt (c[0 ] * c[0 ] + c[1 ] * c[1 ] + c[2 ] * c[2 ]); }
47
+
48
+ void Vec3::formatColor (std::ostream &out) const
49
+ {
50
+ out << static_cast <int >(255.999 * c[0 ]) << ' '
51
+ << static_cast <int >(255.999 * c[1 ]) << ' '
52
+ << static_cast <int >(255.999 * c[2 ]) << ' \n ' ;
53
+ }
54
+
55
+ inline Vec3 Vec3::operator +(const Vec3 &otherV) const
56
+ {
57
+ return Vec3 (c[0 ] + otherV.x (), c[1 ] + otherV.y (), c[2 ] + otherV.z ());
58
+ }
59
+
60
+ inline Vec3 Vec3::operator -(const Vec3 &otherV) const
61
+ {
62
+ return Vec3 (c[0 ] - otherV.x (), c[1 ] - otherV.y (), c[2 ] - otherV.z ());
63
+ }
64
+
65
+ inline Vec3 Vec3::operator *(const Vec3 &otherV) const
66
+ {
67
+ return Vec3 (c[0 ] * otherV.x (), c[1 ] * otherV.y (), c[2 ] * otherV.z ());
68
+ }
69
+
70
+ inline Vec3 Vec3::operator *(double s) const
71
+ {
72
+ return Vec3 (c[0 ] * s, c[1 ] * s, c[2 ] * s);
73
+ }
74
+
75
+ inline Vec3 Vec3::operator /(double s) const
76
+ {
77
+ return Vec3 (c[0 ] / s, c[1 ] / s, c[2 ] / s);
78
+ }
79
+
80
+ inline Vec3 Vec3::getUnitVector () const { return *this / len (); }
81
+
82
+ inline double Vec3::o (const Vec3 &otherV) const
83
+ {
84
+ return c[0 ] * otherV.x () + c[1 ] * otherV.y () + c[2 ] * otherV.z ();
85
+ }
86
+
87
+ inline Vec3 Vec3::x (const Vec3 &otherV) const
88
+ {
89
+ return Vec3 (c[1 ] * otherV.z () - c[2 ] * otherV.y (),
90
+ c[2 ] * otherV.x () - c[0 ] * otherV.z (),
91
+ c[0 ] * otherV.y () - c[1 ] * otherV.x ());
92
+ }
93
+
94
+ inline std::ostream &operator <<(std::ostream &out, const Vec3 &v)
95
+ {
96
+ return out << v.x () << ' ' << v.y () << ' ' << v.z ();
97
+ }
98
+
99
+ inline Vec3 operator *(double s, const Vec3 &v)
100
+ {
101
+ return v * s;
102
+ }
0 commit comments