rust/landgen/src/outline.rs
changeset 14076 e5904ead4864
parent 14069 abb42ba345b6
child 14077 5ade484f3351
equal deleted inserted replaced
14075:df0e86b2630f 14076:e5904ead4864
     1 use itertools::Itertools;
     1 use itertools::Itertools;
     2 
     2 
     3 use integral_geometry::{Point, Size};
     3 use integral_geometry::{Line, Point, Size};
     4 use land2d::Land2D;
     4 use land2d::Land2D;
     5 
     5 
     6 use outline_template::OutlineTemplate;
     6 use outline_template::OutlineTemplate;
     7 
     7 
     8 pub struct OutlinePoints {
     8 pub struct OutlinePoints {
    46             .chain(self.fill_points.iter_mut())
    46             .chain(self.fill_points.iter_mut())
    47     }
    47     }
    48 
    48 
    49     fn divide_edge<I: Iterator<Item = u32>>(
    49     fn divide_edge<I: Iterator<Item = u32>>(
    50         &self,
    50         &self,
    51         start_point: Point,
    51         segment: Line,
    52         end_point: Point,
       
    53         random_numbers: &mut I,
    52         random_numbers: &mut I,
    54     ) -> Option<Point> {
    53     ) -> Option<Point> {
    55         None
    54         None
    56     }
    55     }
    57 
    56 
    58     fn divide_edges<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) {
    57     fn divide_edges<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) {
    59         for is in 0..self.islands.len() {
    58         for is in 0..self.islands.len() {
    60             let mut i = 0;
    59             let mut i = 0;
    61             let mut start_point = Point::zero();
    60             let mut segment;
    62             let mut end_point = Point::zero();
       
    63 
    61 
    64             loop {
    62             loop {
    65                 {
    63                 {
    66                     let island = &self.islands[is];
    64                     let island = &self.islands[is];
       
    65                     let mut end_point;
    67                     if i < island.len() {
    66                     if i < island.len() {
    68                         start_point = island[i];
       
    69                         end_point = if i + 1 < island.len() {
    67                         end_point = if i + 1 < island.len() {
    70                             island[i + 1]
    68                             island[i + 1]
    71                         } else {
    69                         } else {
    72                             island[0]
    70                             island[0]
    73                         };
    71                         };
    74                     } else {
    72                     } else {
    75                         break
    73                         break;
    76                     }
    74                     }
       
    75 
       
    76                     segment = Line::new(island[i], end_point);
    77                 }
    77                 }
    78 
    78 
    79                 if let Some(new_point) = self.divide_edge(start_point, end_point, random_numbers) {
    79                 if let Some(new_point) = self.divide_edge(segment, random_numbers) {
    80                     self.islands[is].insert(i + 1, new_point);
    80                     self.islands[is].insert(i + 1, new_point);
    81                     i += 2;
    81                     i += 2;
    82                 } else {
    82                 } else {
    83                     i += 1;
    83                     i += 1;
    84                 }
    84                 }
   102 
   102 
   103         self.bezierize();
   103         self.bezierize();
   104     }
   104     }
   105 
   105 
   106     pub fn draw<T: Copy + PartialEq>(&self, land: &mut Land2D<T>, value: T) {
   106     pub fn draw<T: Copy + PartialEq>(&self, land: &mut Land2D<T>, value: T) {
   107         for island in &self.islands {
   107         for segment in self.segments_iter() {
   108             if island.len() > 1 {
   108             land.draw_line(segment, value);
   109                 for i in 0..island.len() - 1 {
   109         }
   110                     land.draw_line(island[i], island[i + 1], value);
   110     }
   111                 }
   111 
   112                 land.draw_line(island[island.len() - 1], island[0], value);
   112     fn segments_iter(&self) -> OutlineSegmentsIterator {
   113             }
   113         OutlineSegmentsIterator {
       
   114             outline: self,
       
   115             island: 0,
       
   116             index: 0,
   114         }
   117         }
   115     }
   118     }
   116 }
   119 }
       
   120 
       
   121 struct OutlineSegmentsIterator<'a> {
       
   122     outline: &'a OutlinePoints,
       
   123     island: usize,
       
   124     index: usize,
       
   125 }
       
   126 
       
   127 impl<'a> Iterator for OutlineSegmentsIterator<'a> {
       
   128     type Item = Line;
       
   129 
       
   130     fn next(&mut self) -> Option<Self::Item> {
       
   131         if self.island < self.outline.islands.len() {
       
   132             if self.index + 1 < self.outline.islands[self.index].len() {
       
   133                 Some(Line::new(
       
   134                     self.outline.islands[self.index][self.index],
       
   135                     self.outline.islands[self.index][self.index + 1],
       
   136                 ))
       
   137             } else if self.index + 1 == self.outline.islands[self.index].len() {
       
   138                 Some(Line::new(
       
   139                     self.outline.islands[self.index][self.index],
       
   140                     self.outline.islands[self.index][0],
       
   141                 ))
       
   142             } else {
       
   143                 self.island += 1;
       
   144                 self.next()
       
   145             }
       
   146         } else {
       
   147             None
       
   148         }
       
   149     }
       
   150 }