+
+impl Region {
+ fn enclosing_rect(&self) -> (usize, usize, usize, usize) {
+ let mut min = (usize::MAX, usize::MAX);
+ let mut max = (0, 0);
+ for c in &self.cells {
+ if c.0 < min.0 { min.0 = c.0; }
+ else if c.0 > max.0 { max.0 = c.0; }
+ if c.1 < min.1 { min.1 = c.1; }
+ else if c.1 > max.1 { max.1 = c.1; }
+ }
+ (min.0, min.1, 1 + max.0 - min.0, 1 + max.1 - min.1)
+ }
+
+ pub fn outline(&mut self, scale: usize) -> Vec<Point2D<isize>> {
+ let rect = self.enclosing_rect();
+ let (ox, oy, w, h) = rect;
+ let grid = self.grid(&rect);
+ let mut marked = vec!(vec!(false; h); w);
+ let mut outline = vec!();
+
+ let (mut p, mut dir) = self.find_first_point_of_outline(&rect, &grid);
+// println!("starting at {:?} with dir {:?}", p, dir);
+ marked[p.x as usize][p.y as usize] = true;
+ loop {
+ outline.push((p + (ox as isize, oy as isize)) * scale as isize);
+ let result = self.find_next_point_of_outline(&grid, p, dir);
+ p = result.0;
+ dir = result.1;
+// println!("next at {:?} with dir {:?}", p, dir);
+ if marked[p.x as usize][p.y as usize] {
+ // we're back at the beginning
+ break;
+ }
+ marked[p.x as usize][p.y as usize] = true;
+ }
+
+ outline
+ }
+
+ fn grid(&self, rect: &(usize, usize, usize, usize)) -> Vec<Vec<bool>> {
+ let (x, y, w, h) = rect;
+ let mut grid = vec!(vec!(false; *h); *w);
+ for c in &self.cells {
+ grid[c.0 - x][c.1 - y] = true;
+ }
+ grid
+ }
+
+ fn find_first_point_of_outline(&self, rect: &(usize, usize, usize, usize), grid: &Vec<Vec<bool>>) -> (Point2D<isize>, Point2D<isize>) {
+ let (ox, oy, w, h) = rect;
+ let is_outer_wall = (ox, oy) == (&0, &0); // we know this is always the outer wall of the level
+ for x in 0..*w {
+ for y in 0..*h {
+ if is_outer_wall && !grid[x][y] {
+ return (point!(x as isize, y as isize - 1), point!(0, 1)) // one step back because we're not on a wall tile
+ }
+ else if !is_outer_wall && grid[x][y] {
+ return (point!(x as isize, y as isize), point!(1, 0))
+ }
+ }
+ }
+ panic!("no wall found!");
+ }
+
+ fn find_next_point_of_outline(&self, grid: &Vec<Vec<bool>>, p: Point2D<isize>, dir: Point2D<isize>) -> (Point2D<isize>, Point2D<isize>) {
+ let left = match dir.into() {
+ (-1, 0) => (0, 1),
+ (0, 1) => (1, 0),
+ (1, 0) => (0, -1),
+ (0, -1) => (-1, 0),
+ _ => (0, 0),
+ };
+ let right = match dir.into() {
+ (0, 1) => (-1, 0),
+ (1, 0) => (0, 1),
+ (0, -1) => (1, 0),
+ (-1, 0) => (0, -1),
+ _ => (0, 0),
+ };
+ if self.check(p + dir, grid) {
+// println!("{:?} is true", p + dir);
+ if self.check(p + dir + left, grid) {
+// println!("going left to {:?}", p + dir + left);
+ return (p + dir + left, left.into())
+ } else {
+ return (p + dir, dir)
+ }
+ } else {
+// println!("{:?} is false", p + dir);
+ if self.check(p + dir + right, grid) {
+// println!("going right to {:?}", p + dir + right);
+ return (p + dir + right, dir)
+ } else {
+// println!("going right from p to {:?}", p + right);
+ return (p + right, right.into())
+ }
+ }
+ }
+
+ fn check(&self, p: Point2D<isize>, grid: &Vec<Vec<bool>>) -> bool {
+ if p.x < 0 || p.x >= grid.len() as isize || p.y < 0 || p.y >= grid[0].len() as isize {
+ false
+ } else {
+ grid[p.x as usize][p.y as usize]
+ }
+ }
+}