|
| 1 | +use crate::utils::point::Point; |
| 2 | +use crate::utils::range::Range; |
| 3 | + |
| 4 | +/// Represents a line on a grid that can only be horizontal or vertical |
| 5 | +#[derive(Debug, Copy, Clone, PartialEq)] |
| 6 | +pub enum GridLine { |
| 7 | + Horizontal { |
| 8 | + y: isize, |
| 9 | + x_start: isize, |
| 10 | + x_end: isize, |
| 11 | + }, |
| 12 | + Vertical { |
| 13 | + x: isize, |
| 14 | + y_start: isize, |
| 15 | + y_end: isize, |
| 16 | + }, |
| 17 | +} |
| 18 | + |
| 19 | +impl GridLine { |
| 20 | + pub fn new(start: Point, end: Point) -> Option<Self> { |
| 21 | + if start.x == end.x { |
| 22 | + Some(Self::Vertical { |
| 23 | + x: start.x, |
| 24 | + y_start: start.y, |
| 25 | + y_end: end.y, |
| 26 | + }) |
| 27 | + } else if start.y == end.y { |
| 28 | + Some(Self::Horizontal { |
| 29 | + y: start.y, |
| 30 | + x_start: start.x, |
| 31 | + x_end: end.x, |
| 32 | + }) |
| 33 | + } else { |
| 34 | + None |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + pub fn start(&self) -> Point { |
| 39 | + match self { |
| 40 | + Self::Horizontal { y, x_start, .. } => Point::new(*x_start, *y), |
| 41 | + Self::Vertical { x, y_start, .. } => Point::new(*x, *y_start), |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + pub fn end(&self) -> Point { |
| 46 | + match self { |
| 47 | + Self::Horizontal { y, x_end, .. } => Point::new(*x_end, *y), |
| 48 | + Self::Vertical { x, y_end, .. } => Point::new(*x, *y_end), |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + pub fn is_vertical(&self) -> bool { |
| 53 | + matches!(self, Self::Vertical { .. }) |
| 54 | + } |
| 55 | + |
| 56 | + #[allow(dead_code)] |
| 57 | + pub fn is_horizontal(&self) -> bool { |
| 58 | + matches!(self, Self::Horizontal { .. }) |
| 59 | + } |
| 60 | + |
| 61 | + /// Returns all points on this grid line from start to end |
| 62 | + pub fn points(&self) -> Vec<Point> { |
| 63 | + match self { |
| 64 | + Self::Horizontal { y, x_start, x_end } => Range::from_unordered(*x_start, *x_end) |
| 65 | + .iter() |
| 66 | + .map(|x| Point::new(x, *y)) |
| 67 | + .collect(), |
| 68 | + Self::Vertical { x, y_start, y_end } => Range::from_unordered(*y_start, *y_end) |
| 69 | + .iter() |
| 70 | + .map(|y| Point::new(*x, y)) |
| 71 | + .collect(), |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// Checks if a point is on this grid line |
| 76 | + pub fn is_on(&self, point: &Point) -> bool { |
| 77 | + match self { |
| 78 | + Self::Horizontal { y, x_start, x_end } => { |
| 79 | + point.y == *y |
| 80 | + && point.x >= (*x_start).min(*x_end) |
| 81 | + && point.x <= (*x_start).max(*x_end) |
| 82 | + } |
| 83 | + Self::Vertical { x, y_start, y_end } => { |
| 84 | + point.x == *x |
| 85 | + && point.y >= (*y_start).min(*y_end) |
| 86 | + && point.y <= (*y_start).max(*y_end) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +#[cfg(test)] |
| 93 | +mod tests { |
| 94 | + use crate::utils::grid_line::GridLine; |
| 95 | + use crate::utils::point::Point; |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn new_horizontal() { |
| 99 | + let line = GridLine::new(Point::new(1, 3), Point::new(4, 3)); |
| 100 | + assert!(line.is_some()); |
| 101 | + assert!(line.unwrap().is_horizontal()); |
| 102 | + } |
| 103 | + |
| 104 | + #[test] |
| 105 | + fn new_vertical() { |
| 106 | + let line = GridLine::new(Point::new(2, 1), Point::new(2, 4)); |
| 107 | + assert!(line.is_some()); |
| 108 | + assert!(line.unwrap().is_vertical()); |
| 109 | + } |
| 110 | + |
| 111 | + #[test] |
| 112 | + fn new_diagonal_returns_none() { |
| 113 | + let line = GridLine::new(Point::new(0, 0), Point::new(3, 3)); |
| 114 | + assert!(line.is_none()); |
| 115 | + } |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn points_horizontal() { |
| 119 | + let line = GridLine::new(Point::new(1, 3), Point::new(4, 3)).unwrap(); |
| 120 | + let expected = vec![ |
| 121 | + Point::new(1, 3), |
| 122 | + Point::new(2, 3), |
| 123 | + Point::new(3, 3), |
| 124 | + Point::new(4, 3), |
| 125 | + ]; |
| 126 | + assert_eq!(line.points(), expected); |
| 127 | + } |
| 128 | + |
| 129 | + #[test] |
| 130 | + fn points_horizontal_reversed() { |
| 131 | + let line = GridLine::new(Point::new(4, 3), Point::new(1, 3)).unwrap(); |
| 132 | + let expected = vec![ |
| 133 | + Point::new(1, 3), |
| 134 | + Point::new(2, 3), |
| 135 | + Point::new(3, 3), |
| 136 | + Point::new(4, 3), |
| 137 | + ]; |
| 138 | + assert_eq!(line.points(), expected); |
| 139 | + } |
| 140 | + |
| 141 | + #[test] |
| 142 | + fn points_vertical() { |
| 143 | + let line = GridLine::new(Point::new(2, 1), Point::new(2, 4)).unwrap(); |
| 144 | + let expected = vec![ |
| 145 | + Point::new(2, 1), |
| 146 | + Point::new(2, 2), |
| 147 | + Point::new(2, 3), |
| 148 | + Point::new(2, 4), |
| 149 | + ]; |
| 150 | + assert_eq!(line.points(), expected); |
| 151 | + } |
| 152 | + |
| 153 | + #[test] |
| 154 | + fn points_vertical_reversed() { |
| 155 | + let line = GridLine::new(Point::new(2, 4), Point::new(2, 1)).unwrap(); |
| 156 | + let expected = vec![ |
| 157 | + Point::new(2, 1), |
| 158 | + Point::new(2, 2), |
| 159 | + Point::new(2, 3), |
| 160 | + Point::new(2, 4), |
| 161 | + ]; |
| 162 | + assert_eq!(line.points(), expected); |
| 163 | + } |
| 164 | + |
| 165 | + #[test] |
| 166 | + fn is_on_horizontal() { |
| 167 | + let line = GridLine::new(Point::new(1, 3), Point::new(4, 3)).unwrap(); |
| 168 | + assert!(line.is_on(&Point::new(1, 3))); |
| 169 | + assert!(line.is_on(&Point::new(2, 3))); |
| 170 | + assert!(line.is_on(&Point::new(4, 3))); |
| 171 | + assert!(!line.is_on(&Point::new(0, 3))); |
| 172 | + assert!(!line.is_on(&Point::new(5, 3))); |
| 173 | + assert!(!line.is_on(&Point::new(2, 4))); |
| 174 | + } |
| 175 | + |
| 176 | + #[test] |
| 177 | + fn is_on_vertical() { |
| 178 | + let line = GridLine::new(Point::new(2, 1), Point::new(2, 4)).unwrap(); |
| 179 | + assert!(line.is_on(&Point::new(2, 1))); |
| 180 | + assert!(line.is_on(&Point::new(2, 2))); |
| 181 | + assert!(line.is_on(&Point::new(2, 4))); |
| 182 | + assert!(!line.is_on(&Point::new(2, 0))); |
| 183 | + assert!(!line.is_on(&Point::new(2, 5))); |
| 184 | + assert!(!line.is_on(&Point::new(3, 2))); |
| 185 | + } |
| 186 | +} |
0 commit comments