@@ -89,6 +89,40 @@ class PolyUtil {
8989 return PolyUtil::isLocationOnEdgeOrPath (point, polyline, false , geodesic, tolerance);
9090 }
9191
92+ /* *
93+ * Computes the distance on the sphere between the point p and the line segment start to end.
94+ *
95+ * @param p the point to be measured
96+ * @param start the beginning of the line segment
97+ * @param end the end of the line segment
98+ * @return the distance in meters (assuming spherical earth)
99+ */
100+ static inline double distanceToLine (const LatLng& p, const LatLng& start, const LatLng& end) {
101+ if (start == end) {
102+ return SphericalUtil::computeDistanceBetween (end, p);
103+ }
104+ double s0lat = MathUtil::deg2rad (p.lat );
105+ double s0lng = MathUtil::deg2rad (p.lng );
106+ double s1lat = MathUtil::deg2rad (start.lat );
107+ double s1lng = MathUtil::deg2rad (start.lng );
108+ double s2lat = MathUtil::deg2rad (end.lat );
109+ double s2lng = MathUtil::deg2rad (end.lng );
110+ double s2s1lat = s2lat - s1lat;
111+ double s2s1lng = s2lng - s1lng;
112+ double u = ((s0lat - s1lat) * s2s1lat + (s0lng - s1lng) * s2s1lng)
113+ / (s2s1lat * s2s1lat + s2s1lng * s2s1lng);
114+ if (u <= 0 ) {
115+ return SphericalUtil::computeDistanceBetween (p, start);
116+ }
117+ if (u >= 1 ) {
118+ return SphericalUtil::computeDistanceBetween (p, end);
119+ }
120+ LatLng su (start.lat + u * (end.lat - start.lat ), start.lng + u * (end.lng - start.lng ));
121+ return SphericalUtil::computeDistanceBetween (p, su);
122+ }
123+
124+
125+ private:
92126 /* *
93127 * Computes whether a given point lies on or near a polyline, within a specified tolerance.
94128 *
@@ -170,40 +204,6 @@ class PolyUtil {
170204 return false ;
171205 }
172206
173- /* *
174- * Computes the distance on the sphere between the point p and the line segment start to end.
175- *
176- * @param p the point to be measured
177- * @param start the beginning of the line segment
178- * @param end the end of the line segment
179- * @return the distance in meters (assuming spherical earth)
180- */
181- static inline double distanceToLine (const LatLng& p, const LatLng& start, const LatLng& end) {
182- if (start == end) {
183- return SphericalUtil::computeDistanceBetween (end, p);
184- }
185- double s0lat = MathUtil::deg2rad (p.lat );
186- double s0lng = MathUtil::deg2rad (p.lng );
187- double s1lat = MathUtil::deg2rad (start.lat );
188- double s1lng = MathUtil::deg2rad (start.lng );
189- double s2lat = MathUtil::deg2rad (end.lat );
190- double s2lng = MathUtil::deg2rad (end.lng );
191- double s2s1lat = s2lat - s1lat;
192- double s2s1lng = s2lng - s1lng;
193- double u = ((s0lat - s1lat) * s2s1lat + (s0lng - s1lng) * s2s1lng)
194- / (s2s1lat * s2s1lat + s2s1lng * s2s1lng);
195- if (u <= 0 ) {
196- return SphericalUtil::computeDistanceBetween (p, start);
197- }
198- if (u >= 1 ) {
199- return SphericalUtil::computeDistanceBetween (p, end);
200- }
201- LatLng su (start.lat + u * (end.lat - start.lat ), start.lng + u * (end.lng - start.lng ));
202- return SphericalUtil::computeDistanceBetween (p, su);
203- }
204-
205-
206- private:
207207 /* *
208208 * Returns tan(latitude-at-lng3) on the great circle (lat1, lng1) to (lat2, lng2). lng1==0.
209209 * See http://williams.best.vwh.net/avform.htm .
0 commit comments