1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use super::*;
use super::{Drawable, PointCollection};
use crate::drawing::backend::{BackendCoord, DrawingBackend, DrawingErrorKind};
use crate::style::ShapeStyle;

/// The element that used to describe a point
pub trait PointElement<Coord> {
    fn make_point(pos: Coord, size: u32, style: ShapeStyle) -> Self;
}

/// Describe a cross
pub struct Cross<Coord> {
    center: Coord,
    size: u32,
    style: ShapeStyle,
}

impl<Coord> Cross<Coord> {
    pub fn new<T: Into<ShapeStyle>>(coord: Coord, size: u32, style: T) -> Self {
        Self {
            center: coord,
            size,
            style: style.into(),
        }
    }
}

impl<'a, Coord: 'a> PointCollection<'a, Coord> for &'a Cross<Coord> {
    type Borrow = &'a Coord;
    type IntoIter = std::iter::Once<&'a Coord>;
    fn point_iter(self) -> std::iter::Once<&'a Coord> {
        std::iter::once(&self.center)
    }
}

impl<Coord, DB: DrawingBackend> Drawable<DB> for Cross<Coord> {
    fn draw<I: Iterator<Item = BackendCoord>>(
        &self,
        mut points: I,
        backend: &mut DB,
    ) -> Result<(), DrawingErrorKind<DB::ErrorType>> {
        if let Some((x, y)) = points.next() {
            let size = self.size as i32;
            let (x0, y0) = (x - size, y - size);
            let (x1, y1) = (x + size, y + size);
            backend.draw_line((x0, y0), (x1, y1), &self.style.color)?;
            backend.draw_line((x0, y1), (x1, y0), &self.style.color)?;
        }
        Ok(())
    }
}
impl<Coord> PointElement<Coord> for Cross<Coord> {
    fn make_point(pos: Coord, size: u32, style: ShapeStyle) -> Self {
        Self::new(pos, size, style)
    }
}

impl<Coord> PointElement<Coord> for Circle<Coord> {
    fn make_point(pos: Coord, size: u32, style: ShapeStyle) -> Self {
        Self::new(pos, size, style)
    }
}

impl<Coord> PointElement<Coord> for Pixel<Coord> {
    fn make_point(pos: Coord, _: u32, style: ShapeStyle) -> Self {
        Self::new(pos, style)
    }
}