Add spherical subtract

This commit is contained in:
Pascal Serrarens 2024-06-18 11:45:38 +02:00
parent 2bad384611
commit c70c079efc
3 changed files with 22 additions and 3 deletions

View File

@ -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);
}

View File

@ -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

View File

@ -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;