diff options
author | Keuin <[email protected]> | 2022-04-11 10:24:03 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2022-04-11 10:24:22 +0800 |
commit | cdb0bdfc60481708ab1a9707c0e1e4458e6396bf (patch) | |
tree | 22a36ffb85e9c35c4a88d48508a6bae277ebac1d | |
parent | 3fa02569b6af28de0cefd383f4b1cdf8d7d9f6b3 (diff) |
Test vec3 minus and cross product. Fix missing const qualifier in overloaded operators.
-rw-r--r-- | test.cpp | 19 | ||||
-rw-r--r-- | vec.h | 12 |
2 files changed, 25 insertions, 6 deletions
@@ -12,4 +12,23 @@ TEST(Vec, VecAdd) { vec3d d{1.1, 2.2, 3.3}, e{4.4, 5.5, 6.6}, f{5.5, 7.7, 9.9}; ASSERT_EQ(d + e, f); +} + +TEST(Vec, VecMin) { + vec3i a{1, 1, 1}, b{2, 2, 2}, c{-1, -1, -1}; + ASSERT_EQ(a - b, c); + ASSERT_EQ(-a, c); + + vec3d d{1.1, 2.2, 3.3}, e{-4.4, -5.5, -6.6}, f{5.5, 7.7, 9.9}; + ASSERT_EQ(d - e, f); + ASSERT_EQ(d + (-e), f); +} + +TEST(Vec, VecCrossProduct) { + vec3i a{1, 1, 1}, b{2, 2, 2}, c{3, 4, 5}, d{6, 7, 8}, e{-3, -6, -3}; + ASSERT_EQ(a * b, vec3i{}); + ASSERT_EQ(c * d, e); + + vec3d f{3.0, 4.0, 5.0}, g{6.0, 7.0, 8.0}, h{-3, -6, -3}; + ASSERT_EQ(f * g, h); }
\ No newline at end of file @@ -27,7 +27,7 @@ struct vec3 { T y; T z; - vec3 operator+(vec3 &b) const { + vec3 operator+(const vec3 &b) const { return vec3{.x=x + b.x, .y=y + b.y, .z=z + b.z}; } @@ -35,26 +35,26 @@ struct vec3 { return vec3{.x = -x, .y = -y, .z = -z}; } - vec3 operator-(vec3 &b) const { + vec3 operator-(const vec3 &b) const { return vec3{.x=x - b.x, .y=y - b.y, .z=z - b.z}; } // cross product - vec3 operator*(vec3 &b) const { + vec3 operator*(const vec3 &b) const { return vec3{.x=y * b.z - z * b.y, .y=x * b.z - z * b.x, .z=x * b.y - y * b.x}; } - bool operator==(vec3 b) const { + bool operator==(const vec3 b) const { return eq(x, b.x) && eq(y, b.y) && eq(z, b.z); } // dot product - int dot(vec3 &b) const { + int dot(const vec3 &b) const { return x * b.x + y * b.y + z * b.z; } // norm value - int norm(int level = 2) const { + int norm(const int level = 2) const { if (level == 2) { return std::abs(x * x + y * y + z * z); } else if (level == 1) { |