|
|
|
@ -56,7 +56,7 @@ pub struct Coordinate<T>(pub i32, pub i32, pub T); |
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Coordinates<T>(pub Vec<Coordinate<T>>);
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub struct LineIterator<T> where T: Debug {
|
|
|
|
a :Option<Coordinate<T>>
|
|
|
|
, b :Coordinate<T>
|
|
|
|
@ -74,6 +74,7 @@ where T: Add<Output = T> + Debug + Copy + From<i32> { |
|
|
|
type Item = Coordinate<T>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
println!("== LineIterator next: {:?}", self);
|
|
|
|
match self.a {
|
|
|
|
None => None,
|
|
|
|
Some(a) => {
|
|
|
|
@ -263,37 +264,70 @@ pub struct VertexIterator<'a,T> where T: Debug { |
|
|
|
p :&'a Polygon<T>,
|
|
|
|
top :usize,
|
|
|
|
current :Option<usize>,
|
|
|
|
inner :Option<LineIterator<T>>,
|
|
|
|
edge :Option<LineIterator<T>>,
|
|
|
|
direction :Direction,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a,T> Iterator for VertexIterator<'a,T>
|
|
|
|
impl<'a,T> VertexIterator<'a,T>
|
|
|
|
where T: Add<Output = T> + Sub<Output = T> + Div<Output = T>
|
|
|
|
+ Debug + Copy + From<i32> {
|
|
|
|
type Item = Coordinate<T>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
let inner = match self.inner {
|
|
|
|
Some(i) => i,
|
|
|
|
None => {
|
|
|
|
fn new(p :&'a Polygon<T>, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// if this yields "None" we are finished.
|
|
|
|
fn next_edge(&mut self) -> Option<LineIterator<T>> {
|
|
|
|
let current = self.current?;
|
|
|
|
let next = self.p.next_y(current, self.direction)?;
|
|
|
|
self.p.vertex(current).edge_iter(&self.p.vertex(next))
|
|
|
|
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.
|
|
|
|
None => self.next_edge(),
|
|
|
|
Some(_) => {
|
|
|
|
self.current = Some(next);
|
|
|
|
self.edge = Some(edge);
|
|
|
|
self.edge
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.current {
|
|
|
|
None => None,
|
|
|
|
Some(c) => {
|
|
|
|
let r = self.p.vertex(c);
|
|
|
|
self.current = self.p.next_y(c, self.direction);
|
|
|
|
Some(r)
|
|
|
|
impl<'a,T> Iterator for VertexIterator<'a,T>
|
|
|
|
where T: Add<Output = T> + Sub<Output = T> + Div<Output = T>
|
|
|
|
+ Debug + Copy + From<i32> {
|
|
|
|
type Item = Coordinate<T>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
// if for whatever reason edge is "None" finish this iterator.
|
|
|
|
let next = self.edge?.next();
|
|
|
|
|
|
|
|
println!("== next: {:?}", next);
|
|
|
|
|
|
|
|
match next {
|
|
|
|
Some(_) => next,
|
|
|
|
None => {
|
|
|
|
self.next_edge()?;
|
|
|
|
self.next()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Polygon<T> where T: Copy + Debug {
|
|
|
|
impl<T> Polygon<T>
|
|
|
|
where T: Add<Output = T> + Sub<Output = T> + Div<Output = T>
|
|
|
|
+ Copy + Debug + From<i32> {
|
|
|
|
fn vert_min<'a>(&'a self) -> usize {
|
|
|
|
let Polygon(Coordinates(cs)) = self;
|
|
|
|
|
|
|
|
@ -313,11 +347,7 @@ impl<T> Polygon<T> where T: Copy + Debug { |
|
|
|
}
|
|
|
|
|
|
|
|
fn left_vertices(&self) -> VertexIterator<T> {
|
|
|
|
VertexIterator { p: &self
|
|
|
|
, top: self.vert_min()
|
|
|
|
, current: Some(self.vert_min())
|
|
|
|
, inner: None |
|
|
|
, direction: Direction::Left }
|
|
|
|
VertexIterator::new(self, Direction::Left)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn left(&self, v :usize) -> usize {
|
|
|
|
@ -353,7 +383,8 @@ impl<T> Polygon<T> where T: Copy + Debug { |
|
|
|
, c :usize |
|
|
|
, n :usize |
|
|
|
, d :Direction) -> Option<usize>
|
|
|
|
where T: Copy + Debug {
|
|
|
|
where T: Add<Output = T> + Sub<Output = T> + Div<Output = T>
|
|
|
|
+ Copy + Debug + From<i32> {
|
|
|
|
if c == n {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
@ -362,6 +393,8 @@ impl<T> Polygon<T> where T: Copy + Debug { |
|
|
|
|
|
|
|
match ny.cmp(&cy) {
|
|
|
|
cmp::Ordering::Less => None,
|
|
|
|
// TODO On equal we need to find out which one of both to
|
|
|
|
// keep in the list… (the outermost)
|
|
|
|
cmp::Ordering::Equal => inner(p, c, p.step(n, d), d),
|
|
|
|
cmp::Ordering::Greater => Some(n),
|
|
|
|
}
|
|
|
|
@ -446,7 +479,10 @@ where T: Add<Output = T> + Sub<Output = T> + Div<Output = T> |
|
|
|
r = append_edge(&mut r_edge, r, Direction::Right);
|
|
|
|
}
|
|
|
|
|
|
|
|
let l_edge2 :Vec<Coordinate<T>> = self.left_vertices().collect();
|
|
|
|
|
|
|
|
println!("== [{}] {:?}", l_edge.len(), l_edge);
|
|
|
|
println!("== [{}] {:?}", l_edge2.len(), l_edge2);
|
|
|
|
println!("== [{}] {:?}", r_edge.len(), r_edge);
|
|
|
|
|
|
|
|
// TODO we always miss the last scanline…
|
|
|
|
|