Extract all edges from a polygon ring as segment pairs.
(ring: &[[f64; 2]])
| 146 | |
| 147 | /// Extract all edges from a polygon ring as segment pairs. |
| 148 | pub fn ring_edges(ring: &[[f64; 2]]) -> Vec<([f64; 2], [f64; 2])> { |
| 149 | if ring.len() < 2 { |
| 150 | return Vec::new(); |
| 151 | } |
| 152 | let mut edges = Vec::with_capacity(ring.len()); |
| 153 | for i in 0..ring.len() - 1 { |
| 154 | edges.push((ring[i], ring[i + 1])); |
| 155 | } |
| 156 | // Close the ring if not explicitly closed. |
| 157 | if ring.first() != ring.last() |
| 158 | && let (Some(&first), Some(&last)) = (ring.first(), ring.last()) |
| 159 | { |
| 160 | edges.push((last, first)); |
| 161 | } |
| 162 | edges |
| 163 | } |
| 164 | |
| 165 | #[cfg(test)] |
| 166 | mod tests { |