From 345285786fe5dc55d38506fde24b8c070e08fd13 Mon Sep 17 00:00:00 2001 From: savilli <78875209+savilli@users.noreply.github.com> Date: Mon, 8 Jan 2024 01:41:39 +0100 Subject: [PATCH] Make vector comparison operators transitive --- include/vector2d.h | 29 +++++++++++++++-------------- include/vector3d.h | 36 ++++++++++++++++-------------------- 2 files changed, 31 insertions(+), 34 deletions(-) diff --git a/include/vector2d.h b/include/vector2d.h index 549661ad..139ee7a5 100644 --- a/include/vector2d.h +++ b/include/vector2d.h @@ -75,36 +75,37 @@ public: return *(&X+index); } - //! sort in order X, Y. Equality with rounding tolerance. + //! sort in order X, Y. bool operator<=(const vector2d&other) const { - return (X other); } - //! sort in order X, Y. Equality with rounding tolerance. + //! sort in order X, Y. bool operator>=(const vector2d&other) const { - return (X>other.X || core::equals(X, other.X)) || - (core::equals(X, other.X) && (Y>other.Y || core::equals(Y, other.Y))); + return !(*this < other); } - //! sort in order X, Y. Difference must be above rounding tolerance. + //! sort in order X, Y. bool operator<(const vector2d&other) const { - return (X(const vector2d&other) const { - return (X>other.X && !core::equals(X, other.X)) || - (core::equals(X, other.X) && Y>other.Y && !core::equals(Y, other.Y)); + return X > other.X || (X == other.X && Y > other.Y); } - bool operator==(const vector2d& other) const { return equals(other); } - bool operator!=(const vector2d& other) const { return !equals(other); } + bool operator==(const vector2d& other) const { + return X == other.X && Y == other.Y; + } + + bool operator!=(const vector2d& other) const { + return !(*this == other); + } // functions diff --git a/include/vector3d.h b/include/vector3d.h index 7c6b98a4..2817287f 100644 --- a/include/vector3d.h +++ b/include/vector3d.h @@ -68,52 +68,48 @@ namespace core return *(&X+index); } - //! sort in order X, Y, Z. Equality with rounding tolerance. + //! sort in order X, Y, Z. bool operator<=(const vector3d&other) const { - return (X other); } - //! sort in order X, Y, Z. Equality with rounding tolerance. + //! sort in order X, Y, Z. bool operator>=(const vector3d&other) const { - return (X>other.X || core::equals(X, other.X)) || - (core::equals(X, other.X) && (Y>other.Y || core::equals(Y, other.Y))) || - (core::equals(X, other.X) && core::equals(Y, other.Y) && (Z>other.Z || core::equals(Z, other.Z))); + return !(*this < other); } - //! sort in order X, Y, Z. Difference must be above rounding tolerance. + //! sort in order X, Y, Z. bool operator<(const vector3d&other) const { - return (X(const vector3d&other) const { - return (X>other.X && !core::equals(X, other.X)) || - (core::equals(X, other.X) && Y>other.Y && !core::equals(Y, other.Y)) || - (core::equals(X, other.X) && core::equals(Y, other.Y) && Z>other.Z && !core::equals(Z, other.Z)); + return X > other.X || (X == other.X && Y > other.Y) || + (X == other.X && Y == other.Y && Z > other.Z); } - //! use weak float compare bool operator==(const vector3d& other) const { - return this->equals(other); + return X == other.X && Y == other.Y && Z == other.Z; } bool operator!=(const vector3d& other) const { - return !this->equals(other); + return !(*this == other); } // functions - //! returns if this vector equals the other one, taking floating point rounding errors into account + //! Checks if this vector equals the other one. + /** Takes floating point rounding errors into account. + \param other Vector to compare with. + \return True if the two vector are (almost) equal, else false. */ bool equals(const vector3d& other) const { return core::equals(X, other.X) && core::equals(Y, other.Y) && core::equals(Z, other.Z);