summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--test.cpp7
-rw-r--r--vec.h5
2 files changed, 12 insertions, 0 deletions
diff --git a/test.cpp b/test.cpp
index 8c450f9..1b765aa 100644
--- a/test.cpp
+++ b/test.cpp
@@ -67,4 +67,11 @@ TEST(Vec, Norm) {
TEST(Vec, UnitVec) {
vec3d a{1, 2, 2}, b{1.0 / 3, 2.0 / 3, 2.0 / 3};
ASSERT_EQ(a.unit_vec(), b);
+}
+
+TEST(Vec, Mod2) {
+ vec3i a{1, 2, 3};
+ vec3d b{2.5, 3, 1.2};
+ ASSERT_EQ(a.mod2(), 14);
+ ASSERT_LE(abs(b.mod2() - (2.5 * 2.5 + 3 * 3 + 1.2 * 1.2)), 1e-10);
} \ No newline at end of file
diff --git a/vec.h b/vec.h
index 017b0a9..bf40e7d 100644
--- a/vec.h
+++ b/vec.h
@@ -71,6 +71,11 @@ struct vec3 {
}
}
+ // Squared module
+ T mod2() const {
+ return x * x + y * y + z * z;
+ }
+
vec3 unit_vec() const {
return *this * (1.0 / norm());
}