rust/land2d/src/lib.rs
changeset 13969 c6e1769ac9aa
parent 13967 54e2a3698425
child 13970 a1895019bb94
equal deleted inserted replaced
13968:85645992bc8a 13969:c6e1769ac9aa
    80 
    80 
    81     pub fn draw_line(&mut self, from: Point, to: Point, value: T) -> usize {
    81     pub fn draw_line(&mut self, from: Point, to: Point, value: T) -> usize {
    82         self.fill_from_iter(LinePoints::new(from, to), value)
    82         self.fill_from_iter(LinePoints::new(from, to), value)
    83     }
    83     }
    84 
    84 
    85     pub fn fill(&mut self, start_x: i32, start_y: i32, border_value: T, fill_value: T) {
    85     pub fn fill(&mut self, start_point: Point, border_value: T, fill_value: T) {
    86         debug_assert!(self.is_valid_coordinate(start_x - 1, start_y));
    86         debug_assert!(self.is_valid_coordinate(start_point.x - 1, start_point.y));
    87         debug_assert!(self.is_valid_coordinate(start_x, start_y));
    87         debug_assert!(self.is_valid_coordinate(start_point.x, start_point.y));
    88 
    88 
    89         let mut stack: Vec<(usize, usize, usize, isize)> = Vec::new();
    89         let mut stack: Vec<(usize, usize, usize, isize)> = Vec::new();
    90         fn push<T: Copy + PartialEq>(
    90         fn push<T: Copy + PartialEq>(
    91             land: &Land2D<T>,
    91             land: &Land2D<T>,
    92             stack: &mut Vec<(usize, usize, usize, isize)>,
    92             stack: &mut Vec<(usize, usize, usize, isize)>,
   100             if land.is_valid_coordinate(0, yd as i32) {
   100             if land.is_valid_coordinate(0, yd as i32) {
   101                 stack.push((xl, xr, yd as usize, dir));
   101                 stack.push((xl, xr, yd as usize, dir));
   102             }
   102             }
   103         };
   103         };
   104 
   104 
   105         let start_x_l = (start_x - 1) as usize;
   105         let start_x_l = (start_point.x - 1) as usize;
   106         let start_x_r = start_x as usize;
   106         let start_x_r = start_point.x as usize;
   107         push(self, &mut stack, start_x_l, start_x_r, start_y as usize, -1);
   107         push(
   108         push(self, &mut stack, start_x_l, start_x_r, start_y as usize, 1);
   108             self,
       
   109             &mut stack,
       
   110             start_x_l,
       
   111             start_x_r,
       
   112             start_point.y as usize,
       
   113             -1,
       
   114         );
       
   115         push(
       
   116             self,
       
   117             &mut stack,
       
   118             start_x_l,
       
   119             start_x_r,
       
   120             start_point.y as usize,
       
   121             1,
       
   122         );
   109 
   123 
   110         loop {
   124         loop {
   111             let a = stack.pop();
   125             let a = stack.pop();
   112             match a {
   126             match a {
   113                 None => return,
   127                 None => return,
   254         l.draw_line(Point::new(96, 80), Point::new(128, 0), 1);
   268         l.draw_line(Point::new(96, 80), Point::new(128, 0), 1);
   255 
   269 
   256         l.draw_line(Point::new(0, 128), Point::new(64, 96), 1);
   270         l.draw_line(Point::new(0, 128), Point::new(64, 96), 1);
   257         l.draw_line(Point::new(128, 128), Point::new(64, 96), 1);
   271         l.draw_line(Point::new(128, 128), Point::new(64, 96), 1);
   258 
   272 
   259         l.fill(32, 32, 1, 2);
   273         l.fill(Point::new(32, 32), 1, 2);
   260         l.fill(16, 96, 1, 3);
   274         l.fill(Point::new(16, 96), 1, 3);
   261         l.fill(60, 100, 1, 4);
   275         l.fill(Point::new(60, 100), 1, 4);
   262 
   276 
   263         assert_eq!(l.pixels[0][0], 1);
   277         assert_eq!(l.pixels[0][0], 1);
   264         assert_eq!(l.pixels[96][64], 1);
   278         assert_eq!(l.pixels[96][64], 1);
   265 
   279 
   266         assert_eq!(l.pixels[40][32], 2);
   280         assert_eq!(l.pixels[40][32], 2);