author | unc0rr |
Wed, 17 Oct 2018 22:58:21 +0200 | |
changeset 13957 | 78c798d655ad |
parent 13955 | 9c112f2ae02d |
child 13959 | 1fa905aa4cdb |
permissions | -rw-r--r-- |
13957 | 1 |
extern crate integral_geometry; |
13938 | 2 |
extern crate vec2d; |
3 |
||
4 |
use std::cmp; |
|
13955
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
5 |
use std::ops; |
13938 | 6 |
|
7 |
pub struct Land2D<T> { |
|
8 |
pixels: vec2d::Vec2D<T>, |
|
9 |
width_mask: usize, |
|
10 |
height_mask: usize, |
|
11 |
} |
|
12 |
||
13952 | 13 |
impl<T: Copy + PartialEq> Land2D<T> { |
14 |
pub fn new(width: usize, height: usize, fill_value: T) -> Self { |
|
13938 | 15 |
assert!(width.is_power_of_two()); |
16 |
assert!(height.is_power_of_two()); |
|
17 |
||
18 |
Self { |
|
13952 | 19 |
pixels: vec2d::Vec2D::new(width, height, fill_value), |
13938 | 20 |
width_mask: !(width - 1), |
21 |
height_mask: !(height - 1), |
|
22 |
} |
|
23 |
} |
|
24 |
||
25 |
#[inline] |
|
13945 | 26 |
pub fn width(&self) -> usize { |
27 |
self.pixels.width() |
|
28 |
} |
|
29 |
||
30 |
#[inline] |
|
31 |
pub fn height(&self) -> usize { |
|
32 |
self.pixels.height() |
|
33 |
} |
|
34 |
||
35 |
#[inline] |
|
13952 | 36 |
pub fn is_valid_x(&self, x: i32) -> bool { |
37 |
(x as usize & self.width_mask) == 0 |
|
38 |
} |
|
39 |
||
40 |
#[inline] |
|
41 |
pub fn is_valid_y(&self, y: i32) -> bool { |
|
42 |
(y as usize & self.height_mask) == 0 |
|
43 |
} |
|
44 |
||
45 |
#[inline] |
|
13945 | 46 |
pub fn is_valid_coordinate(&self, x: i32, y: i32) -> bool { |
13952 | 47 |
self.is_valid_x(x) && self.is_valid_y(y) |
13938 | 48 |
} |
49 |
||
50 |
#[inline] |
|
13955
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
51 |
pub fn map<U: Default, F: FnOnce(&mut T) -> U>(&mut self, y: i32, x: i32, f: F) -> U { |
13938 | 52 |
if self.is_valid_coordinate(x, y) { |
13955
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
53 |
unsafe { |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
54 |
// hey, I just checked that coordinates are valid! |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
55 |
f(self.pixels.get_unchecked_mut(y as usize, x as usize)) |
13952 | 56 |
} |
13955
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
57 |
} else { |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
58 |
U::default() |
13938 | 59 |
} |
60 |
} |
|
61 |
||
13955
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
62 |
fn apply_along_line<U: Default + ops::AddAssign, F: FnMut(i32, i32) -> U>( |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
63 |
x1: i32, |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
64 |
y1: i32, |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
65 |
x2: i32, |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
66 |
y2: i32, |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
67 |
f: &mut F, |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
68 |
) -> U { |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
69 |
let mut result = U::default(); |
13938 | 70 |
let mut e_x: i32 = 0; |
71 |
let mut e_y: i32 = 0; |
|
72 |
let mut d_x: i32 = x2 - x1; |
|
73 |
let mut d_y: i32 = y2 - y1; |
|
74 |
||
75 |
let s_x: i32; |
|
76 |
let s_y: i32; |
|
77 |
||
78 |
if d_x > 0 { |
|
79 |
s_x = 1; |
|
80 |
} else if d_x < 0 { |
|
81 |
s_x = -1; |
|
82 |
d_x = -d_x; |
|
83 |
} else { |
|
84 |
s_x = d_x; |
|
85 |
} |
|
86 |
||
87 |
if d_y > 0 { |
|
88 |
s_y = 1; |
|
89 |
} else if d_y < 0 { |
|
90 |
s_y = -1; |
|
91 |
d_y = -d_y; |
|
92 |
} else { |
|
93 |
s_y = d_y; |
|
94 |
} |
|
95 |
||
96 |
let d = cmp::max(d_x, d_y); |
|
97 |
||
98 |
let mut x = x1; |
|
99 |
let mut y = y1; |
|
100 |
||
101 |
for _i in 0..=d { |
|
102 |
e_x += d_x; |
|
103 |
e_y += d_y; |
|
104 |
||
105 |
if e_x > d { |
|
106 |
e_x -= d; |
|
107 |
x += s_x; |
|
108 |
} |
|
109 |
if e_y > d { |
|
110 |
e_y -= d; |
|
111 |
y += s_y; |
|
112 |
} |
|
113 |
||
13955
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
114 |
result += f(x, y); |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
115 |
} |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
116 |
|
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
117 |
result |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
118 |
} |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
119 |
|
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
120 |
fn apply_around_circle<U: Default + ops::AddAssign, F: FnMut(i32, i32) -> U>( |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
121 |
radius: i32, |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
122 |
f: &mut F, |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
123 |
) -> U { |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
124 |
let mut dx: i32 = 0; |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
125 |
let mut dy: i32 = radius; |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
126 |
let mut d = 3 - 2 * radius; |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
127 |
let mut result = U::default(); |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
128 |
|
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
129 |
while dx < dy { |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
130 |
result += f(dx, dy); |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
131 |
|
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
132 |
if d < 0 { |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
133 |
d += 4 * dx + 6; |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
134 |
} else { |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
135 |
d += 4 * (dx - dy) + 10; |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
136 |
dy -= 1; |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
137 |
} |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
138 |
|
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
139 |
dx += 1; |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
140 |
} |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
141 |
|
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
142 |
if dx == dy { |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
143 |
result += f(dx, dy); |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
144 |
} |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
145 |
|
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
146 |
result |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
147 |
} |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
148 |
|
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
149 |
pub fn draw_line(&mut self, x1: i32, y1: i32, x2: i32, y2: i32, value: T) -> usize { |
13957 | 150 |
integral_geometry::LinePoints::new(x1, y1, x2, y2) |
151 |
.filter_map(|(x, y)| { |
|
152 |
self.map(y, x, |p| { |
|
153 |
*p = value; |
|
154 |
Some(1) |
|
155 |
}) |
|
156 |
}).count() |
|
13938 | 157 |
} |
13945 | 158 |
|
159 |
pub fn fill(&mut self, start_x: i32, start_y: i32, border_value: T, fill_value: T) { |
|
160 |
debug_assert!(self.is_valid_coordinate(start_x - 1, start_y)); |
|
161 |
debug_assert!(self.is_valid_coordinate(start_x, start_y)); |
|
162 |
||
163 |
let mut stack: Vec<(usize, usize, usize, isize)> = Vec::new(); |
|
13952 | 164 |
fn push<T: Copy + PartialEq>( |
13945 | 165 |
land: &Land2D<T>, |
166 |
stack: &mut Vec<(usize, usize, usize, isize)>, |
|
167 |
xl: usize, |
|
168 |
xr: usize, |
|
169 |
y: usize, |
|
170 |
dir: isize, |
|
171 |
) { |
|
172 |
let yd = y as isize + dir; |
|
173 |
||
174 |
if land.is_valid_coordinate(0, yd as i32) { |
|
175 |
stack.push((xl, xr, yd as usize, dir)); |
|
176 |
} |
|
177 |
}; |
|
178 |
||
179 |
let start_x_l = (start_x - 1) as usize; |
|
180 |
let start_x_r = start_x as usize; |
|
181 |
push(self, &mut stack, start_x_l, start_x_r, start_y as usize, -1); |
|
182 |
push(self, &mut stack, start_x_l, start_x_r, start_y as usize, 1); |
|
183 |
||
184 |
loop { |
|
185 |
let a = stack.pop(); |
|
186 |
match a { |
|
187 |
None => return, |
|
188 |
Some(a) => { |
|
189 |
let (mut xl, mut xr, y, mut dir) = a; |
|
190 |
||
191 |
while xl > 0 && self |
|
192 |
.pixels |
|
193 |
.get(y, xl) |
|
194 |
.map_or(false, |v| *v != border_value && *v != fill_value) |
|
195 |
{ |
|
196 |
xl -= 1; |
|
197 |
} |
|
198 |
||
199 |
while xr < self.width() - 1 && self |
|
200 |
.pixels |
|
201 |
.get(y, xr) |
|
202 |
.map_or(false, |v| *v != border_value && *v != fill_value) |
|
203 |
{ |
|
204 |
xr += 1; |
|
205 |
} |
|
206 |
||
207 |
while xl < xr { |
|
208 |
while xl <= xr |
|
209 |
&& (self.pixels[y][xl] == border_value |
|
210 |
|| self.pixels[y][xl] == fill_value) |
|
211 |
{ |
|
212 |
xl += 1; |
|
213 |
} |
|
214 |
||
215 |
let mut x = xl; |
|
216 |
||
217 |
while xl <= xr |
|
218 |
&& (self.pixels[y][xl] != border_value |
|
219 |
&& self.pixels[y][xl] != fill_value) |
|
220 |
{ |
|
221 |
self.pixels[y][xl] = fill_value; |
|
222 |
||
223 |
xl += 1; |
|
224 |
} |
|
225 |
||
226 |
if x < xl { |
|
227 |
push(self, &mut stack, x, xl - 1, y, dir); |
|
228 |
push(self, &mut stack, x, xl - 1, y, -dir); |
|
229 |
} |
|
230 |
} |
|
231 |
} |
|
232 |
} |
|
233 |
} |
|
234 |
} |
|
13952 | 235 |
|
236 |
#[inline] |
|
237 |
fn fill_circle_line<F: Fn(&mut T) -> usize>( |
|
238 |
&mut self, |
|
239 |
y: i32, |
|
240 |
x_from: i32, |
|
241 |
x_to: i32, |
|
242 |
f: &F, |
|
243 |
) -> usize { |
|
244 |
let mut result = 0; |
|
245 |
||
246 |
if self.is_valid_y(y) { |
|
247 |
for i in cmp::min(x_from, 0) as usize..cmp::max(x_to as usize, self.width() - 1) { |
|
13955
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
248 |
unsafe { |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
249 |
// coordinates are valid at this point |
13952 | 250 |
result += f(self.pixels.get_unchecked_mut(y as usize, i)); |
251 |
} |
|
252 |
} |
|
253 |
} |
|
254 |
||
255 |
result |
|
256 |
} |
|
257 |
||
258 |
#[inline] |
|
259 |
fn fill_circle_lines<F: Fn(&mut T) -> usize>( |
|
260 |
&mut self, |
|
261 |
x: i32, |
|
262 |
y: i32, |
|
263 |
dx: i32, |
|
264 |
dy: i32, |
|
265 |
f: &F, |
|
266 |
) -> usize { |
|
267 |
self.fill_circle_line(y + dy, x - dx, x + dx, f) |
|
268 |
+ self.fill_circle_line(y - dy, x - dx, x + dx, f) |
|
269 |
+ self.fill_circle_line(y + dx, x - dy, x + dy, f) |
|
270 |
+ self.fill_circle_line(y - dx, x - dy, x + dy, f) |
|
271 |
} |
|
272 |
||
273 |
pub fn change_round<F: Fn(&mut T) -> usize>( |
|
274 |
&mut self, |
|
275 |
x: i32, |
|
276 |
y: i32, |
|
277 |
radius: i32, |
|
278 |
f: F, |
|
279 |
) -> usize { |
|
13955
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
280 |
<Land2D<T>>::apply_around_circle(radius, &mut |dx, dy| { |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
281 |
self.fill_circle_lines(x, y, dx, dy, &f) |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
282 |
}) |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
283 |
} |
13952 | 284 |
|
13955
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
285 |
#[inline] |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
286 |
fn change_dots_around<U: Default + ops::AddAssign, F: FnMut(i32, i32) -> U>( |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
287 |
x: i32, |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
288 |
y: i32, |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
289 |
xx: i32, |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
290 |
yy: i32, |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
291 |
f: &mut F, |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
292 |
) -> U { |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
293 |
let mut result = U::default(); |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
294 |
result += f(y + yy, x + xx); |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
295 |
result += f(y - yy, x + xx); |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
296 |
result += f(y + yy, x - xx); |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
297 |
result += f(y - yy, x - xx); |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
298 |
result += f(y + xx, x + yy); |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
299 |
result += f(y - xx, x + yy); |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
300 |
result += f(y + xx, x - yy); |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
301 |
result += f(y - xx, x - yy); |
13952 | 302 |
|
303 |
result |
|
304 |
} |
|
13955
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
305 |
|
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
306 |
pub fn draw_thick_line( |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
307 |
&mut self, |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
308 |
x1: i32, |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
309 |
y1: i32, |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
310 |
x2: i32, |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
311 |
y2: i32, |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
312 |
radius: i32, |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
313 |
value: T, |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
314 |
) -> usize { |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
315 |
<Land2D<T>>::apply_around_circle(radius, &mut |dx, dy| { |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
316 |
<Land2D<T>>::apply_along_line(x1, y1, x2, y2, &mut |x, y| { |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
317 |
<Land2D<T>>::change_dots_around(x, y, dx, dy, &mut |x, y| { |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
318 |
self.map(x, y, |p| { |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
319 |
if *p != value { |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
320 |
*p = value; |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
321 |
1 |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
322 |
} else { |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
323 |
0 |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
324 |
} |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
325 |
}) |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
326 |
}) |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
327 |
}) |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
328 |
}) |
9c112f2ae02d
Raise levels of abstraction to implement draw_thick_line() avoiding code duplication
unc0rr
parents:
13952
diff
changeset
|
329 |
} |
13938 | 330 |
} |
331 |
||
332 |
#[cfg(test)] |
|
333 |
mod tests { |
|
334 |
use super::*; |
|
335 |
||
336 |
#[test] |
|
337 |
fn basics() { |
|
13952 | 338 |
let l: Land2D<u8> = Land2D::new(32, 64, 0); |
13938 | 339 |
|
340 |
assert!(l.is_valid_coordinate(0, 0)); |
|
341 |
assert!(!l.is_valid_coordinate(-1, -1)); |
|
342 |
||
343 |
assert!(l.is_valid_coordinate(31, 63)); |
|
344 |
assert!(!l.is_valid_coordinate(32, 63)); |
|
345 |
assert!(!l.is_valid_coordinate(31, 64)); |
|
346 |
} |
|
347 |
||
13945 | 348 |
#[test] |
349 |
fn fill() { |
|
13952 | 350 |
let mut l: Land2D<u8> = Land2D::new(128, 128, 0); |
13945 | 351 |
|
352 |
l.draw_line(0, 0, 32, 96, 1); |
|
353 |
l.draw_line(32, 96, 64, 32, 1); |
|
354 |
l.draw_line(64, 32, 96, 80, 1); |
|
355 |
l.draw_line(96, 80, 128, 0, 1); |
|
356 |
||
357 |
l.draw_line(0, 128, 64, 96, 1); |
|
358 |
l.draw_line(128, 128, 64, 96, 1); |
|
359 |
||
360 |
l.fill(32, 32, 1, 2); |
|
361 |
l.fill(16, 96, 1, 3); |
|
362 |
l.fill(60, 100, 1, 4); |
|
363 |
||
364 |
assert_eq!(l.pixels[0][0], 1); |
|
365 |
assert_eq!(l.pixels[96][64], 1); |
|
366 |
||
367 |
assert_eq!(l.pixels[40][32], 2); |
|
368 |
assert_eq!(l.pixels[40][96], 2); |
|
369 |
assert_eq!(l.pixels[5][0], 3); |
|
370 |
assert_eq!(l.pixels[120][0], 3); |
|
371 |
assert_eq!(l.pixels[5][127], 3); |
|
372 |
assert_eq!(l.pixels[120][127], 3); |
|
373 |
assert_eq!(l.pixels[35][64], 3); |
|
374 |
assert_eq!(l.pixels[120][20], 4); |
|
375 |
assert_eq!(l.pixels[120][100], 4); |
|
376 |
assert_eq!(l.pixels[100][64], 4); |
|
377 |
} |
|
13938 | 378 |
} |