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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//! # plotters-arrows
//!
//! Simple arrow shapes for [plotters](::plotters) crate.

use plotters::element::{Drawable, PointCollection};
use plotters::prelude::*;
use plotters::style::SizeDesc;
use plotters_backend::{BackendCoord, DrawingErrorKind};

#[derive(Clone)]
pub struct ThinArrow<Coord, Size: SizeDesc> {
    points: [Coord; 2],
    head: Size,
    width: Size,
    style: ShapeStyle,
}

impl<Coord> ThinArrow<Coord, i32> {
    pub fn new(nock: Coord, tip: Coord, style: impl Into<ShapeStyle>) -> Self {
        Self {
            points: [nock, tip],
            head: 5,
            width: 5,
            style: style.into(),
        }
    }
}

impl<Coord, Size: SizeDesc> ThinArrow<Coord, Size> {
    pub fn new_detail(
        nock: Coord,
        tip: Coord,
        head: Size,
        width: Size,
        style: impl Into<ShapeStyle>,
    ) -> Self {
        Self {
            points: [nock, tip],
            head,
            width,
            style: style.into(),
        }
    }

    pub fn head(self, head: Size) -> Self {
        Self { head, ..self }
    }

    pub fn width(self, width: Size) -> Self {
        Self { width, ..self }
    }
}

impl<'a, Coord: 'a, Size: SizeDesc> PointCollection<'a, Coord> for &'a ThinArrow<Coord, Size> {
    type Point = &'a Coord;

    type IntoIter = &'a [Coord];

    fn point_iter(self) -> Self::IntoIter {
        &self.points
    }
}

impl<DB: DrawingBackend, Coord, Size: SizeDesc> Drawable<DB> for ThinArrow<Coord, Size> {
    fn draw<I: Iterator<Item = BackendCoord>>(
        &self,
        mut pos: I,
        backend: &mut DB,
        parent_dim: (u32, u32),
    ) -> Result<(), DrawingErrorKind<DB::ErrorType>> {
        let nock = pos.next();
        let tip = pos.next();
        debug_assert!(nock.is_some());
        debug_assert!(tip.is_some());
        if let (Some(nock), Some(tip)) = (nock, tip) {
            let head = self.head.in_pixels(&parent_dim) as f64;
            let width = self.width.in_pixels(&parent_dim) as f64;
            if nock != tip {
                let dx = (tip.0 - nock.0) as f64;
                let dy = (tip.1 - nock.1) as f64;
                let d = f64::sqrt(dx * dx + dy * dy);
                let head = head.min(d);
                let width = width.min(d);
                let half_width = width * 0.5;
                let left = (
                    tip.0 + ((-head * dx + half_width * dy) / d) as i32,
                    tip.1 + ((-head * dy - half_width * dx) / d) as i32,
                );
                let right = (
                    tip.0 + ((-head * dx - half_width * dy) / d) as i32,
                    tip.1 + ((-head * dy + half_width * dx) / d) as i32,
                );
                backend.draw_line(nock, tip, &self.style)?;
                backend.draw_line(tip, left, &self.style)?;
                backend.draw_line(tip, right, &self.style)?;
            }
        }
        Ok(())
    }
}

#[derive(Clone)]
pub struct TriangleArrow<Coord, Size: SizeDesc> {
    points: [Coord; 2],
    head: Size,
    width: Size,
    style: ShapeStyle,
}

impl<Coord> TriangleArrow<Coord, i32> {
    pub fn new(nock: Coord, tip: Coord, style: impl Into<ShapeStyle>) -> Self {
        Self {
            points: [nock, tip],
            head: 5,
            width: 5,
            style: style.into(),
        }
    }
}

impl<Coord, Size: SizeDesc> TriangleArrow<Coord, Size> {
    pub fn new_detail(
        nock: Coord,
        tip: Coord,
        head: Size,
        width: Size,
        style: impl Into<ShapeStyle>,
    ) -> Self {
        Self {
            points: [nock, tip],
            head,
            width,
            style: style.into(),
        }
    }

    pub fn head(self, head: Size) -> Self {
        Self { head, ..self }
    }

    pub fn width(self, width: Size) -> Self {
        Self { width, ..self }
    }
}

impl<'a, Coord: 'a, Size: SizeDesc> PointCollection<'a, Coord> for &'a TriangleArrow<Coord, Size> {
    type Point = &'a Coord;

    type IntoIter = &'a [Coord];

    fn point_iter(self) -> Self::IntoIter {
        &self.points
    }
}

impl<DB: DrawingBackend, Coord, Size: SizeDesc> Drawable<DB> for TriangleArrow<Coord, Size> {
    fn draw<I: Iterator<Item = BackendCoord>>(
        &self,
        mut pos: I,
        backend: &mut DB,
        parent_dim: (u32, u32),
    ) -> Result<(), DrawingErrorKind<DB::ErrorType>> {
        let nock = pos.next();
        let tip = pos.next();
        debug_assert!(nock.is_some());
        debug_assert!(tip.is_some());
        if let (Some(nock), Some(tip)) = (nock, tip) {
            let head = self.head.in_pixels(&parent_dim) as f64;
            let width = self.width.in_pixels(&parent_dim) as f64;
            if nock != tip {
                let dx = (tip.0 - nock.0) as f64;
                let dy = (tip.1 - nock.1) as f64;
                let d = f64::sqrt(dx * dx + dy * dy);
                let head = head.min(d);
                let width = width.min(d);
                let half_width = width * 0.5;
                let left = (
                    tip.0 + ((-head * dx + half_width * dy) / d) as i32,
                    tip.1 + ((-head * dy - half_width * dx) / d) as i32,
                );
                let right = (
                    tip.0 + ((-head * dx - half_width * dy) / d) as i32,
                    tip.1 + ((-head * dy + half_width * dx) / d) as i32,
                );
                backend.draw_line(nock, tip, &self.style)?;
                backend.fill_polygon(vec![tip, left, right, tip], &self.style)?;
            }
        }
        Ok(())
    }
}