Skip to content

Commit 15b978c

Browse files
committed
Reconcile the three versions of vec3.h
1 parent d160727 commit 15b978c

File tree

3 files changed

+109
-110
lines changed

3 files changed

+109
-110
lines changed

src/InOneWeekend/vec3.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include <stdlib.h>
1717

1818

19-
class vec3 {
19+
class vec3 {
2020
public:
2121
vec3() {}
2222
vec3(float e0, float e1, float e2) { e[0] = e0; e[1] = e1; e[2] = e2; }

src/TheNextWeek/vec3.h

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,37 @@
1616
#include <stdlib.h>
1717

1818

19-
class vec3 {
20-
21-
22-
public:
23-
vec3() {}
24-
vec3(float e0, float e1, float e2) { e[0] = e0; e[1] = e1; e[2] = e2; }
25-
inline float x() const { return e[0]; }
26-
inline float y() const { return e[1]; }
27-
inline float z() const { return e[2]; }
28-
inline float r() const { return e[0]; }
29-
inline float g() const { return e[1]; }
30-
inline float b() const { return e[2]; }
31-
32-
inline const vec3& operator+() const { return *this; }
33-
inline vec3 operator-() const { return vec3(-e[0], -e[1], -e[2]); }
34-
inline float operator[](int i) const { return e[i]; }
35-
inline float& operator[](int i) { return e[i]; };
36-
37-
inline vec3& operator+=(const vec3 &v2);
38-
inline vec3& operator-=(const vec3 &v2);
39-
inline vec3& operator*=(const vec3 &v2);
40-
inline vec3& operator/=(const vec3 &v2);
41-
inline vec3& operator*=(const float t);
42-
inline vec3& operator/=(const float t);
43-
44-
inline float length() const { return sqrt(e[0]*e[0] + e[1]*e[1] + e[2]*e[2]); }
45-
inline float squared_length() const { return e[0]*e[0] + e[1]*e[1] + e[2]*e[2]; }
46-
inline void make_unit_vector();
47-
48-
float e[3];
19+
class vec3 {
20+
public:
21+
vec3() {}
22+
vec3(float e0, float e1, float e2) { e[0] = e0; e[1] = e1; e[2] = e2; }
23+
inline float x() const { return e[0]; }
24+
inline float y() const { return e[1]; }
25+
inline float z() const { return e[2]; }
26+
inline float r() const { return e[0]; }
27+
inline float g() const { return e[1]; }
28+
inline float b() const { return e[2]; }
29+
30+
inline const vec3& operator+() const { return *this; }
31+
inline vec3 operator-() const { return vec3(-e[0], -e[1], -e[2]); }
32+
inline float operator[](int i) const { return e[i]; }
33+
inline float& operator[](int i) { return e[i]; }
34+
35+
inline vec3& operator+=(const vec3 &v2);
36+
inline vec3& operator-=(const vec3 &v2);
37+
inline vec3& operator*=(const vec3 &v2);
38+
inline vec3& operator/=(const vec3 &v2);
39+
inline vec3& operator*=(const float t);
40+
inline vec3& operator/=(const float t);
41+
42+
inline float length() const { return sqrt(e[0]*e[0] + e[1]*e[1] + e[2]*e[2]); }
43+
inline float squared_length() const { return e[0]*e[0] + e[1]*e[1] + e[2]*e[2]; }
44+
inline void make_unit_vector();
45+
46+
float e[3];
4947
};
5048

5149

52-
5350
inline std::istream& operator>>(std::istream &is, vec3 &t) {
5451
is >> t.e[0] >> t.e[1] >> t.e[2];
5552
return is;
@@ -94,61 +91,64 @@ inline vec3 operator*(const vec3 &v, float t) {
9491
}
9592

9693
inline float dot(const vec3 &v1, const vec3 &v2) {
97-
return v1.e[0] *v2.e[0] + v1.e[1] *v2.e[1] + v1.e[2] *v2.e[2];
94+
return v1.e[0] * v2.e[0]
95+
+ v1.e[1] * v2.e[1]
96+
+ v1.e[2] * v2.e[2];
9897
}
9998

10099
inline vec3 cross(const vec3 &v1, const vec3 &v2) {
101-
return vec3(v1.e[1]*v2.e[2] - v1.e[2]*v2.e[1],
102-
v1.e[2]*v2.e[0] - v1.e[0]*v2.e[2],
103-
v1.e[0]*v2.e[1] - v1.e[1]*v2.e[0]);
100+
return vec3(v1.e[1] * v2.e[2] - v1.e[2] * v2.e[1],
101+
v1.e[2] * v2.e[0] - v1.e[0] * v2.e[2],
102+
v1.e[0] * v2.e[1] - v1.e[1] * v2.e[0]);
104103
}
105104

106105
inline vec3& vec3::operator+=(const vec3 &v){
107-
e[0] += v.e[0];
108-
e[1] += v.e[1];
109-
e[2] += v.e[2];
106+
e[0] += v.e[0];
107+
e[1] += v.e[1];
108+
e[2] += v.e[2];
110109
return *this;
111110
}
112111

113112
inline vec3& vec3::operator*=(const vec3 &v){
114-
e[0] *= v.e[0];
115-
e[1] *= v.e[1];
116-
e[2] *= v.e[2];
113+
e[0] *= v.e[0];
114+
e[1] *= v.e[1];
115+
e[2] *= v.e[2];
117116
return *this;
118117
}
119118

120119
inline vec3& vec3::operator/=(const vec3 &v){
121-
e[0] /= v.e[0];
122-
e[1] /= v.e[1];
123-
e[2] /= v.e[2];
120+
e[0] /= v.e[0];
121+
e[1] /= v.e[1];
122+
e[2] /= v.e[2];
124123
return *this;
125124
}
126125

127126
inline vec3& vec3::operator-=(const vec3& v) {
128-
e[0] -= v.e[0];
129-
e[1] -= v.e[1];
130-
e[2] -= v.e[2];
127+
e[0] -= v.e[0];
128+
e[1] -= v.e[1];
129+
e[2] -= v.e[2];
131130
return *this;
132131
}
133132

134133
inline vec3& vec3::operator*=(const float t) {
135-
e[0] *= t;
136-
e[1] *= t;
137-
e[2] *= t;
134+
e[0] *= t;
135+
e[1] *= t;
136+
e[2] *= t;
138137
return *this;
139138
}
140139

141140
inline vec3& vec3::operator/=(const float t) {
142-
float k = 1.0/t;
141+
float k = 1.0f/t;
143142

144-
e[0] *= k;
145-
e[1] *= k;
146-
e[2] *= k;
143+
e[0] *= k;
144+
e[1] *= k;
145+
e[2] *= k;
147146
return *this;
148147
}
149148

150149
inline vec3 unit_vector(vec3 v) {
151150
return v / v.length();
152151
}
153152

153+
154154
#endif

src/TheRestOfYourLife/vec3.h

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,37 @@
1616
#include <stdlib.h>
1717

1818

19-
class vec3 {
20-
21-
22-
public:
23-
vec3() {}
24-
vec3(float e0, float e1, float e2) { e[0] = e0; e[1] = e1; e[2] = e2; }
25-
inline float x() const { return e[0]; }
26-
inline float y() const { return e[1]; }
27-
inline float z() const { return e[2]; }
28-
inline float r() const { return e[0]; }
29-
inline float g() const { return e[1]; }
30-
inline float b() const { return e[2]; }
31-
32-
inline const vec3& operator+() const { return *this; }
33-
inline vec3 operator-() const { return vec3(-e[0], -e[1], -e[2]); }
34-
inline float operator[](int i) const { return e[i]; }
35-
inline float& operator[](int i) { return e[i]; };
36-
37-
inline vec3& operator+=(const vec3 &v2);
38-
inline vec3& operator-=(const vec3 &v2);
39-
inline vec3& operator*=(const vec3 &v2);
40-
inline vec3& operator/=(const vec3 &v2);
41-
inline vec3& operator*=(const float t);
42-
inline vec3& operator/=(const float t);
43-
44-
inline float length() const { return sqrt(e[0]*e[0] + e[1]*e[1] + e[2]*e[2]); }
45-
inline float squared_length() const { return e[0]*e[0] + e[1]*e[1] + e[2]*e[2]; }
46-
inline void make_unit_vector();
47-
48-
49-
float e[3];
19+
class vec3 {
20+
public:
21+
vec3() {}
22+
vec3(float e0, float e1, float e2) { e[0] = e0; e[1] = e1; e[2] = e2; }
23+
inline float x() const { return e[0]; }
24+
inline float y() const { return e[1]; }
25+
inline float z() const { return e[2]; }
26+
inline float r() const { return e[0]; }
27+
inline float g() const { return e[1]; }
28+
inline float b() const { return e[2]; }
29+
30+
inline const vec3& operator+() const { return *this; }
31+
inline vec3 operator-() const { return vec3(-e[0], -e[1], -e[2]); }
32+
inline float operator[](int i) const { return e[i]; }
33+
inline float& operator[](int i) { return e[i]; }
34+
35+
inline vec3& operator+=(const vec3 &v2);
36+
inline vec3& operator-=(const vec3 &v2);
37+
inline vec3& operator*=(const vec3 &v2);
38+
inline vec3& operator/=(const vec3 &v2);
39+
inline vec3& operator*=(const float t);
40+
inline vec3& operator/=(const float t);
41+
42+
inline float length() const { return sqrt(e[0]*e[0] + e[1]*e[1] + e[2]*e[2]); }
43+
inline float squared_length() const { return e[0]*e[0] + e[1]*e[1] + e[2]*e[2]; }
44+
inline void make_unit_vector();
45+
46+
float e[3];
5047
};
5148

5249

53-
5450
inline std::istream& operator>>(std::istream &is, vec3 &t) {
5551
is >> t.e[0] >> t.e[1] >> t.e[2];
5652
return is;
@@ -95,61 +91,64 @@ inline vec3 operator*(const vec3 &v, float t) {
9591
}
9692

9793
inline float dot(const vec3 &v1, const vec3 &v2) {
98-
return v1.e[0] *v2.e[0] + v1.e[1] *v2.e[1] + v1.e[2] *v2.e[2];
94+
return v1.e[0] * v2.e[0]
95+
+ v1.e[1] * v2.e[1]
96+
+ v1.e[2] * v2.e[2];
9997
}
10098

10199
inline vec3 cross(const vec3 &v1, const vec3 &v2) {
102-
return vec3(v1.e[1]*v2.e[2] - v1.e[2]*v2.e[1],
103-
v1.e[2]*v2.e[0] - v1.e[0]*v2.e[2],
104-
v1.e[0]*v2.e[1] - v1.e[1]*v2.e[0]);
100+
return vec3(v1.e[1] * v2.e[2] - v1.e[2] * v2.e[1],
101+
v1.e[2] * v2.e[0] - v1.e[0] * v2.e[2],
102+
v1.e[0] * v2.e[1] - v1.e[1] * v2.e[0]);
105103
}
106104

107105
inline vec3& vec3::operator+=(const vec3 &v){
108-
e[0] += v.e[0];
109-
e[1] += v.e[1];
110-
e[2] += v.e[2];
106+
e[0] += v.e[0];
107+
e[1] += v.e[1];
108+
e[2] += v.e[2];
111109
return *this;
112110
}
113111

114112
inline vec3& vec3::operator*=(const vec3 &v){
115-
e[0] *= v.e[0];
116-
e[1] *= v.e[1];
117-
e[2] *= v.e[2];
113+
e[0] *= v.e[0];
114+
e[1] *= v.e[1];
115+
e[2] *= v.e[2];
118116
return *this;
119117
}
120118

121119
inline vec3& vec3::operator/=(const vec3 &v){
122-
e[0] /= v.e[0];
123-
e[1] /= v.e[1];
124-
e[2] /= v.e[2];
120+
e[0] /= v.e[0];
121+
e[1] /= v.e[1];
122+
e[2] /= v.e[2];
125123
return *this;
126124
}
127125

128126
inline vec3& vec3::operator-=(const vec3& v) {
129-
e[0] -= v.e[0];
130-
e[1] -= v.e[1];
131-
e[2] -= v.e[2];
127+
e[0] -= v.e[0];
128+
e[1] -= v.e[1];
129+
e[2] -= v.e[2];
132130
return *this;
133131
}
134132

135133
inline vec3& vec3::operator*=(const float t) {
136-
e[0] *= t;
137-
e[1] *= t;
138-
e[2] *= t;
134+
e[0] *= t;
135+
e[1] *= t;
136+
e[2] *= t;
139137
return *this;
140138
}
141139

142140
inline vec3& vec3::operator/=(const float t) {
143-
float k = 1.0/t;
141+
float k = 1.0f/t;
144142

145-
e[0] *= k;
146-
e[1] *= k;
147-
e[2] *= k;
143+
e[0] *= k;
144+
e[1] *= k;
145+
e[2] *= k;
148146
return *this;
149147
}
150148

151149
inline vec3 unit_vector(vec3 v) {
152150
return v / v.length();
153151
}
154152

153+
155154
#endif

0 commit comments

Comments
 (0)