Fills the poligon defined by `points` using a scanline intersection algorithm.
(rasops: &mut R, points: &[PixelsXY])
| 320 | |
| 321 | /// Fills the poligon defined by `points` using a scanline intersection algorithm. |
| 322 | fn fill_polygon<R>(rasops: &mut R, points: &[PixelsXY]) -> io::Result<()> |
| 323 | where |
| 324 | R: RasterOps + ?Sized, |
| 325 | { |
| 326 | if points.is_empty() { |
| 327 | return Ok(()); |
| 328 | } |
| 329 | |
| 330 | let min_y = points.iter().map(|p| i32::from(p.y)).min().expect("Points are not empty"); |
| 331 | let max_y = points.iter().map(|p| i32::from(p.y)).max().expect("Points are not empty"); |
| 332 | |
| 333 | for y in min_y..=max_y { |
| 334 | let mut xs = vec![]; |
| 335 | for i in 0..points.len() { |
| 336 | let next = (i + 1) % points.len(); |
| 337 | if let Some(x) = edge_intersection(y, points[i], points[next]) { |
| 338 | xs.push(x); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | if xs.len() < 2 { |
| 343 | continue; |
| 344 | } |
| 345 | |
| 346 | xs.sort_unstable(); |
| 347 | for pair in xs.chunks_exact(2) { |
| 348 | rasops.draw_line( |
| 349 | PixelsXY::new(pair[0].clamped_into(), y.clamped_into()), |
| 350 | PixelsXY::new(pair[1].clamped_into(), y.clamped_into()), |
| 351 | )?; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | Ok(()) |
| 356 | } |
| 357 | |
| 358 | /// Draws a filled polygon via `rasops`. |
| 359 | pub fn draw_poly_filled<R>(rasops: &mut R, points: &[PixelsXY]) -> io::Result<()> |
no test coverage detected