refactor class route add a struct to hold coords

This commit is contained in:
2022-12-27 13:11:03 +01:00
parent a09cfe9b04
commit b9840bccf8
2 changed files with 60 additions and 20 deletions
+39 -12
View File
@@ -12,36 +12,59 @@
#include "route.h" #include "route.h"
Point::Point(double lat, double lon, uint32_t horizontalAccuracy) { Point::Point(double lat, double lon, uint32_t horizontalAccuracy) {
this->lat = lat; this->coordinates.lat = lat;
this->lon = lon; this->coordinates.lon = lon;
this->init(horizontalAccuracy);
}
Point::Point(int32_t lat, int32_t lon, uint32_t horizontalAccuracy) {
this->coordinates.lat = lat / 10000000.0;
this->coordinates.lon = lon / 10000000.0;
this->init(horizontalAccuracy);
}
Point::Point(Coordinates coords, uint32_t horizontalAccuracy) {
this->coordinates = coords;
this->init(horizontalAccuracy); this->init(horizontalAccuracy);
} }
Point::Point() { Point::Point() {
this->lat = 0; this->coordinates.lat = 0;
this->lon = 0; this->coordinates.lon = 0;
this->init(0); this->init(0);
} }
double Point::distanceTo(const Point &point) const { bool Point::operator==(const Point& rhs) const {
// distance = sqrt(dx * dx + dy * dy) return this->coordinates == rhs.getCoordinates();
}
// distance = sqrt(dx * dx + dy * dy)
// mit distance: Entfernung in km // mit distance: Entfernung in km
// dx = 111.3 * cos(lat) * (lon1 - lon2) // dx = 111.3 * cos(lat) * (lon1 - lon2)
// lat = (lat1 + lat2) / 2 * 0.01745 // lat = (lat1 + lat2) / 2 * 0.01745
// dy = 111.3 * (lat1 - lat2) // dy = 111.3 * (lat1 - lat2)
// lat1, lat2, lon1, lon2: Breite, Länge in Grad // lat1, lat2, lon1, lon2: Breite, Länge in Grad
double Point::distanceTo(const Coordinates& point) const {
Coordinates begin = this->coordinates;
Coordinates end = point;
double lat = (this->lat + point.lat) / 2 * ROUTE_DEGREE_TO_RADIANT; double lat = (begin.lat + end.lat) / 2 * ROUTE_DEGREE_TO_RADIANT;
double dy = ROUTE_DISTANCE_BETWEEN_LATITUDE * (this->lat - point.lat); double dy = ROUTE_DISTANCE_BETWEEN_LATITUDE * (begin.lat - end.lat);
double dx = ROUTE_DISTANCE_BETWEEN_LATITUDE * cos(lat) * (this->lon - point.lon); double dx = ROUTE_DISTANCE_BETWEEN_LATITUDE * cos(lat) * (begin.lon - end.lon);
return sqrt(dx * dx + dy * dy); return sqrt(dx * dx + dy * dy);
} }
double Point::courseTo(const Point &point) const { double Point::distanceTo(const Point &point) const {
double phi = log( tan(point.lat * ROUTE_DEGREE_TO_RADIANT / 2 + ROUTE_PI / 4) / tan(this->lat * ROUTE_DEGREE_TO_RADIANT / 2 + ROUTE_PI / 4) ); return this->distanceTo(point.getCoordinates());
double lon = (this->lon * ROUTE_DEGREE_TO_RADIANT - point.lon * ROUTE_DEGREE_TO_RADIANT); }
double Point::courseTo(const Coordinates& point) const {
Coordinates begin = this->coordinates;
Coordinates end = point;
double phi = log( tan(end.lat * ROUTE_DEGREE_TO_RADIANT / 2 + ROUTE_PI / 4) / tan(begin.lat * ROUTE_DEGREE_TO_RADIANT / 2 + ROUTE_PI / 4) );
double lon = (begin.lon * ROUTE_DEGREE_TO_RADIANT - end.lon * ROUTE_DEGREE_TO_RADIANT);
double res = atan2(lon, phi) / ROUTE_DEGREE_TO_RADIANT; double res = atan2(lon, phi) / ROUTE_DEGREE_TO_RADIANT;
@@ -51,6 +74,10 @@ double Point::courseTo(const Point &point) const {
return res; return res;
} }
double Point::courseTo(const Point &point) const {
return this->courseTo(point.getCoordinates());
}
void Point::init(uint32_t horizontalAccuracy) { void Point::init(uint32_t horizontalAccuracy) {
this->creationTime = millis(); this->creationTime = millis();
if (horizontalAccuracy > 9999) if (horizontalAccuracy > 9999)
+21 -8
View File
@@ -30,6 +30,15 @@
*/ */
class Point{ class Point{
public: public:
struct Coordinates {
double lon;
double lat;
bool operator==(const Coordinates rhs) const {
return ( this->lon == rhs.lon ) && ( this->lon == rhs.lon );
}
};
enum PointAccuracy { enum PointAccuracy {
none, none,
fourDigOfCM, fourDigOfCM,
@@ -42,15 +51,14 @@ class Point{
/** /**
* @brief Construct a new Point object * @brief Construct a new Point object
* *
* @param lat latidude * @param lat latitude
* @param lon longitude * @param lon longitude
*/ */
Point(double lat, double lon, uint32_t horizontalAccuracy = 0); Point(double lat, double lon, uint32_t horizontalAccuracy = 0);
Point(int32_t lat, int32_t lon, uint32_t horizontalAccuracy = 0);
Point(Coordinates coords, uint32_t horizontalAccuracy = 0);
Point(); Point();
double lat = 0;
double lon = 0;
/** /**
* @brief checks if to points are equal * @brief checks if to points are equal
* *
@@ -58,9 +66,7 @@ class Point{
* @return true * @return true
* @return false * @return false
*/ */
bool operator==(const Point& rhs) const { bool operator==(const Point& rhs) const;
return this->lat == rhs.lat && this->lon == rhs.lon;
}
/** /**
* @brief Checks if the Point is initalized. * @brief Checks if the Point is initalized.
@@ -68,18 +74,25 @@ class Point{
* @return true * @return true
* @return false * @return false
*/ */
bool isInit() const { return this->lat + this->lon; } bool isInit() const { return this->coordinates.lat + this->coordinates.lon; }
bool isValid() const { return (this->accuracy > 0) ? true : false; } bool isValid() const { return (this->accuracy > 0) ? true : false; }
double distanceTo(const Coordinates& point) const;
double distanceTo(const Point& point) const; double distanceTo(const Point& point) const;
double courseTo(const Coordinates& point) const;
double courseTo(const Point& point) const; double courseTo(const Point& point) const;
double getLongitude() const { return this->coordinates.lon; }
double getLatitude() const { return this->coordinates.lat; }
Coordinates getCoordinates() const { return this->coordinates; }
PointAccuracy getAccuracy() { return this->accuracy; } PointAccuracy getAccuracy() { return this->accuracy; }
private: private:
void init(uint32_t horizontalAccuracy); void init(uint32_t horizontalAccuracy);
PointAccuracy accuracy = PointAccuracy::none; PointAccuracy accuracy = PointAccuracy::none;
Coordinates coordinates;
uint32_t creationTime = 0; uint32_t creationTime = 0;
}; };