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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use super::super::drawable::*;
use super::super::transform::*;
use coord::{Coord, ToUnsigned};
use pixelcolor::PixelColor;
use style::Style;
use style::WithStyle;
#[derive(Debug, Copy, Clone)]
pub struct Circle<C: PixelColor> {
pub center: Coord,
pub radius: u32,
pub style: Style<C>,
}
impl<C> Circle<C>
where
C: PixelColor,
{
pub fn new(center: Coord, radius: u32) -> Self {
Circle {
center,
radius,
style: Style::default(),
}
}
}
impl<C> WithStyle<C> for Circle<C>
where
C: PixelColor,
{
fn with_style(mut self, style: Style<C>) -> Self {
self.style = style;
self
}
fn with_stroke(mut self, color: Option<C>) -> Self {
self.style.stroke_color = color;
self
}
fn with_stroke_width(mut self, width: u8) -> Self {
self.style.stroke_width = width;
self
}
fn with_fill(mut self, color: Option<C>) -> Self {
self.style.fill_color = color;
self
}
}
impl<'a, C> IntoIterator for &'a Circle<C>
where
C: PixelColor,
{
type Item = Pixel<C>;
type IntoIter = CircleIterator<C>;
fn into_iter(self) -> Self::IntoIter {
CircleIterator {
center: self.center,
radius: self.radius,
style: self.style,
x: -(self.radius as i32),
y: -(self.radius as i32),
}
}
}
#[derive(Debug, Copy, Clone)]
pub struct CircleIterator<C: PixelColor> {
center: Coord,
radius: u32,
style: Style<C>,
x: i32,
y: i32,
}
impl<C> Iterator for CircleIterator<C>
where
C: PixelColor,
{
type Item = Pixel<C>;
fn next(&mut self) -> Option<Self::Item> {
if self.style.stroke_color.is_none() {
return None;
}
let cx = self.center[0];
let cy = self.center[1];
let radius = self.radius as i32 - self.style.stroke_width as i32 + 1;
let outer_radius = self.radius as i32;
let radius_sq = radius * radius;
let outer_radius_sq = outer_radius * outer_radius;
let item = loop {
let tx = self.x;
let ty = self.y;
let len = tx * tx + ty * ty;
let is_border = len > radius_sq - radius && len < outer_radius_sq + radius;
let is_fill = len <= outer_radius_sq;
let item = if is_border && self.style.stroke_color.is_some() {
Some((
cx + tx,
cy + ty,
self.style.stroke_color.expect("Border color not defined"),
))
} else if is_fill && self.style.fill_color.is_some() {
Some((
cx + tx,
cy + ty,
self.style.fill_color.expect("Fill color not defined"),
))
} else {
None
};
self.x += 1;
if self.x > self.radius as i32 {
self.x = -(self.radius as i32);
self.y += 1;
}
if self.y > self.radius as i32 {
break None;
}
if let Some(i) = item {
if i.0 >= 0 && i.1 >= 0 {
break item;
}
}
};
item.map(|(x, y, c)| Pixel(Coord::new(x, y).to_unsigned(), c))
}
}
impl<C> Drawable for Circle<C> where C: PixelColor {}
impl<C> Transform for Circle<C>
where
C: PixelColor,
{
fn translate(&self, by: Coord) -> Self {
Self {
center: self.center + by,
..self.clone()
}
}
fn translate_mut(&mut self, by: Coord) -> &mut Self {
self.center += by;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use dev::TestPixelColor;
#[test]
fn it_handles_offscreen_coords() {
let mut circ: CircleIterator<TestPixelColor> = Circle::new(Coord::new(-10, -10), 5)
.with_style(Style::with_stroke(1u8.into()))
.into_iter();
assert_eq!(circ.next(), None);
}
#[test]
fn it_handles_partially_on_screen_coords() {
let mut circ: CircleIterator<TestPixelColor> = Circle::new(Coord::new(-5, -5), 30)
.with_style(Style::with_stroke(1u8.into()))
.into_iter();
assert!(circ.next().is_some());
}
}