plotters_unsable/element/
points.rs

1use super::*;
2use super::{Drawable, PointCollection};
3use crate::drawing::backend::{BackendCoord, DrawingBackend, DrawingErrorKind};
4use crate::style::ShapeStyle;
5
6/// The element that used to describe a point
7pub trait PointElement<'a, Coord> {
8    fn make_point(pos: Coord, size: u32, style: ShapeStyle<'a>) -> Self;
9}
10
11/// Describe a cross
12pub struct Cross<'a, Coord> {
13    center: Coord,
14    size: u32,
15    style: ShapeStyle<'a>,
16}
17
18impl<'a, Coord> Cross<'a, Coord> {
19    pub fn new(coord: Coord, size: u32, style: ShapeStyle<'a>) -> Self {
20        Self {
21            center: coord,
22            size,
23            style,
24        }
25    }
26}
27
28impl<'b, 'a, Coord: 'a> PointCollection<'a, Coord> for &'a Cross<'b, Coord> {
29    type Borrow = &'a Coord;
30    type IntoIter = std::iter::Once<&'a Coord>;
31    fn point_iter(self) -> std::iter::Once<&'a Coord> {
32        std::iter::once(&self.center)
33    }
34}
35
36impl<'a, Coord: 'a, DB: DrawingBackend> Drawable<DB> for Cross<'a, Coord> {
37    fn draw<I: Iterator<Item = BackendCoord>>(
38        &self,
39        mut points: I,
40        backend: &mut DB,
41    ) -> Result<(), DrawingErrorKind<DB::ErrorType>> {
42        if let Some((x, y)) = points.next() {
43            let size = self.size as i32;
44            let (x0, y0) = (x - size, y - size);
45            let (x1, y1) = (x + size, y + size);
46            backend.draw_line((x0, y0), (x1, y1), &Box::new(self.style.color))?;
47            backend.draw_line((x0, y1), (x1, y0), &Box::new(self.style.color))?;
48        }
49        Ok(())
50    }
51}
52impl<'a, Coord> PointElement<'a, Coord> for Cross<'a, Coord> {
53    fn make_point(pos: Coord, size: u32, style: ShapeStyle<'a>) -> Self {
54        Self::new(pos, size, style)
55    }
56}
57
58impl<'a, Coord> PointElement<'a, Coord> for Circle<'a, Coord> {
59    fn make_point(pos: Coord, size: u32, style: ShapeStyle<'a>) -> Self {
60        Self::new(pos, size, style)
61    }
62}
63
64impl<'a, Coord> PointElement<'a, Coord> for Pixel<'a, Coord> {
65    fn make_point(pos: Coord, _: u32, style: ShapeStyle<'a>) -> Self {
66        Self::new(pos, style)
67    }
68}