From c70c079efc4fcfe9e9b2c29cd82f67faa63c827f Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Tue, 18 Jun 2024 11:45:38 +0200 Subject: [PATCH] Add spherical subtract --- Spherical.cpp | 14 ++++++++++++++ Spherical.h | 5 +++++ test/Spherical_test.cc | 6 +++--- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Spherical.cpp b/Spherical.cpp index b521c77..b09fda0 100644 --- a/Spherical.cpp +++ b/Spherical.cpp @@ -71,6 +71,19 @@ Spherical Spherical::operator-() const { return v; } +Spherical Spherical::operator-(const Spherical &s2) const { + // let's do it the easy way... + Vector3 v1 = Vector3(*this); + Vector3 v2 = Vector3(s2); + Vector3 v = v1 - v2; + Spherical r = Spherical(v); + return r; +} +Spherical Spherical::operator-=(const Spherical &v) { + *this = *this - v; + return *this; +} + Spherical Spherical::operator+(const Spherical &s2) const { // let's do it the easy way... Vector3 v1 = Vector3(*this); @@ -140,6 +153,7 @@ Spherical Spherical::operator*=(float f) { this->distance *= f; return *this; } + Spherical Passer::LinearAlgebra::operator/(const Spherical &v, float f) { return Spherical(v.distance / f, v.horizontalAngle, v.verticalAngle); } diff --git a/Spherical.h b/Spherical.h index 3bbf72e..4e285e3 100644 --- a/Spherical.h +++ b/Spherical.h @@ -90,6 +90,11 @@ public: /// vertically. Distance will stay the same. Spherical operator-() const; + /// @brief Subtract a spherical vector from this vector + /// @param v The vector to subtract + /// @return The result of the subtraction + Spherical operator-(const Spherical &v) const; + Spherical operator-=(const Spherical &v); /// @brief Add a spherical vector to this vector /// @param v The vector to add /// @return The result of the addition diff --git a/test/Spherical_test.cc b/test/Spherical_test.cc index 5e89ce2..0ffae7f 100644 --- a/test/Spherical_test.cc +++ b/test/Spherical_test.cc @@ -142,9 +142,9 @@ TEST(Spherical, Addition) { v2 = Spherical(1, -45, 0); r = v1 + v2; - EXPECT_FLOAT_EQ(r.distance, sqrtf(2)) << "Addition(0 0 0)"; - EXPECT_FLOAT_EQ(r.horizontalAngle, 0) << "Addition(0 0 0)"; - EXPECT_FLOAT_EQ(r.verticalAngle, 0) << "Addition(0 0 0)"; + EXPECT_FLOAT_EQ(r.distance, sqrtf(2)) << "Addition(1 -45 0)"; + EXPECT_FLOAT_EQ(r.horizontalAngle, 0) << "Addition(1 -45 0)"; + EXPECT_FLOAT_EQ(r.verticalAngle, 0) << "Addition(1 -45 0)"; v2 = Spherical(1, 0, 90); r = v1 + v2;