+#[derive(Debug, Default)]
+pub struct Grid<T> {
+ pub size: Dimension<usize>,
+ pub scale: Dimension<f64>,
+ pub cells: Vec<Vec<T>>,
+}
+
+impl<T> Grid<T> {
+ // pub fn at<C>(&self, c: C) -> Option<&T>
+ // where C: Into<(isize, isize)>
+ // {
+ // let c = c.into();
+ // if c.0 >= 0 && c.0 < self.size.width as isize && c.1 >= 0 && c.1 < self.size.height as isize {
+ // Some(&self.cells[c.0 as usize][c.1 as usize])
+ // } else {
+ // None
+ // }
+ // }
+
+ pub fn to_grid_coordinate<C>(&self, c: C) -> Option<Point<usize>>
+ where C: Into<(isize, isize)>
+ {
+ let c = c.into();
+ if c.0 >= 0 && c.0 < self.size.width as isize && c.1 >= 0 && c.1 < self.size.height as isize {
+ Some(point!(c.0 as usize, c.1 as usize))
+ } else {
+ None
+ }
+ }
+
+ /// Returns a list of grid coordinates that a line in world coordinates passes through.
+ pub fn grid_coordinates_on_line(&self, p1: Point<f64>, p2: Point<f64>) -> Vec<Point<usize>> {
+ supercover_line(p1 / self.scale, p2 / self.scale)
+ .iter()
+ .map(|c| self.to_grid_coordinate(*c))
+ .flatten()
+ .collect()
+ }
+}
+
+////////// WALL REGION /////////////////////////////////////////////////////////
+
+#[derive(Debug, Default)]
+pub struct WallRegion {
+ edges: Vec<Rc<WallEdge>>,
+}
+
+impl WallRegion {
+ pub fn new(points: Vec<Point<f64>>) -> Self {
+ let index: RegionIndex = 0; // use as param
+ let mut edges = Vec::with_capacity(points.len());
+
+ for i in 0..points.len() {
+ let edge = Rc::new(WallEdge {
+ region: index,
+ id: i,
+ p1: points[i],
+ p2: points[(i + 1) % points.len()],
+ });
+ edges.push(edge);
+ }
+
+ WallRegion { edges }
+ }
+
+ fn next(&self, index: EdgeIndex) -> Rc<WallEdge> {
+ let index = (index + 1) % self.edges.len();
+ Rc::clone(&self.edges[index])
+ }
+
+ fn previous(&self, index: EdgeIndex) -> Rc<WallEdge> {
+ let index = (index + self.edges.len() + 1) % self.edges.len();
+ Rc::clone(&self.edges[index])
+ }
+}
+
+////////// WALL EDGE ///////////////////////////////////////////////////////////
+
+type RegionIndex = usize;
+type EdgeIndex = usize;
+
+#[derive(Debug, Default)]
+struct WallEdge {
+ region: RegionIndex,
+ id: EdgeIndex,
+ pub p1: Point<f64>,
+ pub p2: Point<f64>,
+}
+
+impl WallEdge {
+ fn point_is_in_front(&self, p: Point<f64>) -> bool {
+ let cross = (self.p2 - self.p1).cross_product(p - self.p1);
+ cross > 0.0
+ }
+}
+
+////////// WALL ////////////////////////////////////////////////////////////////
+
+pub struct Wall {
+ region: Rc<WallRegion>,
+ edge: Rc<WallEdge>,
+}
+
+impl Wall {
+ #[allow(dead_code)]
+ pub fn next(self) -> Wall {
+ Wall {
+ edge: self.region.next(self.edge.id),
+ region: self.region,
+ }
+ }
+
+ #[allow(dead_code)]
+ pub fn previous(self) -> Wall {
+ Wall {
+ edge: self.region.previous(self.edge.id),
+ region: self.region,
+ }
+ }
+
+ pub fn normal(&self) -> Angle {
+ (self.edge.p2 - self.edge.p1).to_angle() + std::f64::consts::FRAC_PI_2.radians()
+ }