6 |
6 |
7 use super::outline_template::OutlineTemplate; |
7 use super::outline_template::OutlineTemplate; |
8 |
8 |
9 pub struct OutlinePoints { |
9 pub struct OutlinePoints { |
10 pub islands: Vec<Polygon>, |
10 pub islands: Vec<Polygon>, |
|
11 pub walls: Vec<Polygon>, |
11 pub fill_points: Vec<Point>, |
12 pub fill_points: Vec<Point>, |
12 pub size: Size, |
13 pub size: Size, |
13 pub play_box: Rect, |
14 pub play_box: Rect, |
14 pub intersections_box: Rect, |
15 pub intersections_box: Rect, |
15 } |
16 } |
35 }) |
36 }) |
36 .collect::<Vec<_>>() |
37 .collect::<Vec<_>>() |
37 .into() |
38 .into() |
38 }) |
39 }) |
39 .collect(), |
40 .collect(), |
|
41 walls: outline_template |
|
42 .walls |
|
43 .iter() |
|
44 .map(|i| { |
|
45 i.iter() |
|
46 .zip(random_numbers.tuples()) |
|
47 .map(|(rect, (rnd_a, rnd_b))| { |
|
48 play_box.top_left() + rect.quotient(rnd_a as usize, rnd_b as usize) |
|
49 }) |
|
50 .collect::<Vec<_>>() |
|
51 .into() |
|
52 }) |
|
53 .collect(), |
40 fill_points: outline_template.fill_points.clone(), |
54 fill_points: outline_template.fill_points.clone(), |
41 intersections_box: Rect::at_origin(size) |
55 intersections_box: Rect::at_origin(size) |
42 .with_margin(size.to_square().width as i32 * -2), |
56 .with_margin(size.to_square().width as i32 * -2), |
43 } |
57 } |
44 } |
58 } |
49 |
63 |
50 pub fn iter(&self) -> impl Iterator<Item = &Point> { |
64 pub fn iter(&self) -> impl Iterator<Item = &Point> { |
51 self.islands |
65 self.islands |
52 .iter() |
66 .iter() |
53 .flat_map(|p| p.iter()) |
67 .flat_map(|p| p.iter()) |
|
68 .chain(self.walls.iter().flat_map(|p| p.iter())) |
54 .chain(self.fill_points.iter()) |
69 .chain(self.fill_points.iter()) |
55 } |
70 } |
56 |
71 |
57 pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Point> { |
72 pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Point> { |
58 self.islands |
73 self.islands |
59 .iter_mut() |
74 .iter_mut() |
60 .flat_map(|i| i.iter_mut()) |
75 .flat_map(|i| i.iter_mut()) |
|
76 .chain(self.walls.iter_mut().flat_map(|p| p.iter_mut())) |
61 .chain(self.fill_points.iter_mut()) |
77 .chain(self.fill_points.iter_mut()) |
62 } |
78 } |
63 |
79 |
64 fn divide_edge<I: Iterator<Item = u32>>( |
80 fn divide_edge<I: Iterator<Item = u32>>( |
65 &self, |
81 &self, |
290 } |
306 } |
291 } |
307 } |
292 } |
308 } |
293 |
309 |
294 pub fn draw<T: Copy + PartialEq + Default>(&self, land: &mut Land2D<T>, value: T) { |
310 pub fn draw<T: Copy + PartialEq + Default>(&self, land: &mut Land2D<T>, value: T) { |
295 for segment in self.segments_iter() { |
311 for segment in self.visible_segments_iter() { |
296 land.draw_line(segment, value); |
312 land.draw_line(segment, value); |
297 } |
313 } |
298 } |
314 } |
299 |
315 |
|
316 fn visible_segments_iter<'a>(&'a self) -> impl Iterator<Item = Line> + 'a { |
|
317 self.islands |
|
318 .iter() |
|
319 .flat_map(|p| p.iter_edges()) |
|
320 } |
|
321 |
300 fn segments_iter<'a>(&'a self) -> impl Iterator<Item = Line> + 'a { |
322 fn segments_iter<'a>(&'a self) -> impl Iterator<Item = Line> + 'a { |
301 self.islands.iter().flat_map(|p| p.iter_edges()) |
323 self.islands |
|
324 .iter() |
|
325 .flat_map(|p| p.iter_edges()) |
|
326 .chain(self.walls.iter().flat_map(|p| p.iter_edges())) |
302 } |
327 } |
303 |
328 |
304 pub fn mirror(&mut self) { |
329 pub fn mirror(&mut self) { |
305 let r = self.size.width as i32 - 1; |
330 let r = self.size.width as i32 - 1; |
306 |
331 |
320 let mut points = OutlinePoints { |
345 let mut points = OutlinePoints { |
321 islands: vec![ |
346 islands: vec![ |
322 Polygon::new(&[Point::new(0, 0), Point::new(20, 0), Point::new(30, 30)]), |
347 Polygon::new(&[Point::new(0, 0), Point::new(20, 0), Point::new(30, 30)]), |
323 Polygon::new(&[Point::new(10, 15), Point::new(15, 20), Point::new(20, 15)]), |
348 Polygon::new(&[Point::new(10, 15), Point::new(15, 20), Point::new(20, 15)]), |
324 ], |
349 ], |
|
350 walls: vec![], |
325 fill_points: vec![Point::new(1, 1)], |
351 fill_points: vec![Point::new(1, 1)], |
326 play_box: Rect::at_origin(size).with_margin(10), |
352 play_box: Rect::at_origin(size).with_margin(10), |
327 size: Size::square(100), |
353 size: Size::square(100), |
328 intersections_box: Rect::at_origin(size), |
354 intersections_box: Rect::at_origin(size), |
329 }; |
355 }; |