Skip to content

Commit d2654a9

Browse files
committed
Readability improved
1 parent d1f75d3 commit d2654a9

12 files changed

+672
-673
lines changed

Diff for: README.md

+5-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ C++ Geometry Library provides utility functions for the computation of geometric
2424

2525
* [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.
2626
* [Poly](https://developers.google.com/maps/documentation/javascript/reference#poly) utility functions for computations involving polygons and polylines.
27-
* [Encoding](https://developers.google.com/maps/documentation/javascript/reference#encoding) utilities for polyline encoding and decoding.
2827

2928
## Usage
3029

@@ -66,9 +65,9 @@ int main() {
6665

6766
### PolyUtil class
6867

69-
* [`containsLocation(LatLng point, LatLngList polygon, bool geodesic = false)`](#containsLocation)
70-
* [`isLocationOnEdge(LatLng point, LatLngList polygon, double tolerance = PolyUtil::DEFAULT_TOLERANCE, bool geodesic = true)`](#isLocationOnEdge)
71-
* [`isLocationOnPath(LatLng point, LatLngList polyline, double tolerance = PolyUtil::DEFAULT_TOLERANCE, bool geodesic = true)`](#isLocationOnPath)
68+
* [`containsLocation(LatLng point, LatLngList polygon, bool geodesic)`](#containsLocation)
69+
* [`isLocationOnEdge(LatLng point, LatLngList polygon, double tolerance, bool geodesic)`](#isLocationOnEdge)
70+
* [`isLocationOnPath(LatLng point, LatLngList polyline, double tolerance, bool geodesic)`](#isLocationOnPath)
7271
* [`distanceToLine(LatLng point, LatLng start, LatLng end)`](#distanceToLine)
7372

7473
### SphericalUtil class
@@ -360,6 +359,5 @@ assert(SphericalUtil::computeSignedArea(path) == -SphericalUtil::computeSignedAr
360359

361360
## License
362361

363-
Geometry Library Google Maps API V3 is released under the MIT License. See the bundled
364-
[LICENSE](https://github.com/alexpechkarev/geometry-library/blob/master/LICENSE)
365-
file for details.
362+
Geometry Library Google Maps API V3 is released under the MIT License.
363+
See the bundled [LICENSE](https://github.com/alexpechkarev/geometry-library/blob/master/LICENSE) file for details.

Diff for: include/LatLng.hpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
class LatLng {
2020
public:
21-
double lat; // The latitude of this location
22-
double lng; // The longitude of this location
21+
double lat; // The latitude of this location
22+
double lng; // The longitude of this location
2323

2424
/**
2525
* Constructs a location with a latitude/longitude pair.
@@ -30,10 +30,14 @@ class LatLng {
3030
LatLng(double lat, double lng)
3131
: lat(lat), lng(lng) {}
3232

33-
bool operator==(const LatLng& other) {
34-
return isCoordinateEqual(lat, other.lat) &&
33+
LatLng(const LatLng & point) = default;
34+
35+
LatLng& operator=(const LatLng & other) = default;
36+
37+
bool operator==(const LatLng & other) {
38+
return isCoordinateEqual(lat, other.lat) &&
3539
isCoordinateEqual(lng, other.lng);
36-
}
40+
}
3741

3842

3943
private:

Diff for: include/MathUtil.hpp

+91-91
Original file line numberDiff line numberDiff line change
@@ -21,104 +21,104 @@
2121
#define M_PI 3.14159265358979323846
2222

2323
inline double deg2rad(double degrees) {
24-
return degrees * M_PI / 180.0;
24+
return degrees * M_PI / 180.0;
2525
}
2626

2727
inline double rad2deg(double angle) {
28-
return angle * 180.0 / M_PI;
28+
return angle * 180.0 / M_PI;
2929
}
3030

3131
class MathUtil {
3232
public:
33-
/**
34-
* The earth's radius, in meters.
35-
* Mean radius as defined by IUGG.
36-
*/
37-
static constexpr double EARTH_RADIUS = 6371009.0;
38-
39-
/**
40-
* Restrict x to the range [low, high].
41-
*/
42-
static inline double clamp(double x, double low, double high) {
43-
return x < low ? low : (x > high ? high : x);
44-
}
45-
46-
/**
47-
* Wraps the given value into the inclusive-exclusive interval between min and max.
48-
* @param n The value to wrap.
49-
* @param min The minimum.
50-
* @param max The maximum.
51-
*/
52-
static inline double wrap(double n, double min, double max) {
53-
return (n >= min && n < max) ? n : (MathUtil::mod(n - min, max - min) + min);
54-
}
55-
56-
/**
57-
* Returns the non-negative remainder of x / m.
58-
* @param x The operand.
59-
* @param m The modulus.
60-
*/
61-
static inline double mod(double x, double m) {
62-
return remainder(remainder(x, m) + m, m);
63-
}
64-
65-
/**
66-
* Returns mercator Y corresponding to latitude.
67-
* See http://en.wikipedia.org/wiki/Mercator_projection .
68-
*/
69-
static inline double mercator(double lat) {
70-
return log(tan(lat * 0.5 + M_PI / 4.0));
71-
}
72-
73-
/**
74-
* Returns latitude from mercator Y.
75-
*/
76-
static inline double inverseMercator(double y) {
77-
return 2.0 * atan(exp(y)) - M_PI / 2.0;
78-
}
79-
80-
/**
81-
* Returns haversine(angle-in-radians).
82-
* hav(x) == (1 - cos(x)) / 2 == sin(x / 2)^2.
83-
*/
84-
static inline double hav(double x) {
85-
double sinHalf = sin(x * 0.5);
86-
return sinHalf * sinHalf;
87-
}
88-
89-
/**
90-
* Computes inverse haversine. Has good numerical stability around 0.
91-
* arcHav(x) == acos(1 - 2 * x) == 2 * asin(sqrt(x)).
92-
* The argument must be in [0, 1], and the result is positive.
93-
*/
94-
static inline double arcHav(double x) {
95-
return 2.0 * asin(sqrt(x));
96-
}
97-
98-
// Given h==hav(x), returns sin(abs(x)).
99-
static inline double sinFromHav(double h) {
100-
return 2.0 * sqrt(h * (1.0 - h));
101-
}
102-
103-
// Returns hav(asin(x)).
104-
static inline double havFromSin(double x) {
105-
double x2 = x * x;
106-
return x2 / (1.0 + sqrt(1.0 - x2)) * 0.5;
107-
}
108-
109-
// Returns sin(arcHav(x) + arcHav(y)).
110-
static inline double sinSumFromHav(double x, double y) {
111-
double a = sqrt(x * (1 - x));
112-
double b = sqrt(y * (1 - y));
113-
return 2.0 * (a + b - 2 * (a * y + b * x));
114-
}
115-
116-
/**
117-
* Returns hav() of distance from (lat1, lng1) to (lat2, lng2) on the unit sphere.
118-
*/
119-
static inline double havDistance(double lat1, double lat2, double dLng) {
120-
return MathUtil::hav(lat1 - lat2) + MathUtil::hav(dLng) * cos(lat1) * cos(lat2);
121-
}
33+
/**
34+
* The earth's radius, in meters.
35+
* Mean radius as defined by IUGG.
36+
*/
37+
static constexpr double EARTH_RADIUS = 6371009.0;
38+
39+
/**
40+
* Restrict x to the range [low, high].
41+
*/
42+
static inline double clamp(double x, double low, double high) {
43+
return x < low ? low : (x > high ? high : x);
44+
}
45+
46+
/**
47+
* Wraps the given value into the inclusive-exclusive interval between min and max.
48+
* @param n The value to wrap.
49+
* @param min The minimum.
50+
* @param max The maximum.
51+
*/
52+
static inline double wrap(double n, double min, double max) {
53+
return (n >= min && n < max) ? n : (MathUtil::mod(n - min, max - min) + min);
54+
}
55+
56+
/**
57+
* Returns the non-negative remainder of x / m.
58+
* @param x The operand.
59+
* @param m The modulus.
60+
*/
61+
static inline double mod(double x, double m) {
62+
return remainder(remainder(x, m) + m, m);
63+
}
64+
65+
/**
66+
* Returns mercator Y corresponding to latitude.
67+
* See http://en.wikipedia.org/wiki/Mercator_projection .
68+
*/
69+
static inline double mercator(double lat) {
70+
return log(tan(lat * 0.5 + M_PI / 4.0));
71+
}
72+
73+
/**
74+
* Returns latitude from mercator Y.
75+
*/
76+
static inline double inverseMercator(double y) {
77+
return 2.0 * atan(exp(y)) - M_PI / 2.0;
78+
}
79+
80+
/**
81+
* Returns haversine(angle-in-radians).
82+
* hav(x) == (1 - cos(x)) / 2 == sin(x / 2)^2.
83+
*/
84+
static inline double hav(double x) {
85+
double sinHalf = sin(x * 0.5);
86+
return sinHalf * sinHalf;
87+
}
88+
89+
/**
90+
* Computes inverse haversine. Has good numerical stability around 0.
91+
* arcHav(x) == acos(1 - 2 * x) == 2 * asin(sqrt(x)).
92+
* The argument must be in [0, 1], and the result is positive.
93+
*/
94+
static inline double arcHav(double x) {
95+
return 2.0 * asin(sqrt(x));
96+
}
97+
98+
// Given h==hav(x), returns sin(abs(x)).
99+
static inline double sinFromHav(double h) {
100+
return 2.0 * sqrt(h * (1.0 - h));
101+
}
102+
103+
// Returns hav(asin(x)).
104+
static inline double havFromSin(double x) {
105+
double x2 = x * x;
106+
return x2 / (1.0 + sqrt(1.0 - x2)) * 0.5;
107+
}
108+
109+
// Returns sin(arcHav(x) + arcHav(y)).
110+
static inline double sinSumFromHav(double x, double y) {
111+
double a = sqrt(x * (1 - x));
112+
double b = sqrt(y * (1 - y));
113+
return 2.0 * (a + b - 2 * (a * y + b * x));
114+
}
115+
116+
/**
117+
* Returns hav() of distance from (lat1, lng1) to (lat2, lng2) on the unit sphere.
118+
*/
119+
static inline double havDistance(double lat1, double lat2, double dLng) {
120+
return MathUtil::hav(lat1 - lat2) + MathUtil::hav(dLng) * cos(lat1) * cos(lat2);
121+
}
122122
};
123123

124124
#endif // GEOMETRY_LIBRARY_MATH_UTIL

0 commit comments

Comments
 (0)