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

Function edge_intersection

std/src/console/drawing.rs:301–319  ·  view source on GitHub ↗

Calculates the x-coordinate intersection of a horizontal scanline with a line segment. This function uses the linear interpolation formula to find the point where the scanline at height `y` crosses the edge defined by points `p1` and `p2`. Returns the x-coordinate of the intersection if the scanline passes through the edge (specifically within the half-open interval `[ymin, ymax)`, or `None` if

(y: i32, p1: PixelsXY, p2: PixelsXY)

Source from the content-addressed store, hash-verified

299/// horizontal (to avoid division by zero) or if the scanline does not intersect the
300/// vertical span of the edge.
301fn edge_intersection(y: i32, p1: PixelsXY, p2: PixelsXY) -> Option<i32> {
302 let y1 = i32::from(p1.y);
303 let y2 = i32::from(p2.y);
304 if y1 == y2 {
305 return None;
306 }
307
308 let ymin = y1.min(y2);
309 let ymax = y1.max(y2);
310 // The check `y >= ymax` is to return `None` to prevent double-counting vertices
311 // in polygon filling algorithms (the "top-left" rule).
312 if y < ymin || y >= ymax {
313 return None;
314 }
315
316 let x1 = i32::from(p1.x);
317 let x2 = i32::from(p2.x);
318 Some(x1 + (y - y1) * (x2 - x1) / (y2 - y1))
319}
320
321/// Fills the poligon defined by `points` using a scanline intersection algorithm.
322fn fill_polygon<R>(rasops: &mut R, points: &[PixelsXY]) -> io::Result<()>

Callers 1

fill_polygonFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected