Skip to content

Commit ff7b2c9

Browse files
committed
Correcting semantic usage of overloaded CloudLocation::operator
When using the - operator with a location one would typically expect the result being a new location which coordinates are calculated by LocNew.lat = Loc.lat - otherLoc.lat and LocNew.lon = Loc.lon - otherLoc.lon - and not a float value representing the euclidean distance.
1 parent 0e2f786 commit ff7b2c9

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

types/CloudLocation.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,18 @@ class Location {
4242
lon = aLocation.lon;
4343
return *this;
4444
}
45-
float operator-(Location& aLocation) {
46-
return sqrt(pow(lat - aLocation.lat, 2) + pow(lon - aLocation.lon, 2));
45+
Location operator-(Location& aLocation) {
46+
return Location(lat - aLocation.lat, lon - aLocation.lon);
4747
}
4848
bool operator==(Location& aLocation) {
4949
return lat == aLocation.lat && lon == aLocation.lon;
5050
}
5151
bool operator!=(Location& aLocation) {
5252
return !(operator==(aLocation));
5353
}
54+
static float distance(Location& loc1, Location& loc2) {
55+
return sqrt(pow(loc1.lat - loc2.lat, 2) + pow(loc1.lon - loc2.lon, 2));
56+
}
5457
};
5558

5659
class CloudLocation : public ArduinoCloudProperty {
@@ -61,7 +64,7 @@ class CloudLocation : public ArduinoCloudProperty {
6164
CloudLocation() : _value(0, 0), _cloud_value(0, 0) {}
6265
CloudLocation(float lat, float lon) : _value(lat, lon), _cloud_value(lat, lon) {}
6366
virtual bool isDifferentFromCloud() {
64-
float distance = _value - _cloud_value;
67+
float const distance = Location::distance(_value, _cloud_value);
6568
return _value != _cloud_value && (abs(distance) >= ArduinoCloudProperty::_min_delta_property);
6669
}
6770

0 commit comments

Comments
 (0)