Skip to content

Commit ccc808e

Browse files
committed
Fix LatLng::operator== to treat ±180° longitude as equal
1 parent db31aa7 commit ccc808e

3 files changed

Lines changed: 27 additions & 2 deletions

File tree

include/LatLng.hpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,22 @@ class LatLng {
3535
LatLng& operator=(const LatLng & other) = default;
3636

3737
bool operator==(const LatLng & other) const {
38-
return isCoordinateEqual(lat, other.lat) &&
39-
isCoordinateEqual(lng, other.lng);
38+
return isCoordinateEqual(lat, other.lat) &&
39+
isLngEqual(lng, other.lng);
4040
}
4141

4242

4343
private:
4444
bool isCoordinateEqual(double first, double second) const {
4545
return std::fabs(first - second) < 1e-12;
4646
}
47+
48+
// Longitude wraps: 180° and -180° are the same meridian.
49+
bool isLngEqual(double a, double b) const {
50+
double diff = std::fabs(std::fmod(a - b, 360.0));
51+
if (diff > 180.0) diff = 360.0 - diff;
52+
return diff < 1e-12;
53+
}
4754
};
4855

4956
#endif // GEOMETRY_LIBRARY_LATLNG

tests/LatLng/operator_equal.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <gtest/gtest.h>
2+
3+
#include "LatLng.hpp"
4+
5+
TEST(LatLng, operator_equal) {
6+
// Basic equality and inequality
7+
EXPECT_TRUE (LatLng( 0, 0) == LatLng( 0, 0));
8+
EXPECT_FALSE(LatLng( 0, 0) == LatLng( 1, 0));
9+
EXPECT_FALSE(LatLng( 0, 0) == LatLng( 0, 1));
10+
EXPECT_FALSE(LatLng( 1, 0) == LatLng(-1, 0));
11+
12+
// ±180° is the same meridian
13+
EXPECT_TRUE(LatLng( 0, 180) == LatLng( 0, -180));
14+
EXPECT_TRUE(LatLng( 45, 180) == LatLng( 45, -180));
15+
EXPECT_TRUE(LatLng(-45, 180) == LatLng(-45, -180));
16+
}

tests/Tests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/** Including all tests */
2+
#include "LatLng/operator_equal.hpp"
3+
24
#include "MathUtil/mod.hpp"
35

46
#include "SphericalUtil/interpolate.hpp"

0 commit comments

Comments
 (0)