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
use super::Shape;
use crate::style::Color;
pub struct Line {
pub x1: f64,
pub y1: f64,
pub x2: f64,
pub y2: f64,
pub color: Color,
}
pub struct LineIterator {
x: f64,
y: f64,
dx: f64,
dy: f64,
dir_x: f64,
dir_y: f64,
current: f64,
end: f64,
}
impl Iterator for LineIterator {
type Item = (f64, f64);
fn next(&mut self) -> Option<Self::Item> {
if self.current < self.end {
let pos = (
self.x + (self.current * self.dx) / self.end * self.dir_x,
self.y + (self.current * self.dy) / self.end * self.dir_y,
);
self.current += 1.0;
Some(pos)
} else {
None
}
}
}
impl<'a> IntoIterator for &'a Line {
type Item = (f64, f64);
type IntoIter = LineIterator;
fn into_iter(self) -> Self::IntoIter {
let dx = self.x1.max(self.x2) - self.x1.min(self.x2);
let dy = self.y1.max(self.y2) - self.y1.min(self.y2);
let dir_x = if self.x1 <= self.x2 { 1.0 } else { -1.0 };
let dir_y = if self.y1 <= self.y2 { 1.0 } else { -1.0 };
let end = dx.max(dy);
LineIterator {
x: self.x1,
y: self.y1,
dx,
dy,
dir_x,
dir_y,
current: 0.0,
end,
}
}
}
impl<'a> Shape<'a> for Line {
fn color(&self) -> Color {
self.color
}
fn points(&'a self) -> Box<Iterator<Item = (f64, f64)> + 'a> {
Box::new(self.into_iter())
}
}