MCPcopy Create free account
hub / github.com/ecto/muni / intersect_line_polygon

Function intersect_line_polygon

depot/dispatch/src/coverage.rs:201–228  ·  view source on GitHub ↗

Intersect a horizontal line with the edges of a polygon, returning X coordinates of all intersection points.

(line: &Line, polygon: &Polygon)

Source from the content-addressed store, hash-verified

199/// Intersect a horizontal line with the edges of a polygon, returning X coordinates
200/// of all intersection points.
201fn intersect_line_polygon(line: &Line, polygon: &Polygon) -> Vec<f64> {
202 let mut crossings = Vec::new();
203 let exterior = polygon.exterior();
204 let points: Vec<Coord> = exterior.0.clone();
205
206 for i in 0..points.len().saturating_sub(1) {
207 let edge = Line::new(points[i], points[i + 1]);
208 if !line.intersects(&edge) {
209 continue;
210 }
211 if let Some(intersection) = geo::algorithm::line_intersection::line_intersection(
212 edge.into(),
213 (*line).into(),
214 ) {
215 match intersection {
216 LineIntersection::SinglePoint { intersection, .. } => {
217 crossings.push(intersection.x);
218 }
219 LineIntersection::Collinear { intersection } => {
220 crossings.push(intersection.start.x);
221 crossings.push(intersection.end.x);
222 }
223 }
224 }
225 }
226
227 crossings
228}
229
230/// Compute total path length from waypoints.
231fn compute_path_length(waypoints: &[Waypoint]) -> f64 {

Callers 1

generateFunction · 0.85

Calls 2

lenMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected