diff options
Diffstat (limited to 'vec.h')
-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 |