Showing
1 changed file
with
59 additions
and
23 deletions
| ... | ... | @@ -56,7 +56,7 @@ pub struct Coordinate<T>(pub i32, pub i32, pub T); |
| 56 | 56 | #[derive(Debug, Clone)] |
| 57 | 57 | pub struct Coordinates<T>(pub Vec<Coordinate<T>>); |
| 58 | 58 | |
| 59 | -#[derive(Debug, Clone)] | |
| 59 | +#[derive(Debug, Clone, Copy)] | |
| 60 | 60 | pub struct LineIterator<T> where T: Debug { |
| 61 | 61 | a :Option<Coordinate<T>> |
| 62 | 62 | , b :Coordinate<T> |
| ... | ... | @@ -74,6 +74,7 @@ where T: Add<Output = T> + Debug + Copy + From<i32> { |
| 74 | 74 | type Item = Coordinate<T>; |
| 75 | 75 | |
| 76 | 76 | fn next(&mut self) -> Option<Self::Item> { |
| 77 | + println!("== LineIterator next: {:?}", self); | |
| 77 | 78 | match self.a { |
| 78 | 79 | None => None, |
| 79 | 80 | Some(a) => { |
| ... | ... | @@ -263,37 +264,70 @@ pub struct VertexIterator<'a,T> where T: Debug { |
| 263 | 264 | p :&'a Polygon<T>, |
| 264 | 265 | top :usize, |
| 265 | 266 | current :Option<usize>, |
| 266 | - inner :Option<LineIterator<T>>, | |
| 267 | + edge :Option<LineIterator<T>>, | |
| 267 | 268 | direction :Direction, |
| 268 | 269 | } |
| 269 | 270 | |
| 271 | +impl<'a,T> VertexIterator<'a,T> | |
| 272 | +where T: Add<Output = T> + Sub<Output = T> + Div<Output = T> | |
| 273 | + + Debug + Copy + From<i32> { | |
| 274 | + | |
| 275 | + 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 | |
| 284 | + } | |
| 285 | + | |
| 286 | + // if this yields "None" we are finished. | |
| 287 | + fn next_edge(&mut self) -> Option<LineIterator<T>> { | |
| 288 | + let current = self.current?; | |
| 289 | + let next = self.p.next_y(current, self.direction)?; | |
| 290 | + let mut edge = self.p.vertex(current).edge_iter(&self.p.vertex(next)); | |
| 291 | + | |
| 292 | + println!("== next: {:?} {:?} {:?}", current, next, edge); | |
| 293 | + | |
| 294 | + match edge.next() { | |
| 295 | + // It should be impossible that a new edge iterator has no values | |
| 296 | + // at all… anyway, just in case I handle it here. | |
| 297 | + None => self.next_edge(), | |
| 298 | + Some(_) => { | |
| 299 | + self.current = Some(next); | |
| 300 | + self.edge = Some(edge); | |
| 301 | + self.edge | |
| 302 | + }, | |
| 303 | + } | |
| 304 | + } | |
| 305 | +} | |
| 306 | + | |
| 270 | 307 | impl<'a,T> Iterator for VertexIterator<'a,T> |
| 271 | 308 | where T: Add<Output = T> + Sub<Output = T> + Div<Output = T> |
| 272 | 309 | + Debug + Copy + From<i32> { |
| 273 | 310 | type Item = Coordinate<T>; |
| 274 | 311 | |
| 275 | 312 | fn next(&mut self) -> Option<Self::Item> { |
| 276 | - let inner = match self.inner { | |
| 277 | - Some(i) => i, | |
| 278 | - None => { | |
| 279 | - let current = self.current?; | |
| 280 | - let next = self.p.next_y(current, self.direction)?; | |
| 281 | - self.p.vertex(current).edge_iter(&self.p.vertex(next)) | |
| 282 | - }, | |
| 283 | - } | |
| 313 | + // if for whatever reason edge is "None" finish this iterator. | |
| 314 | + let next = self.edge?.next(); | |
| 284 | 315 | |
| 285 | - match self.current { | |
| 286 | - None => None, | |
| 287 | - Some(c) => { | |
| 288 | - let r = self.p.vertex(c); | |
| 289 | - self.current = self.p.next_y(c, self.direction); | |
| 290 | - Some(r) | |
| 316 | + println!("== next: {:?}", next); | |
| 317 | + | |
| 318 | + match next { | |
| 319 | + Some(_) => next, | |
| 320 | + None => { | |
| 321 | + self.next_edge()?; | |
| 322 | + self.next() | |
| 291 | 323 | }, |
| 292 | 324 | } |
| 293 | 325 | } |
| 294 | 326 | } |
| 295 | 327 | |
| 296 | -impl<T> Polygon<T> where T: Copy + Debug { | |
| 328 | +impl<T> Polygon<T> | |
| 329 | +where T: Add<Output = T> + Sub<Output = T> + Div<Output = T> | |
| 330 | + + Copy + Debug + From<i32> { | |
| 297 | 331 | fn vert_min<'a>(&'a self) -> usize { |
| 298 | 332 | let Polygon(Coordinates(cs)) = self; |
| 299 | 333 | |
| ... | ... | @@ -313,11 +347,7 @@ impl<T> Polygon<T> where T: Copy + Debug { |
| 313 | 347 | } |
| 314 | 348 | |
| 315 | 349 | fn left_vertices(&self) -> VertexIterator<T> { |
| 316 | - VertexIterator { p: &self | |
| 317 | - , top: self.vert_min() | |
| 318 | - , current: Some(self.vert_min()) | |
| 319 | - , inner: None | |
| 320 | - , direction: Direction::Left } | |
| 350 | + VertexIterator::new(self, Direction::Left) | |
| 321 | 351 | } |
| 322 | 352 | |
| 323 | 353 | fn left(&self, v :usize) -> usize { |
| ... | ... | @@ -353,7 +383,8 @@ impl<T> Polygon<T> where T: Copy + Debug { |
| 353 | 383 | , c :usize |
| 354 | 384 | , n :usize |
| 355 | 385 | , d :Direction) -> Option<usize> |
| 356 | - where T: Copy + Debug { | |
| 386 | + where T: Add<Output = T> + Sub<Output = T> + Div<Output = T> | |
| 387 | + + Copy + Debug + From<i32> { | |
| 357 | 388 | if c == n { |
| 358 | 389 | None |
| 359 | 390 | } else { |
| ... | ... | @@ -362,6 +393,8 @@ impl<T> Polygon<T> where T: Copy + Debug { |
| 362 | 393 | |
| 363 | 394 | match ny.cmp(&cy) { |
| 364 | 395 | cmp::Ordering::Less => None, |
| 396 | + // TODO On equal we need to find out which one of both to | |
| 397 | + // keep in the list… (the outermost) | |
| 365 | 398 | cmp::Ordering::Equal => inner(p, c, p.step(n, d), d), |
| 366 | 399 | cmp::Ordering::Greater => Some(n), |
| 367 | 400 | } |
| ... | ... | @@ -446,7 +479,10 @@ where T: Add<Output = T> + Sub<Output = T> + Div<Output = T> |
| 446 | 479 | r = append_edge(&mut r_edge, r, Direction::Right); |
| 447 | 480 | } |
| 448 | 481 | |
| 482 | + let l_edge2 :Vec<Coordinate<T>> = self.left_vertices().collect(); | |
| 483 | + | |
| 449 | 484 | println!("== [{}] {:?}", l_edge.len(), l_edge); |
| 485 | + println!("== [{}] {:?}", l_edge2.len(), l_edge2); | |
| 450 | 486 | println!("== [{}] {:?}", r_edge.len(), r_edge); |
| 451 | 487 | |
| 452 | 488 | // TODO we always miss the last scanline… | ... | ... |
Please
register
or
login
to post a comment