diff -r 5ade484f3351 -r bf40b5f938b0 rust/landgen/src/outline.rs --- a/rust/landgen/src/outline.rs Fri Nov 02 13:30:04 2018 +0100 +++ b/rust/landgen/src/outline.rs Fri Nov 02 14:29:24 2018 +0100 @@ -1,6 +1,6 @@ use itertools::Itertools; -use integral_geometry::{Line, Point, Size}; +use integral_geometry::{Line, Point, Rect, Size}; use land2d::Land2D; use outline_template::OutlineTemplate; @@ -9,14 +9,19 @@ pub islands: Vec>, pub fill_points: Vec, pub size: Size, + pub play_box: Rect, } impl OutlinePoints { pub fn from_outline_template>( outline_template: &OutlineTemplate, + play_box: Rect, + size: Size, random_numbers: &mut I, ) -> Self { Self { + play_box, + size, islands: outline_template .islands .iter() @@ -24,14 +29,15 @@ 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, - ) + rect.top_left() + + Point::new( + (rnd_a % rect.width) as i32, + (rnd_b % rect.height) as i32, + ) + + play_box.top_left() }).collect() }).collect(), fill_points: outline_template.fill_points.clone(), - size: outline_template.size, } } @@ -51,7 +57,6 @@ segment: Line, random_numbers: &mut I, ) -> Option { - None } @@ -130,18 +135,28 @@ fn next(&mut self) -> Option { if self.island < self.outline.islands.len() { - if self.index + 1 < self.outline.islands[self.index].len() { - Some(Line::new( - self.outline.islands[self.index][self.index], - self.outline.islands[self.index][self.index + 1], - )) - } else if self.index + 1 == self.outline.islands[self.index].len() { - Some(Line::new( - self.outline.islands[self.index][self.index], - self.outline.islands[self.index][0], - )) + if self.index + 1 < self.outline.islands[self.island].len() { + let result = Some(Line::new( + self.outline.islands[self.island][self.index], + self.outline.islands[self.island][self.index + 1], + )); + + self.index += 1; + + result + } else if self.index + 1 == self.outline.islands[self.island].len() { + let result = Some(Line::new( + self.outline.islands[self.island][self.index], + self.outline.islands[self.island][0], + )); + + self.island += 1; + self.index = 0; + + result } else { self.island += 1; + self.index = 0; self.next() } } else { @@ -149,3 +164,30 @@ } } } + +#[test()] +fn points_test() { + let mut points = OutlinePoints { + islands: vec![ + vec![Point::new(0, 0), Point::new(20, 0), Point::new(30, 30)], + vec![Point::new(10, 15), Point::new(15, 20), Point::new(20, 15)], + ], + fill_points: vec![Point::new(1, 1)], + play_box: Rect::from_box(0, 100, 0, 100).with_margin(10), + size: Size::square(100), + }; + + let segments: Vec = points.segments_iter().collect(); + assert_eq!( + segments.first(), + Some(&Line::new(Point::new(0, 0), Point::new(20, 0))) + ); + assert_eq!( + segments.last(), + Some(&Line::new(Point::new(20, 15), Point::new(10, 15))) + ); + + points.iter_mut().for_each(|p| p.x = 2); + assert_eq!(points.fill_points[0].x, 2); + assert_eq!(points.islands[0][0].x, 2); +}