MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / polygon_to_polygon_distance

Function polygon_to_polygon_distance

nodedb-spatial/src/predicates/distance.rs:212–247  ·  view source on GitHub ↗
(ra: &[Vec<[f64; 2]>], rb: &[Vec<[f64; 2]>])

Source from the content-addressed store, hash-verified

210}
211
212fn polygon_to_polygon_distance(ra: &[Vec<[f64; 2]>], rb: &[Vec<[f64; 2]>]) -> f64 {
213 let Some(ext_a) = ra.first() else {
214 return f64::INFINITY;
215 };
216 let Some(ext_b) = rb.first() else {
217 return f64::INFINITY;
218 };
219
220 // If any vertex of B is inside A (or vice versa), distance is 0.
221 for pt in ext_b {
222 if point_in_polygon(pt[0], pt[1], ext_a) || point_on_ring_boundary(*pt, ext_a) {
223 return 0.0;
224 }
225 }
226 for pt in ext_a {
227 if point_in_polygon(pt[0], pt[1], ext_b) || point_on_ring_boundary(*pt, ext_b) {
228 return 0.0;
229 }
230 }
231
232 // Min edge-to-edge distance.
233 let mut min_dist = f64::INFINITY;
234 let a_edges = ring_edges(ext_a);
235 let b_edges = ring_edges(ext_b);
236 for &(a1, a2) in &a_edges {
237 for &(b1, b2) in &b_edges {
238 let d_sq = segment_to_segment_dist_sq(a1, a2, b1, b2);
239 if d_sq < 1e-20 {
240 return 0.0;
241 }
242 let d = coord_dist_to_meters(d_sq.sqrt(), a1, b1);
243 min_dist = min_dist.min(d);
244 }
245 }
246 min_dist
247}
248
249/// Point to segment distance in meters.
250///

Callers 1

st_distanceFunction · 0.85

Calls 6

point_in_polygonFunction · 0.85
point_on_ring_boundaryFunction · 0.85
ring_edgesFunction · 0.85
coord_dist_to_metersFunction · 0.85
firstMethod · 0.80

Tested by

no test coverage detected