diff -r 859a41b137d4 -r abb42ba345b6 rust/landgen/src/outline.rs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rust/landgen/src/outline.rs Fri Nov 02 09:45:54 2018 +0100 @@ -0,0 +1,116 @@ +use itertools::Itertools; + +use integral_geometry::{Point, Size}; +use land2d::Land2D; + +use outline_template::OutlineTemplate; + +pub struct OutlinePoints { + pub islands: Vec>, + pub fill_points: Vec, + pub size: Size, +} + +impl OutlinePoints { + pub fn from_outline_template>( + outline_template: &OutlineTemplate, + random_numbers: &mut I, + ) -> Self { + Self { + islands: outline_template + .islands + .iter() + .map(|i| { + i.iter() + .zip(random_numbers.tuples()) + .map(|(rect, (rnd_a, rnd_b))| { + Point::new( + rect.x + (rnd_a % rect.width) as i32, + rect.y + (rnd_b % rect.height) as i32, + ) + }).collect() + }).collect(), + fill_points: outline_template.fill_points.clone(), + size: outline_template.size, + } + } + + pub fn total_len(&self) -> usize { + self.islands.iter().map(|i| i.len()).sum::() + self.fill_points.len() + } + + pub fn iter_mut(&mut self) -> impl Iterator { + self.islands + .iter_mut() + .flat_map(|i| i.iter_mut()) + .chain(self.fill_points.iter_mut()) + } + + fn divide_edge>( + &self, + start_point: Point, + end_point: Point, + random_numbers: &mut I, + ) -> Option { + None + } + + fn divide_edges>(&mut self, random_numbers: &mut I) { + for is in 0..self.islands.len() { + let mut i = 0; + let mut start_point = Point::zero(); + let mut end_point = Point::zero(); + + loop { + { + let island = &self.islands[is]; + if i < island.len() { + start_point = island[i]; + end_point = if i + 1 < island.len() { + island[i + 1] + } else { + island[0] + }; + } else { + break + } + } + + if let Some(new_point) = self.divide_edge(start_point, end_point, random_numbers) { + self.islands[is].insert(i + 1, new_point); + i += 2; + } else { + i += 1; + } + } + } + } + + pub fn bezierize(&mut self) { + unimplemented!() + } + + pub fn distort>(&mut self, random_numbers: &mut I) { + loop { + let old_len = self.total_len(); + self.divide_edges(random_numbers); + + if self.total_len() != old_len { + break; + } + } + + self.bezierize(); + } + + pub fn draw(&self, land: &mut Land2D, value: T) { + for island in &self.islands { + if island.len() > 1 { + for i in 0..island.len() - 1 { + land.draw_line(island[i], island[i + 1], value); + } + land.draw_line(island[island.len() - 1], island[0], value); + } + } + } +}