Intersect a horizontal line with the edges of a polygon, returning X coordinates of all intersection points.
(line: &Line, polygon: &Polygon)
| 199 | /// Intersect a horizontal line with the edges of a polygon, returning X coordinates |
| 200 | /// of all intersection points. |
| 201 | fn 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. |
| 231 | fn compute_path_length(waypoints: &[Waypoint]) -> f64 { |