Commit 42c575a9487abdbb36e459c5b029abd22ce41696

Authored by Georg Hopp
1 parent d603a914

Complete fill with iterators.

Showing 1 changed file with 23 additions and 63 deletions
@@ -74,7 +74,6 @@ where T: Add<Output = T> + Debug + Copy + From<i32> { @@ -74,7 +74,6 @@ where T: Add<Output = T> + Debug + Copy + From<i32> {
74 type Item = Coordinate<T>; 74 type Item = Coordinate<T>;
75 75
76 fn next(&mut self) -> Option<Self::Item> { 76 fn next(&mut self) -> Option<Self::Item> {
77 - println!("== LineIterator next: {:?}", self);  
78 match self.a { 77 match self.a {
79 None => None, 78 None => None,
80 Some(a) => { 79 Some(a) => {
@@ -273,14 +272,18 @@ where T: Add<Output = T> + Sub<Output = T> + Div<Output = T> @@ -273,14 +272,18 @@ where T: Add<Output = T> + Sub<Output = T> + Div<Output = T>
273 + Debug + Copy + From<i32> { 272 + Debug + Copy + From<i32> {
274 273
275 fn new(p :&'a Polygon<T>, direction :Direction) -> Self { 274 fn new(p :&'a Polygon<T>, direction :Direction) -> Self {
276 - let mut v = VertexIterator { p: p  
277 - , top: p.vert_min()  
278 - , current: Some(p.vert_min())  
279 - , edge: None  
280 - , direction: direction };  
281 - v.next_edge();  
282 - println!("== new: {:?}", v);  
283 - v 275 + let top = p.vert_min();
  276 + let next = p.next_y(top, direction);
  277 + let edge = match next {
  278 + None => None,
  279 + Some(next) => Some(p.vertex(top).edge_iter(&p.vertex(next))),
  280 + };
  281 +
  282 + VertexIterator { p: p
  283 + , top: top
  284 + , current: next
  285 + , edge: edge
  286 + , direction: direction }
284 } 287 }
285 288
286 // if this yields "None" we are finished. 289 // if this yields "None" we are finished.
@@ -289,8 +292,6 @@ where T: Add<Output = T> + Sub<Output = T> + Div<Output = T> @@ -289,8 +292,6 @@ where T: Add<Output = T> + Sub<Output = T> + Div<Output = T>
289 let next = self.p.next_y(current, self.direction)?; 292 let next = self.p.next_y(current, self.direction)?;
290 let mut edge = self.p.vertex(current).edge_iter(&self.p.vertex(next)); 293 let mut edge = self.p.vertex(current).edge_iter(&self.p.vertex(next));
291 294
292 - println!("== next: {:?} {:?} {:?}", current, next, edge);  
293 -  
294 match edge.next() { 295 match edge.next() {
295 // It should be impossible that a new edge iterator has no values 296 // It should be impossible that a new edge iterator has no values
296 // at all… anyway, just in case I handle it here. 297 // at all… anyway, just in case I handle it here.
@@ -311,9 +312,7 @@ where T: Add<Output = T> + Sub<Output = T> + Div<Output = T> @@ -311,9 +312,7 @@ where T: Add<Output = T> + Sub<Output = T> + Div<Output = T>
311 312
312 fn next(&mut self) -> Option<Self::Item> { 313 fn next(&mut self) -> Option<Self::Item> {
313 // if for whatever reason edge is "None" finish this iterator. 314 // if for whatever reason edge is "None" finish this iterator.
314 - let next = self.edge?.next();  
315 -  
316 - println!("== next: {:?}", next); 315 + let next = self.edge.as_mut()?.next();
317 316
318 match next { 317 match next {
319 Some(_) => next, 318 Some(_) => next,
@@ -350,6 +349,10 @@ where T: Add<Output = T> + Sub<Output = T> + Div<Output = T> @@ -350,6 +349,10 @@ where T: Add<Output = T> + Sub<Output = T> + Div<Output = T>
350 VertexIterator::new(self, Direction::Left) 349 VertexIterator::new(self, Direction::Left)
351 } 350 }
352 351
  352 + fn right_vertices(&self) -> VertexIterator<T> {
  353 + VertexIterator::new(self, Direction::Right)
  354 + }
  355 +
353 fn left(&self, v :usize) -> usize { 356 fn left(&self, v :usize) -> usize {
354 let Polygon(Coordinates(cs)) = self; 357 let Polygon(Coordinates(cs)) = self;
355 358
@@ -438,57 +441,14 @@ impl<T> Fillable<T> for Polygon<T> @@ -438,57 +441,14 @@ impl<T> Fillable<T> for Polygon<T>
438 where T: Add<Output = T> + Sub<Output = T> + Div<Output = T> 441 where T: Add<Output = T> + Sub<Output = T> + Div<Output = T>
439 + Debug + Clone + Copy + From<i32> { 442 + Debug + Clone + Copy + From<i32> {
440 fn fill(&self) -> Coordinates<T> { 443 fn fill(&self) -> Coordinates<T> {
441 - let Polygon(Coordinates(cs)) = self;  
442 -  
443 - let vert_min = self.vert_min();  
444 -  
445 - println!("== vert_min: [{}] {:?}", vert_min, cs[vert_min]);  
446 -  
447 - let mut r = (vert_min, self.next_y(vert_min, Direction::Right));  
448 - let mut l = (vert_min, self.next_y(vert_min, Direction::Left));  
449 -  
450 - let mut l_edge :Vec<Coordinate<T>> = Vec::new();  
451 - let mut r_edge :Vec<Coordinate<T>> = Vec::new();  
452 -  
453 - let append_edge = | v :&mut Vec<Coordinate<T>>  
454 - , e :(usize, Option<usize>)  
455 - , f :Direction | {  
456 - match e {  
457 - (_, None) => e,  
458 - (a, Some(b)) => {  
459 - let mut edge = cs[a].edge(&cs[b]);  
460 - v.append(&mut edge);  
461 - (b, self.next_y(b, f))  
462 - },  
463 - }  
464 - };  
465 -  
466 - let print_current = |s :&str, e :(usize, Option<usize>)| {  
467 - match e.1 {  
468 - None => println!( "== {}: [{:?}] - {:?}"  
469 - , s, e.1, "None"),  
470 - Some(e) => println!( "== {}: [{:?}] - {:?}"  
471 - , s, e, cs[e]),  
472 - }  
473 - };  
474 -  
475 - while l.1 != None || r.1 != None {  
476 - print_current("l", l);  
477 - l = append_edge(&mut l_edge, l, Direction::Left);  
478 - print_current("r", r);  
479 - r = append_edge(&mut r_edge, r, Direction::Right);  
480 - }  
481 -  
482 - let l_edge2 :Vec<Coordinate<T>> = self.left_vertices().collect(); 444 + let scanlines = self.left_vertices().zip(self.right_vertices());
483 445
484 - println!("== [{}] {:?}", l_edge.len(), l_edge);  
485 - println!("== [{}] {:?}", l_edge2.len(), l_edge2);  
486 - println!("== [{}] {:?}", r_edge.len(), r_edge); 446 + // vertices is an iterator over all vertices making this polygon.
  447 + let vertices = scanlines.flat_map(|(l, r)| l.line_iter(&r));
487 448
488 - // TODO we always miss the last scanline…  
489 - // TODO check what happend with at least 2 vertices with same y and  
490 - // different x…  
491 - // loop though edges… 449 + // for debug only…
  450 + //let vertices :Vec<(Coordinate<T>)> = vertices.collect();
  451 + //println!("== [{}] {:?}", vertices.len(), vertices);
492 452
493 Coordinates(Vec::<Coordinate<T>>::new()) 453 Coordinates(Vec::<Coordinate<T>>::new())
494 } 454 }
Please register or login to post a comment