summaryrefslogtreecommitdiff
path: root/vec.h
diff options
context:
space:
mode:
authorKeuin <[email protected]>2022-04-15 11:11:08 +0800
committerKeuin <[email protected]>2022-04-15 11:11:08 +0800
commit2ed39ad4eb9ebf64768dbf4aeefafc5ebb072ca7 (patch)
tree1637098a98c764ff5a5558ea64e89d3431507786 /vec.h
parent5f9a664b0f0955cf3e49b5da9ce6d5835cf5f73f (diff)
Add vec3::is_zero and vec3::reflect. (not tested)
Diffstat (limited to 'vec.h')
-rw-r--r--vec.h12
1 files changed, 12 insertions, 0 deletions
diff --git a/vec.h b/vec.h
index 8efbc9a..64eaaca 100644
--- a/vec.h
+++ b/vec.h
@@ -35,6 +35,12 @@ struct vec3 {
return vec3{0, 0, 0};
}
+ bool is_zero() const {
+#define EPS (1e-8)
+ return x >= -EPS && y >= -EPS && z >= -EPS && x <= EPS && y <= EPS && z <= EPS;
+#undef EPS
+ }
+
vec3 operator+(const vec3 &b) const {
return vec3{.x=x + b.x, .y=y + b.y, .z=z + b.z};
}
@@ -80,6 +86,12 @@ struct vec3 {
vec3 unit_vec() const {
return *this * (1.0 / norm());
}
+
+ // Get the reflected vector. Current vector is the incoming vector, n is the normal vector (length should be 1).
+ vec3 reflect(const vec3 &n) const {
+ assert(fabs(n.mod2() - 1.0) < 1e-8);
+ return *this - 2.0 * dot(*this, n) * n;
+ }
};
// print to ostream