From 699287623d51be688f2af0a01fedcf90d035da13 Mon Sep 17 00:00:00 2001 From: Keuin Date: Wed, 20 Apr 2022 22:04:37 +0800 Subject: Bugfix: vec3::cross was implemented and tested incorrectly. --- test_vec.cpp | 10 ++++++++-- vec.h | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/test_vec.cpp b/test_vec.cpp index 9739160..1c9390f 100644 --- a/test_vec.cpp +++ b/test_vec.cpp @@ -25,15 +25,21 @@ TEST(Vec, VecMin) { } TEST(Vec, CrossProduct) { - vec3i a{1, 1, 1}, b{2, 2, 2}, c{3, 4, 5}, d{6, 7, 8}, e{-3, -6, -3}; + vec3i a{1, 1, 1}, b{2, 2, 2}, c{3, 4, 5}, d{6, 7, 8}, e{-3, 6, -3}; ASSERT_EQ(cross(a, b), vec3i{}); ASSERT_EQ(a.cross(b), vec3i{}); ASSERT_EQ(cross(c, d), e); ASSERT_EQ(c.cross(d), e); - vec3d f{3, 4, 5}, g{6, 7, 8}, h{-3, -6, -3}; + vec3d f{3, 4, 5}, g{6, 7, 8}, h{-3, 6, -3}; ASSERT_EQ(cross(f, g), h); ASSERT_EQ(f.cross(g), h); + + vec3d u{2, -2, -2}, v{2.5, -5, -2.5}, w{-5, 0, -5}; + ASSERT_EQ(cross(u, v), w); + + vec3d x{1.6, 3.5, 11}, y{3, 7, 15}, z{-24.5, 9, 0.7}; + ASSERT_EQ(cross(x, y), z); } TEST(Vec, DotProduct) { diff --git a/vec.h b/vec.h index e3e15b8..84cbaa3 100644 --- a/vec.h +++ b/vec.h @@ -76,7 +76,7 @@ struct vec3 { // cross product (aka outer product, or vector product, producing a vector) vec3 cross(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}; + return vec3{.x=y * b.z - z * b.y, .y=z * b.x - x * b.z, .z=x * b.y - y * b.x}; } // Multiply with b on every dimension. -- cgit v1.2.3