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