13938
|
1 |
extern crate vec2d;
|
|
2 |
|
|
3 |
use std::cmp;
|
|
4 |
|
|
5 |
pub struct Land2D<T> {
|
|
6 |
pixels: vec2d::Vec2D<T>,
|
|
7 |
width_mask: usize,
|
|
8 |
height_mask: usize,
|
|
9 |
}
|
|
10 |
|
13952
|
11 |
impl<T: Copy + PartialEq> Land2D<T> {
|
|
12 |
pub fn new(width: usize, height: usize, fill_value: T) -> Self {
|
13938
|
13 |
assert!(width.is_power_of_two());
|
|
14 |
assert!(height.is_power_of_two());
|
|
15 |
|
|
16 |
Self {
|
13952
|
17 |
pixels: vec2d::Vec2D::new(width, height, fill_value),
|
13938
|
18 |
width_mask: !(width - 1),
|
|
19 |
height_mask: !(height - 1),
|
|
20 |
}
|
|
21 |
}
|
|
22 |
|
|
23 |
#[inline]
|
13945
|
24 |
pub fn width(&self) -> usize {
|
|
25 |
self.pixels.width()
|
|
26 |
}
|
|
27 |
|
|
28 |
#[inline]
|
|
29 |
pub fn height(&self) -> usize {
|
|
30 |
self.pixels.height()
|
|
31 |
}
|
|
32 |
|
|
33 |
#[inline]
|
13952
|
34 |
pub fn is_valid_x(&self, x: i32) -> bool {
|
|
35 |
(x as usize & self.width_mask) == 0
|
|
36 |
}
|
|
37 |
|
|
38 |
#[inline]
|
|
39 |
pub fn is_valid_y(&self, y: i32) -> bool {
|
|
40 |
(y as usize & self.height_mask) == 0
|
|
41 |
}
|
|
42 |
|
|
43 |
#[inline]
|
13945
|
44 |
pub fn is_valid_coordinate(&self, x: i32, y: i32) -> bool {
|
13952
|
45 |
self.is_valid_x(x) && self.is_valid_y(y)
|
13938
|
46 |
}
|
|
47 |
|
|
48 |
#[inline]
|
|
49 |
pub fn map<U, F: FnOnce(&mut T) -> U>(&mut self, y: i32, x: i32, f: F) {
|
|
50 |
if self.is_valid_coordinate(x, y) {
|
13952
|
51 |
unsafe { // hey, I just checked that coordinates are valid!
|
|
52 |
f(self.pixels.get_unchecked_mut(y as usize, x as usize));
|
|
53 |
}
|
13938
|
54 |
}
|
|
55 |
}
|
|
56 |
|
|
57 |
pub fn draw_line(&mut self, x1: i32, y1: i32, x2: i32, y2: i32, value: T) {
|
|
58 |
let mut e_x: i32 = 0;
|
|
59 |
let mut e_y: i32 = 0;
|
|
60 |
let mut d_x: i32 = x2 - x1;
|
|
61 |
let mut d_y: i32 = y2 - y1;
|
|
62 |
|
|
63 |
let s_x: i32;
|
|
64 |
let s_y: i32;
|
|
65 |
|
|
66 |
if d_x > 0 {
|
|
67 |
s_x = 1;
|
|
68 |
} else if d_x < 0 {
|
|
69 |
s_x = -1;
|
|
70 |
d_x = -d_x;
|
|
71 |
} else {
|
|
72 |
s_x = d_x;
|
|
73 |
}
|
|
74 |
|
|
75 |
if d_y > 0 {
|
|
76 |
s_y = 1;
|
|
77 |
} else if d_y < 0 {
|
|
78 |
s_y = -1;
|
|
79 |
d_y = -d_y;
|
|
80 |
} else {
|
|
81 |
s_y = d_y;
|
|
82 |
}
|
|
83 |
|
|
84 |
let d = cmp::max(d_x, d_y);
|
|
85 |
|
|
86 |
let mut x = x1;
|
|
87 |
let mut y = y1;
|
|
88 |
|
|
89 |
for _i in 0..=d {
|
|
90 |
e_x += d_x;
|
|
91 |
e_y += d_y;
|
|
92 |
|
|
93 |
if e_x > d {
|
|
94 |
e_x -= d;
|
|
95 |
x += s_x;
|
|
96 |
}
|
|
97 |
if e_y > d {
|
|
98 |
e_y -= d;
|
|
99 |
y += s_y;
|
|
100 |
}
|
|
101 |
|
|
102 |
self.map(y, x, |p| *p = value);
|
|
103 |
}
|
|
104 |
}
|
13945
|
105 |
|
|
106 |
pub fn fill(&mut self, start_x: i32, start_y: i32, border_value: T, fill_value: T) {
|
|
107 |
debug_assert!(self.is_valid_coordinate(start_x - 1, start_y));
|
|
108 |
debug_assert!(self.is_valid_coordinate(start_x, start_y));
|
|
109 |
|
|
110 |
let mut stack: Vec<(usize, usize, usize, isize)> = Vec::new();
|
13952
|
111 |
fn push<T: Copy + PartialEq>(
|
13945
|
112 |
land: &Land2D<T>,
|
|
113 |
stack: &mut Vec<(usize, usize, usize, isize)>,
|
|
114 |
xl: usize,
|
|
115 |
xr: usize,
|
|
116 |
y: usize,
|
|
117 |
dir: isize,
|
|
118 |
) {
|
|
119 |
let yd = y as isize + dir;
|
|
120 |
|
|
121 |
if land.is_valid_coordinate(0, yd as i32) {
|
|
122 |
stack.push((xl, xr, yd as usize, dir));
|
|
123 |
}
|
|
124 |
};
|
|
125 |
|
|
126 |
let start_x_l = (start_x - 1) as usize;
|
|
127 |
let start_x_r = start_x as usize;
|
|
128 |
push(self, &mut stack, start_x_l, start_x_r, start_y as usize, -1);
|
|
129 |
push(self, &mut stack, start_x_l, start_x_r, start_y as usize, 1);
|
|
130 |
|
|
131 |
loop {
|
|
132 |
let a = stack.pop();
|
|
133 |
match a {
|
|
134 |
None => return,
|
|
135 |
Some(a) => {
|
|
136 |
let (mut xl, mut xr, y, mut dir) = a;
|
|
137 |
|
|
138 |
while xl > 0 && self
|
|
139 |
.pixels
|
|
140 |
.get(y, xl)
|
|
141 |
.map_or(false, |v| *v != border_value && *v != fill_value)
|
|
142 |
{
|
|
143 |
xl -= 1;
|
|
144 |
}
|
|
145 |
|
|
146 |
while xr < self.width() - 1 && self
|
|
147 |
.pixels
|
|
148 |
.get(y, xr)
|
|
149 |
.map_or(false, |v| *v != border_value && *v != fill_value)
|
|
150 |
{
|
|
151 |
xr += 1;
|
|
152 |
}
|
|
153 |
|
|
154 |
while xl < xr {
|
|
155 |
while xl <= xr
|
|
156 |
&& (self.pixels[y][xl] == border_value
|
|
157 |
|| self.pixels[y][xl] == fill_value)
|
|
158 |
{
|
|
159 |
xl += 1;
|
|
160 |
}
|
|
161 |
|
|
162 |
let mut x = xl;
|
|
163 |
|
|
164 |
while xl <= xr
|
|
165 |
&& (self.pixels[y][xl] != border_value
|
|
166 |
&& self.pixels[y][xl] != fill_value)
|
|
167 |
{
|
|
168 |
self.pixels[y][xl] = fill_value;
|
|
169 |
|
|
170 |
xl += 1;
|
|
171 |
}
|
|
172 |
|
|
173 |
if x < xl {
|
|
174 |
push(self, &mut stack, x, xl - 1, y, dir);
|
|
175 |
push(self, &mut stack, x, xl - 1, y, -dir);
|
|
176 |
}
|
|
177 |
}
|
|
178 |
}
|
|
179 |
}
|
|
180 |
}
|
|
181 |
}
|
13952
|
182 |
|
|
183 |
#[inline]
|
|
184 |
fn fill_circle_line<F: Fn(&mut T) -> usize>(
|
|
185 |
&mut self,
|
|
186 |
y: i32,
|
|
187 |
x_from: i32,
|
|
188 |
x_to: i32,
|
|
189 |
f: &F,
|
|
190 |
) -> usize {
|
|
191 |
let mut result = 0;
|
|
192 |
|
|
193 |
if self.is_valid_y(y) {
|
|
194 |
for i in cmp::min(x_from, 0) as usize..cmp::max(x_to as usize, self.width() - 1) {
|
|
195 |
unsafe { // coordinates are valid at this point
|
|
196 |
result += f(self.pixels.get_unchecked_mut(y as usize, i));
|
|
197 |
}
|
|
198 |
}
|
|
199 |
}
|
|
200 |
|
|
201 |
result
|
|
202 |
}
|
|
203 |
|
|
204 |
#[inline]
|
|
205 |
fn fill_circle_lines<F: Fn(&mut T) -> usize>(
|
|
206 |
&mut self,
|
|
207 |
x: i32,
|
|
208 |
y: i32,
|
|
209 |
dx: i32,
|
|
210 |
dy: i32,
|
|
211 |
f: &F,
|
|
212 |
) -> usize {
|
|
213 |
self.fill_circle_line(y + dy, x - dx, x + dx, f)
|
|
214 |
+ self.fill_circle_line(y - dy, x - dx, x + dx, f)
|
|
215 |
+ self.fill_circle_line(y + dx, x - dy, x + dy, f)
|
|
216 |
+ self.fill_circle_line(y - dx, x - dy, x + dy, f)
|
|
217 |
}
|
|
218 |
|
|
219 |
pub fn change_round<F: Fn(&mut T) -> usize>(
|
|
220 |
&mut self,
|
|
221 |
x: i32,
|
|
222 |
y: i32,
|
|
223 |
radius: i32,
|
|
224 |
f: F,
|
|
225 |
) -> usize {
|
|
226 |
let mut dx: i32 = 0;
|
|
227 |
let mut dy: i32 = radius;
|
|
228 |
let mut d = 3 - 2 * radius;
|
|
229 |
let mut result = 0;
|
|
230 |
|
|
231 |
while dx < dy {
|
|
232 |
result += self.fill_circle_lines(x, y, dx, dy, &f);
|
|
233 |
|
|
234 |
if d < 0 {
|
|
235 |
d += 4 * dx + 6;
|
|
236 |
} else {
|
|
237 |
d += 4 * (dx - dy) + 10;
|
|
238 |
dy -= 1;
|
|
239 |
}
|
|
240 |
|
|
241 |
dx += 1;
|
|
242 |
}
|
|
243 |
|
|
244 |
if dx == dy {
|
|
245 |
result += self.fill_circle_lines(x, y, dx, dy, &f);
|
|
246 |
}
|
|
247 |
|
|
248 |
result
|
|
249 |
}
|
13938
|
250 |
}
|
|
251 |
|
|
252 |
#[cfg(test)]
|
|
253 |
mod tests {
|
|
254 |
use super::*;
|
|
255 |
|
|
256 |
#[test]
|
|
257 |
fn basics() {
|
13952
|
258 |
let l: Land2D<u8> = Land2D::new(32, 64, 0);
|
13938
|
259 |
|
|
260 |
assert!(l.is_valid_coordinate(0, 0));
|
|
261 |
assert!(!l.is_valid_coordinate(-1, -1));
|
|
262 |
|
|
263 |
assert!(l.is_valid_coordinate(31, 63));
|
|
264 |
assert!(!l.is_valid_coordinate(32, 63));
|
|
265 |
assert!(!l.is_valid_coordinate(31, 64));
|
|
266 |
}
|
|
267 |
|
13945
|
268 |
#[test]
|
|
269 |
fn fill() {
|
13952
|
270 |
let mut l: Land2D<u8> = Land2D::new(128, 128, 0);
|
13945
|
271 |
|
|
272 |
l.draw_line(0, 0, 32, 96, 1);
|
|
273 |
l.draw_line(32, 96, 64, 32, 1);
|
|
274 |
l.draw_line(64, 32, 96, 80, 1);
|
|
275 |
l.draw_line(96, 80, 128, 0, 1);
|
|
276 |
|
|
277 |
l.draw_line(0, 128, 64, 96, 1);
|
|
278 |
l.draw_line(128, 128, 64, 96, 1);
|
|
279 |
|
|
280 |
l.fill(32, 32, 1, 2);
|
|
281 |
l.fill(16, 96, 1, 3);
|
|
282 |
l.fill(60, 100, 1, 4);
|
|
283 |
|
|
284 |
assert_eq!(l.pixels[0][0], 1);
|
|
285 |
assert_eq!(l.pixels[96][64], 1);
|
|
286 |
|
|
287 |
assert_eq!(l.pixels[40][32], 2);
|
|
288 |
assert_eq!(l.pixels[40][96], 2);
|
|
289 |
assert_eq!(l.pixels[5][0], 3);
|
|
290 |
assert_eq!(l.pixels[120][0], 3);
|
|
291 |
assert_eq!(l.pixels[5][127], 3);
|
|
292 |
assert_eq!(l.pixels[120][127], 3);
|
|
293 |
assert_eq!(l.pixels[35][64], 3);
|
|
294 |
assert_eq!(l.pixels[120][20], 4);
|
|
295 |
assert_eq!(l.pixels[120][100], 4);
|
|
296 |
assert_eq!(l.pixels[100][64], 4);
|
|
297 |
}
|
13938
|
298 |
}
|