plotters/element/
points.rs

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use super::*;
use super::{Drawable, PointCollection};
use crate::style::{Color, ShapeStyle, SizeDesc};
use plotters_backend::{BackendCoord, DrawingBackend, DrawingErrorKind};

/**
A common trait for elements that can be interpreted as points: A cross, a circle, a triangle marker...

This is used internally by Plotters and should probably not be included in user code.
See [`EmptyElement`] for more information and examples.
*/
pub trait PointElement<Coord, Size: SizeDesc> {
    /**
    Point creator.

    This is used internally by Plotters and should probably not be included in user code.
    See [`EmptyElement`] for more information and examples.
    */
    fn make_point(pos: Coord, size: Size, style: ShapeStyle) -> Self;
}

/**
A cross marker for visualizing data series.

See [`EmptyElement`] for more information and examples.
*/
pub struct Cross<Coord, Size: SizeDesc> {
    center: Coord,
    size: Size,
    style: ShapeStyle,
}

impl<Coord, Size: SizeDesc> Cross<Coord, Size> {
    /**
    Creates a cross marker.

    See [`EmptyElement`] for more information and examples.
    */
    pub fn new<T: Into<ShapeStyle>>(coord: Coord, size: Size, style: T) -> Self {
        Self {
            center: coord,
            size,
            style: style.into(),
        }
    }
}

impl<'a, Coord: 'a, Size: SizeDesc> PointCollection<'a, Coord> for &'a Cross<Coord, Size> {
    type Point = &'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, Size: SizeDesc> Drawable<DB> for Cross<Coord, Size> {
    fn draw<I: Iterator<Item = BackendCoord>>(
        &self,
        mut points: I,
        backend: &mut DB,
        ps: (u32, u32),
    ) -> Result<(), DrawingErrorKind<DB::ErrorType>> {
        if let Some((x, y)) = points.next() {
            let size = self.size.in_pixels(&ps);
            let (x0, y0) = (x - size, y - size);
            let (x1, y1) = (x + size, y + size);
            backend.draw_line((x0, y0), (x1, y1), &self.style)?;
            backend.draw_line((x0, y1), (x1, y0), &self.style)?;
        }
        Ok(())
    }
}

/**
A triangle marker for visualizing data series.

See [`EmptyElement`] for more information and examples.
*/
pub struct TriangleMarker<Coord, Size: SizeDesc> {
    center: Coord,
    size: Size,
    style: ShapeStyle,
}

impl<Coord, Size: SizeDesc> TriangleMarker<Coord, Size> {
    /**
    Creates a triangle marker.

    See [`EmptyElement`] for more information and examples.
    */
    pub fn new<T: Into<ShapeStyle>>(coord: Coord, size: Size, style: T) -> Self {
        Self {
            center: coord,
            size,
            style: style.into(),
        }
    }
}

impl<'a, Coord: 'a, Size: SizeDesc> PointCollection<'a, Coord> for &'a TriangleMarker<Coord, Size> {
    type Point = &'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, Size: SizeDesc> Drawable<DB> for TriangleMarker<Coord, Size> {
    fn draw<I: Iterator<Item = BackendCoord>>(
        &self,
        mut points: I,
        backend: &mut DB,
        ps: (u32, u32),
    ) -> Result<(), DrawingErrorKind<DB::ErrorType>> {
        if let Some((x, y)) = points.next() {
            let size = self.size.in_pixels(&ps);
            let points = [-90, -210, -330]
                .iter()
                .map(|deg| f64::from(*deg) * std::f64::consts::PI / 180.0)
                .map(|rad| {
                    (
                        (rad.cos() * f64::from(size) + f64::from(x)).ceil() as i32,
                        (rad.sin() * f64::from(size) + f64::from(y)).ceil() as i32,
                    )
                });
            backend.fill_polygon(points, &self.style.color.to_backend_color())?;
        }
        Ok(())
    }
}

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

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

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

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