From 42c575a9487abdbb36e459c5b029abd22ce41696 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Wed, 8 Jan 2020 14:03:41 +0100 Subject: [PATCH] Complete fill with iterators. --- fractional/src/easel.rs | 86 +++++++++++------------------------------ 1 file changed, 23 insertions(+), 63 deletions(-) diff --git a/fractional/src/easel.rs b/fractional/src/easel.rs index 504cf64..75d5b9e 100644 --- a/fractional/src/easel.rs +++ b/fractional/src/easel.rs @@ -74,7 +74,6 @@ where T: Add + Debug + Copy + From { type Item = Coordinate; fn next(&mut self) -> Option { - println!("== LineIterator next: {:?}", self); match self.a { None => None, Some(a) => { @@ -273,14 +272,18 @@ where T: Add + Sub + Div + Debug + Copy + From { fn new(p :&'a Polygon, direction :Direction) -> Self { - let mut v = VertexIterator { p: p - , top: p.vert_min() - , current: Some(p.vert_min()) - , edge: None - , direction: direction }; - v.next_edge(); - println!("== new: {:?}", v); - v + let top = p.vert_min(); + let next = p.next_y(top, direction); + let edge = match next { + None => None, + Some(next) => Some(p.vertex(top).edge_iter(&p.vertex(next))), + }; + + VertexIterator { p: p + , top: top + , current: next + , edge: edge + , direction: direction } } // if this yields "None" we are finished. @@ -289,8 +292,6 @@ where T: Add + Sub + Div let next = self.p.next_y(current, self.direction)?; let mut edge = self.p.vertex(current).edge_iter(&self.p.vertex(next)); - println!("== next: {:?} {:?} {:?}", current, next, edge); - match edge.next() { // It should be impossible that a new edge iterator has no values // at all… anyway, just in case I handle it here. @@ -311,9 +312,7 @@ where T: Add + Sub + Div fn next(&mut self) -> Option { // if for whatever reason edge is "None" finish this iterator. - let next = self.edge?.next(); - - println!("== next: {:?}", next); + let next = self.edge.as_mut()?.next(); match next { Some(_) => next, @@ -350,6 +349,10 @@ where T: Add + Sub + Div VertexIterator::new(self, Direction::Left) } + fn right_vertices(&self) -> VertexIterator { + VertexIterator::new(self, Direction::Right) + } + fn left(&self, v :usize) -> usize { let Polygon(Coordinates(cs)) = self; @@ -438,57 +441,14 @@ impl Fillable for Polygon where T: Add + Sub + Div + Debug + Clone + Copy + From { fn fill(&self) -> Coordinates { - let Polygon(Coordinates(cs)) = self; - - let vert_min = self.vert_min(); - - println!("== vert_min: [{}] {:?}", vert_min, cs[vert_min]); - - let mut r = (vert_min, self.next_y(vert_min, Direction::Right)); - let mut l = (vert_min, self.next_y(vert_min, Direction::Left)); - - let mut l_edge :Vec> = Vec::new(); - let mut r_edge :Vec> = Vec::new(); - - let append_edge = | v :&mut Vec> - , e :(usize, Option) - , f :Direction | { - match e { - (_, None) => e, - (a, Some(b)) => { - let mut edge = cs[a].edge(&cs[b]); - v.append(&mut edge); - (b, self.next_y(b, f)) - }, - } - }; - - let print_current = |s :&str, e :(usize, Option)| { - match e.1 { - None => println!( "== {}: [{:?}] - {:?}" - , s, e.1, "None"), - Some(e) => println!( "== {}: [{:?}] - {:?}" - , s, e, cs[e]), - } - }; - - while l.1 != None || r.1 != None { - print_current("l", l); - l = append_edge(&mut l_edge, l, Direction::Left); - print_current("r", r); - r = append_edge(&mut r_edge, r, Direction::Right); - } - - let l_edge2 :Vec> = self.left_vertices().collect(); + let scanlines = self.left_vertices().zip(self.right_vertices()); - println!("== [{}] {:?}", l_edge.len(), l_edge); - println!("== [{}] {:?}", l_edge2.len(), l_edge2); - println!("== [{}] {:?}", r_edge.len(), r_edge); + // vertices is an iterator over all vertices making this polygon. + let vertices = scanlines.flat_map(|(l, r)| l.line_iter(&r)); - // TODO we always miss the last scanline… - // TODO check what happend with at least 2 vertices with same y and - // different x… - // loop though edges… + // for debug only… + //let vertices :Vec<(Coordinate)> = vertices.collect(); + //println!("== [{}] {:?}", vertices.len(), vertices); Coordinates(Vec::>::new()) }