29 rect.y + (rnd_b % rect.height) as i32, |
29 rect.y + (rnd_b % rect.height) as i32, |
30 ) |
30 ) |
31 }).collect() |
31 }).collect() |
32 }).collect(), |
32 }).collect(), |
33 fill_points: outline_template.fill_points.clone(), |
33 fill_points: outline_template.fill_points.clone(), |
34 size: outline_template.size |
34 size: outline_template.size, |
35 } |
35 } |
|
36 } |
|
37 |
|
38 fn total_len(&self) -> usize { |
|
39 self.islands.iter().map(|i| i.len()).sum::<usize>() + self.fill_points.len() |
36 } |
40 } |
37 |
41 |
38 fn iter_mut(&mut self) -> impl Iterator<Item = &mut Point> { |
42 fn iter_mut(&mut self) -> impl Iterator<Item = &mut Point> { |
39 self.islands.iter_mut() |
43 self.islands |
|
44 .iter_mut() |
40 .flat_map(|i| i.iter_mut()) |
45 .flat_map(|i| i.iter_mut()) |
41 .chain(self.fill_points.iter_mut()) |
46 .chain(self.fill_points.iter_mut()) |
42 } |
47 } |
43 |
48 |
|
49 fn divide_edge<I: Iterator<Item = u32>>( |
|
50 &self, |
|
51 start_point: Point, |
|
52 end_point: Point, |
|
53 random_numbers: &mut I, |
|
54 ) -> Option<Point> { |
|
55 None |
|
56 } |
|
57 |
|
58 fn divide_edges<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) { |
|
59 for is in 0..self.islands.len() { |
|
60 let island = &mut self.islands[is]; |
|
61 let mut i = 0; |
|
62 |
|
63 while i < island.len() { |
|
64 let start_point = island[i]; |
|
65 let end_point = if i + 1 < island.len() { |
|
66 island[i + 1] |
|
67 } else { |
|
68 island[0] |
|
69 }; |
|
70 |
|
71 if let Some(new_point) = self.divide_edge(start_point, end_point, random_numbers) { |
|
72 (*island).insert(i + 1, new_point); |
|
73 i += 2; |
|
74 } else { |
|
75 i += 1; |
|
76 } |
|
77 } |
|
78 } |
|
79 } |
|
80 |
|
81 fn bezierize(&mut self) { |
|
82 unimplemented!() |
|
83 } |
|
84 |
44 fn distort<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) { |
85 fn distort<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) { |
45 unimplemented!() |
86 loop { |
|
87 let old_len = self.total_len(); |
|
88 self.divide_edges(random_numbers); |
|
89 |
|
90 if self.total_len() != old_len { |
|
91 break; |
|
92 } |
|
93 } |
|
94 |
|
95 self.bezierize(); |
|
96 } |
|
97 |
|
98 fn draw<T: Copy + PartialEq>(&self, land: &mut Land2D<T>, value: T) { |
|
99 for island in &self.islands { |
|
100 if island.len() > 1 { |
|
101 for i in 0..island.len() - 1 { |
|
102 land.draw_line(island[i], island[i + 1], value); |
|
103 } |
|
104 land.draw_line(island[island.len() - 1], island[0], value); |
|
105 } |
|
106 } |
46 } |
107 } |
47 } |
108 } |
48 |
109 |
49 pub struct OutlineTemplate { |
110 pub struct OutlineTemplate { |
50 islands: Vec<Vec<Rect>>, |
111 islands: Vec<Vec<Rect>>, |
63 islands: Vec::new(), |
124 islands: Vec::new(), |
64 fill_points: Vec::new(), |
125 fill_points: Vec::new(), |
65 can_flip: false, |
126 can_flip: false, |
66 can_invert: false, |
127 can_invert: false, |
67 can_mirror: false, |
128 can_mirror: false, |
68 is_negative: false |
129 is_negative: false, |
69 } |
130 } |
70 } |
131 } |
71 |
132 |
72 pub fn flippable(self) -> Self { |
133 pub fn flippable(self) -> Self { |
73 Self { can_flip: true, ..self } |
134 Self { |
|
135 can_flip: true, |
|
136 ..self |
|
137 } |
74 } |
138 } |
75 |
139 |
76 pub fn mirrorable(self) -> Self { |
140 pub fn mirrorable(self) -> Self { |
77 Self { can_mirror: true, ..self } |
141 Self { |
|
142 can_mirror: true, |
|
143 ..self |
|
144 } |
78 } |
145 } |
79 |
146 |
80 pub fn invertable(self) -> Self { |
147 pub fn invertable(self) -> Self { |
81 Self { can_invert: true, ..self } |
148 Self { |
|
149 can_invert: true, |
|
150 ..self |
|
151 } |
82 } |
152 } |
83 |
153 |
84 pub fn negative(self) -> Self { |
154 pub fn negative(self) -> Self { |
85 Self { is_negative: true, ..self } |
155 Self { |
86 } |
156 is_negative: true, |
87 |
157 ..self |
88 pub fn with_fill_points(self, fill_points: Vec<Point>) -> Self |
158 } |
89 { |
159 } |
90 Self { fill_points, ..self } |
160 |
91 } |
161 pub fn with_fill_points(self, fill_points: Vec<Point>) -> Self { |
92 |
162 Self { |
93 pub fn with_islands(mut self, islands: Vec<Vec<Rect>>) -> Self { |
163 fill_points, |
|
164 ..self |
|
165 } |
|
166 } |
|
167 |
|
168 pub fn with_islands(self, islands: Vec<Vec<Rect>>) -> Self { |
94 Self { islands, ..self } |
169 Self { islands, ..self } |
95 } |
170 } |
96 |
171 |
97 pub fn add_fill_points(mut self, points: &[Point]) -> Self { |
172 pub fn add_fill_points(mut self, points: &[Point]) -> Self { |
98 self.fill_points.extend_from_slice(points); self |
173 self.fill_points.extend_from_slice(points); |
|
174 self |
99 } |
175 } |
100 |
176 |
101 pub fn add_island(mut self, island: &[Rect]) -> Self { |
177 pub fn add_island(mut self, island: &[Rect]) -> Self { |
102 self.islands.push(island.into()); self |
178 self.islands.push(island.into()); |
|
179 self |
103 } |
180 } |
104 } |
181 } |
105 |
182 |
106 pub struct TemplatedLandGenerator { |
183 pub struct TemplatedLandGenerator { |
107 outline_template: OutlineTemplate, |
184 outline_template: OutlineTemplate, |
135 |
212 |
136 // mirror |
213 // mirror |
137 if self.outline_template.can_mirror { |
214 if self.outline_template.can_mirror { |
138 if let Some(b) = random_numbers.next() { |
215 if let Some(b) = random_numbers.next() { |
139 if b & 1 != 0 { |
216 if b & 1 != 0 { |
140 points.iter_mut().for_each(|p| p.x = land.width() as i32 - 1 - p.x); |
217 points |
|
218 .iter_mut() |
|
219 .for_each(|p| p.x = land.width() as i32 - 1 - p.x); |
141 } |
220 } |
142 } |
221 } |
143 } |
222 } |
144 |
223 |
145 // flip |
224 // flip |
146 if self.outline_template.can_flip { |
225 if self.outline_template.can_flip { |
147 if let Some(b) = random_numbers.next() { |
226 if let Some(b) = random_numbers.next() { |
148 if b & 1 != 0 { |
227 if b & 1 != 0 { |
149 points.iter_mut().for_each(|p| |
228 points |
150 p.y = land.height() as i32 - 1 - p.y); |
229 .iter_mut() |
|
230 .for_each(|p| p.y = land.height() as i32 - 1 - p.y); |
151 } |
231 } |
152 } |
232 } |
153 } |
233 } |
154 |
234 |
155 points.distort(random_numbers); |
235 points.distort(random_numbers); |
156 |
236 |
157 // draw_edge(points, land, parameters.zero) |
237 points.draw(&mut land, parameters.zero); |
158 |
238 |
159 for p in points.fill_points { |
239 for p in &points.fill_points { |
160 land.fill(p, parameters.zero, parameters.zero) |
240 land.fill(*p, parameters.zero, parameters.zero) |
161 } |
241 } |
162 |
242 |
163 // draw_edge(points, land, parameters.basic) |
243 points.draw(&mut land, parameters.basic); |
164 |
244 |
165 land |
245 land |
166 } |
246 } |
167 } |
247 } |
168 |
248 |
169 #[test()] |
249 #[test()] |
170 fn points_test() { |
250 fn points_test() { |
171 let mut points = OutlinePoints { |
251 let mut points = OutlinePoints { |
172 islands: vec![vec![]], |
252 islands: vec![vec![]], |
173 fill_points: vec![Point::new(1, 1)], |
253 fill_points: vec![Point::new(1, 1)], |
174 size: Size::square(100) |
254 size: Size::square(100), |
175 }; |
255 }; |
176 |
256 |
177 points.iter_mut().for_each(|p| p.x = 2); |
257 points.iter_mut().for_each(|p| p.x = 2); |
178 assert_eq!(points.fill_points[0].x, 2); |
258 assert_eq!(points.fill_points[0].x, 2); |
179 } |
259 } |