MCPcopy Index your code
hub / github.com/endbasic/endbasic / fill_polygon

Function fill_polygon

std/src/console/drawing.rs:322–356  ·  view source on GitHub ↗

Fills the poligon defined by `points` using a scanline intersection algorithm.

(rasops: &mut R, points: &[PixelsXY])

Source from the content-addressed store, hash-verified

320
321/// Fills the poligon defined by `points` using a scanline intersection algorithm.
322fn fill_polygon<R>(rasops: &mut R, points: &[PixelsXY]) -> io::Result<()>
323where
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`.
359pub fn draw_poly_filled<R>(rasops: &mut R, points: &[PixelsXY]) -> io::Result<()>

Callers 2

draw_poly_filledFunction · 0.85
draw_tri_filledFunction · 0.85

Calls 7

edge_intersectionFunction · 0.85
iterMethod · 0.80
pushMethod · 0.80
clamped_intoMethod · 0.80
is_emptyMethod · 0.45
lenMethod · 0.45
draw_lineMethod · 0.45

Tested by

no test coverage detected