Skip to content

Commit

Permalink
Readability improved
Browse files Browse the repository at this point in the history
  • Loading branch information
gistrec committed Feb 12, 2020
1 parent d1f75d3 commit d2654a9
Show file tree
Hide file tree
Showing 12 changed files with 672 additions and 673 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ C++ Geometry Library provides utility functions for the computation of geometric

* [Spherical](https://developers.google.com/maps/documentation/javascript/reference#spherical) contains spherical geometry utilities allowing you to compute angles, distances and areas from latitudes and longitudes.
* [Poly](https://developers.google.com/maps/documentation/javascript/reference#poly) utility functions for computations involving polygons and polylines.
* [Encoding](https://developers.google.com/maps/documentation/javascript/reference#encoding) utilities for polyline encoding and decoding.

## Usage

Expand Down Expand Up @@ -66,9 +65,9 @@ int main() {

### PolyUtil class

* [`containsLocation(LatLng point, LatLngList polygon, bool geodesic = false)`](#containsLocation)
* [`isLocationOnEdge(LatLng point, LatLngList polygon, double tolerance = PolyUtil::DEFAULT_TOLERANCE, bool geodesic = true)`](#isLocationOnEdge)
* [`isLocationOnPath(LatLng point, LatLngList polyline, double tolerance = PolyUtil::DEFAULT_TOLERANCE, bool geodesic = true)`](#isLocationOnPath)
* [`containsLocation(LatLng point, LatLngList polygon, bool geodesic)`](#containsLocation)
* [`isLocationOnEdge(LatLng point, LatLngList polygon, double tolerance, bool geodesic)`](#isLocationOnEdge)
* [`isLocationOnPath(LatLng point, LatLngList polyline, double tolerance, bool geodesic)`](#isLocationOnPath)
* [`distanceToLine(LatLng point, LatLng start, LatLng end)`](#distanceToLine)

### SphericalUtil class
Expand Down Expand Up @@ -360,6 +359,5 @@ assert(SphericalUtil::computeSignedArea(path) == -SphericalUtil::computeSignedAr

## License

Geometry Library Google Maps API V3 is released under the MIT License. See the bundled
[LICENSE](https://github.com/alexpechkarev/geometry-library/blob/master/LICENSE)
file for details.
Geometry Library Google Maps API V3 is released under the MIT License.
See the bundled [LICENSE](https://github.com/alexpechkarev/geometry-library/blob/master/LICENSE) file for details.
14 changes: 9 additions & 5 deletions include/LatLng.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

class LatLng {
public:
double lat; // The latitude of this location
double lng; // The longitude of this location
double lat; // The latitude of this location
double lng; // The longitude of this location

/**
* Constructs a location with a latitude/longitude pair.
Expand All @@ -30,10 +30,14 @@ class LatLng {
LatLng(double lat, double lng)
: lat(lat), lng(lng) {}

bool operator==(const LatLng& other) {
return isCoordinateEqual(lat, other.lat) &&
LatLng(const LatLng & point) = default;

LatLng& operator=(const LatLng & other) = default;

bool operator==(const LatLng & other) {
return isCoordinateEqual(lat, other.lat) &&
isCoordinateEqual(lng, other.lng);
}
}


private:
Expand Down
182 changes: 91 additions & 91 deletions include/MathUtil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,104 +21,104 @@
#define M_PI 3.14159265358979323846

inline double deg2rad(double degrees) {
return degrees * M_PI / 180.0;
return degrees * M_PI / 180.0;
}

inline double rad2deg(double angle) {
return angle * 180.0 / M_PI;
return angle * 180.0 / M_PI;
}

class MathUtil {
public:
/**
* The earth's radius, in meters.
* Mean radius as defined by IUGG.
*/
static constexpr double EARTH_RADIUS = 6371009.0;

/**
* Restrict x to the range [low, high].
*/
static inline double clamp(double x, double low, double high) {
return x < low ? low : (x > high ? high : x);
}

/**
* Wraps the given value into the inclusive-exclusive interval between min and max.
* @param n The value to wrap.
* @param min The minimum.
* @param max The maximum.
*/
static inline double wrap(double n, double min, double max) {
return (n >= min && n < max) ? n : (MathUtil::mod(n - min, max - min) + min);
}

/**
* Returns the non-negative remainder of x / m.
* @param x The operand.
* @param m The modulus.
*/
static inline double mod(double x, double m) {
return remainder(remainder(x, m) + m, m);
}

/**
* Returns mercator Y corresponding to latitude.
* See http://en.wikipedia.org/wiki/Mercator_projection .
*/
static inline double mercator(double lat) {
return log(tan(lat * 0.5 + M_PI / 4.0));
}

/**
* Returns latitude from mercator Y.
*/
static inline double inverseMercator(double y) {
return 2.0 * atan(exp(y)) - M_PI / 2.0;
}

/**
* Returns haversine(angle-in-radians).
* hav(x) == (1 - cos(x)) / 2 == sin(x / 2)^2.
*/
static inline double hav(double x) {
double sinHalf = sin(x * 0.5);
return sinHalf * sinHalf;
}

/**
* Computes inverse haversine. Has good numerical stability around 0.
* arcHav(x) == acos(1 - 2 * x) == 2 * asin(sqrt(x)).
* The argument must be in [0, 1], and the result is positive.
*/
static inline double arcHav(double x) {
return 2.0 * asin(sqrt(x));
}

// Given h==hav(x), returns sin(abs(x)).
static inline double sinFromHav(double h) {
return 2.0 * sqrt(h * (1.0 - h));
}

// Returns hav(asin(x)).
static inline double havFromSin(double x) {
double x2 = x * x;
return x2 / (1.0 + sqrt(1.0 - x2)) * 0.5;
}

// Returns sin(arcHav(x) + arcHav(y)).
static inline double sinSumFromHav(double x, double y) {
double a = sqrt(x * (1 - x));
double b = sqrt(y * (1 - y));
return 2.0 * (a + b - 2 * (a * y + b * x));
}

/**
* Returns hav() of distance from (lat1, lng1) to (lat2, lng2) on the unit sphere.
*/
static inline double havDistance(double lat1, double lat2, double dLng) {
return MathUtil::hav(lat1 - lat2) + MathUtil::hav(dLng) * cos(lat1) * cos(lat2);
}
/**
* The earth's radius, in meters.
* Mean radius as defined by IUGG.
*/
static constexpr double EARTH_RADIUS = 6371009.0;

/**
* Restrict x to the range [low, high].
*/
static inline double clamp(double x, double low, double high) {
return x < low ? low : (x > high ? high : x);
}

/**
* Wraps the given value into the inclusive-exclusive interval between min and max.
* @param n The value to wrap.
* @param min The minimum.
* @param max The maximum.
*/
static inline double wrap(double n, double min, double max) {
return (n >= min && n < max) ? n : (MathUtil::mod(n - min, max - min) + min);
}

/**
* Returns the non-negative remainder of x / m.
* @param x The operand.
* @param m The modulus.
*/
static inline double mod(double x, double m) {
return remainder(remainder(x, m) + m, m);
}

/**
* Returns mercator Y corresponding to latitude.
* See http://en.wikipedia.org/wiki/Mercator_projection .
*/
static inline double mercator(double lat) {
return log(tan(lat * 0.5 + M_PI / 4.0));
}

/**
* Returns latitude from mercator Y.
*/
static inline double inverseMercator(double y) {
return 2.0 * atan(exp(y)) - M_PI / 2.0;
}

/**
* Returns haversine(angle-in-radians).
* hav(x) == (1 - cos(x)) / 2 == sin(x / 2)^2.
*/
static inline double hav(double x) {
double sinHalf = sin(x * 0.5);
return sinHalf * sinHalf;
}

/**
* Computes inverse haversine. Has good numerical stability around 0.
* arcHav(x) == acos(1 - 2 * x) == 2 * asin(sqrt(x)).
* The argument must be in [0, 1], and the result is positive.
*/
static inline double arcHav(double x) {
return 2.0 * asin(sqrt(x));
}

// Given h==hav(x), returns sin(abs(x)).
static inline double sinFromHav(double h) {
return 2.0 * sqrt(h * (1.0 - h));
}

// Returns hav(asin(x)).
static inline double havFromSin(double x) {
double x2 = x * x;
return x2 / (1.0 + sqrt(1.0 - x2)) * 0.5;
}

// Returns sin(arcHav(x) + arcHav(y)).
static inline double sinSumFromHav(double x, double y) {
double a = sqrt(x * (1 - x));
double b = sqrt(y * (1 - y));
return 2.0 * (a + b - 2 * (a * y + b * x));
}

/**
* Returns hav() of distance from (lat1, lng1) to (lat2, lng2) on the unit sphere.
*/
static inline double havDistance(double lat1, double lat2, double dLng) {
return MathUtil::hav(lat1 - lat2) + MathUtil::hav(dLng) * cos(lat1) * cos(lat2);
}
};

#endif // GEOMETRY_LIBRARY_MATH_UTIL
Loading

0 comments on commit d2654a9

Please sign in to comment.