diff options
author | Keuin <[email protected]> | 2022-04-15 11:11:08 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2022-04-15 11:11:08 +0800 |
commit | 2ed39ad4eb9ebf64768dbf4aeefafc5ebb072ca7 (patch) | |
tree | 1637098a98c764ff5a5558ea64e89d3431507786 | |
parent | 5f9a664b0f0955cf3e49b5da9ce6d5835cf5f73f (diff) |
Add vec3::is_zero and vec3::reflect. (not tested)
-rw-r--r-- | vec.h | 12 |
1 files changed, 12 insertions, 0 deletions
@@ -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 |