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)
| 299 | /// horizontal (to avoid division by zero) or if the scanline does not intersect the |
| 300 | /// vertical span of the edge. |
| 301 | fn 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. |
| 322 | fn fill_polygon<R>(rasops: &mut R, points: &[PixelsXY]) -> io::Result<()> |