|
|
|
@ -39,6 +39,7 @@ pub trait Canvas<T> { |
|
|
|
fn clear(&mut self);
|
|
|
|
fn draw(&mut self, c :&dyn Drawable<T>, ofs :Coordinate<T>, color :u32);
|
|
|
|
fn put_text(&self, ofs :Coordinate<T>, s :&str);
|
|
|
|
fn set_pixel(&mut self, c :Coordinate<T>, color :u32);
|
|
|
|
fn show(&self);
|
|
|
|
}
|
|
|
|
|
|
|
|
@ -46,8 +47,10 @@ pub trait Drawable<T> { |
|
|
|
fn plot(&self) -> Coordinates<T>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Fillable<T> {
|
|
|
|
fn fill(&self) -> Coordinates<T>;
|
|
|
|
pub trait Fillable<T>
|
|
|
|
where T: Add<Output = T> + Sub<Output = T> + Div<Output = T>
|
|
|
|
+ Debug + Copy + From<i32> {
|
|
|
|
fn fill(&self, canvas :&mut dyn Canvas<T>, color :u32);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
@ -440,17 +443,12 @@ where T: Add<Output = T> + Sub<Output = T> + Div<Output = T> |
|
|
|
impl<T> Fillable<T> for Polygon<T>
|
|
|
|
where T: Add<Output = T> + Sub<Output = T> + Div<Output = T>
|
|
|
|
+ Debug + Clone + Copy + From<i32> {
|
|
|
|
fn fill(&self) -> Coordinates<T> {
|
|
|
|
fn fill(&self, canvas :&mut dyn Canvas<T>, color :u32) {
|
|
|
|
let scanlines = self.left_vertices().zip(self.right_vertices());
|
|
|
|
|
|
|
|
// vertices is an iterator over all vertices making this polygon.
|
|
|
|
let vertices = scanlines.flat_map(|(l, r)| l.line_iter(&r));
|
|
|
|
|
|
|
|
// for debug only…
|
|
|
|
//let vertices :Vec<(Coordinate<T>)> = vertices.collect();
|
|
|
|
//println!("== [{}] {:?}", vertices.len(), vertices);
|
|
|
|
|
|
|
|
Coordinates(Vec::<Coordinate<T>>::new())
|
|
|
|
for l in scanlines.flat_map(|(l, r)| l.line_iter(&r)) {
|
|
|
|
canvas.set_pixel(l, color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|