|
| 1 | +use crate::utils::direction::Direction; |
1 | 2 | use crate::utils::point::Point; |
2 | 3 | use crate::utils::range::Range; |
3 | 4 |
|
@@ -53,7 +54,7 @@ impl GridLine { |
53 | 54 | matches!(self, Self::Vertical { .. }) |
54 | 55 | } |
55 | 56 |
|
56 | | - #[allow(dead_code)] |
| 57 | + #[expect(dead_code)] |
57 | 58 | pub fn is_horizontal(&self) -> bool { |
58 | 59 | matches!(self, Self::Horizontal { .. }) |
59 | 60 | } |
@@ -87,10 +88,60 @@ impl GridLine { |
87 | 88 | } |
88 | 89 | } |
89 | 90 | } |
| 91 | + |
| 92 | + /// It extends its size by 1 in the direction of orientation |
| 93 | + #[expect(dead_code)] |
| 94 | + pub fn extend(&self) -> Self { |
| 95 | + match self { |
| 96 | + GridLine::Horizontal { |
| 97 | + y: _, |
| 98 | + x_start, |
| 99 | + x_end, |
| 100 | + } => { |
| 101 | + if x_start < x_end { |
| 102 | + GridLine::new(self.start().west(), self.end().east()).unwrap() |
| 103 | + } else { |
| 104 | + GridLine::new(self.start().east(), self.end().west()).unwrap() |
| 105 | + } |
| 106 | + } |
| 107 | + GridLine::Vertical { |
| 108 | + x: _, |
| 109 | + y_start, |
| 110 | + y_end, |
| 111 | + } => { |
| 112 | + if y_start < y_end { |
| 113 | + GridLine::new(self.start().north(), self.end().south()).unwrap() |
| 114 | + } else { |
| 115 | + GridLine::new(self.start().south(), self.end().north()).unwrap() |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /// Move line in direction by 1 |
| 122 | + #[expect(dead_code)] |
| 123 | + pub fn moved(&self, direction: Direction) -> Option<Self> { |
| 124 | + let start = self.start(); |
| 125 | + let end = self.end(); |
| 126 | + |
| 127 | + match direction { |
| 128 | + Direction::North => Self::new(start.north(), end.north()), |
| 129 | + Direction::East => Self::new(start.east(), end.east()), |
| 130 | + Direction::South => Self::new(start.south(), end.south()), |
| 131 | + Direction::West => Self::new(start.west(), end.west()), |
| 132 | + _ => unimplemented!("Direction {} unimplemented to move grid line", direction), |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + #[expect(dead_code)] |
| 137 | + pub fn direction(&self) -> Direction { |
| 138 | + self.start().direction(&self.end()) |
| 139 | + } |
90 | 140 | } |
91 | 141 |
|
92 | 142 | #[cfg(test)] |
93 | 143 | mod tests { |
| 144 | + use crate::utils::direction::Direction; |
94 | 145 | use crate::utils::grid_line::GridLine; |
95 | 146 | use crate::utils::point::Point; |
96 | 147 |
|
@@ -183,4 +234,104 @@ mod tests { |
183 | 234 | assert!(!line.is_on(&Point::new(2, 5))); |
184 | 235 | assert!(!line.is_on(&Point::new(3, 2))); |
185 | 236 | } |
| 237 | + |
| 238 | + #[test] |
| 239 | + fn extend_vertical() { |
| 240 | + let line = GridLine::new(Point::new(1, 1), Point::new(4, 1)).unwrap(); |
| 241 | + let extended = line.extend(); |
| 242 | + |
| 243 | + assert_eq!(Point::new(0, 1), extended.start()); |
| 244 | + assert_eq!(Point::new(5, 1), extended.end()); |
| 245 | + } |
| 246 | + |
| 247 | + #[test] |
| 248 | + fn extend_vertical_reversed() { |
| 249 | + let line = GridLine::new(Point::new(5, 1), Point::new(0, 1)).unwrap(); |
| 250 | + let extended = line.extend(); |
| 251 | + |
| 252 | + assert_eq!(Point::new(6, 1), extended.start()); |
| 253 | + assert_eq!(Point::new(-1, 1), extended.end()); |
| 254 | + } |
| 255 | + |
| 256 | + #[test] |
| 257 | + fn extend_horizontal() { |
| 258 | + let line = GridLine::new(Point::new(5, 4), Point::new(5, 11)).unwrap(); |
| 259 | + let extended = line.extend(); |
| 260 | + |
| 261 | + assert_eq!(Point::new(5, 3), extended.start()); |
| 262 | + assert_eq!(Point::new(5, 12), extended.end()); |
| 263 | + } |
| 264 | + |
| 265 | + #[test] |
| 266 | + fn extend_horizontal_reversed() { |
| 267 | + let line = GridLine::new(Point::new(3, 8), Point::new(3, 2)).unwrap(); |
| 268 | + let extended = line.extend(); |
| 269 | + |
| 270 | + assert_eq!(Point::new(3, 9), extended.start()); |
| 271 | + assert_eq!(Point::new(3, 1), extended.end()); |
| 272 | + } |
| 273 | + |
| 274 | + #[test] |
| 275 | + fn moved_horizontal() { |
| 276 | + let line = GridLine::new(Point::new(3, 8), Point::new(3, 2)).unwrap(); |
| 277 | + let east = line.moved(Direction::East); |
| 278 | + |
| 279 | + assert!(east.is_some()); |
| 280 | + assert_eq!( |
| 281 | + GridLine::new(Point::new(4, 8), Point::new(4, 2)).unwrap(), |
| 282 | + east.unwrap() |
| 283 | + ); |
| 284 | + |
| 285 | + let west = line.moved(Direction::West); |
| 286 | + |
| 287 | + assert!(west.is_some()); |
| 288 | + assert_eq!( |
| 289 | + GridLine::new(Point::new(2, 8), Point::new(2, 2)).unwrap(), |
| 290 | + west.unwrap() |
| 291 | + ); |
| 292 | + } |
| 293 | + |
| 294 | + #[test] |
| 295 | + fn moved_vertical() { |
| 296 | + let line = GridLine::new(Point::new(2, 7), Point::new(7, 7)).unwrap(); |
| 297 | + let north = line.moved(Direction::North); |
| 298 | + |
| 299 | + assert!(north.is_some()); |
| 300 | + assert_eq!( |
| 301 | + GridLine::new(Point::new(2, 6), Point::new(7, 6)).unwrap(), |
| 302 | + north.unwrap() |
| 303 | + ); |
| 304 | + |
| 305 | + let south = line.moved(Direction::South); |
| 306 | + |
| 307 | + assert!(south.is_some()); |
| 308 | + assert_eq!( |
| 309 | + GridLine::new(Point::new(2, 8), Point::new(7, 8)).unwrap(), |
| 310 | + south.unwrap() |
| 311 | + ); |
| 312 | + } |
| 313 | + |
| 314 | + #[test] |
| 315 | + fn direction_horizontal_east() { |
| 316 | + let line = GridLine::new(Point::new(1, 3), Point::new(4, 3)).unwrap(); |
| 317 | + assert_eq!(Direction::East, line.direction()); |
| 318 | + } |
| 319 | + |
| 320 | + #[test] |
| 321 | + fn direction_horizontal_west() { |
| 322 | + let line = GridLine::new(Point::new(4, 3), Point::new(1, 3)).unwrap(); |
| 323 | + assert_eq!(Direction::West, line.direction()); |
| 324 | + } |
| 325 | + |
| 326 | + #[test] |
| 327 | + fn direction_vertical_south() { |
| 328 | + let line = GridLine::new(Point::new(2, 1), Point::new(2, 4)).unwrap(); |
| 329 | + assert_eq!(Direction::South, line.direction()); |
| 330 | + } |
| 331 | + |
| 332 | + #[test] |
| 333 | + fn direction_vertical_north() { |
| 334 | + let line = GridLine::new(Point::new(2, 4), Point::new(2, 1)).unwrap(); |
| 335 | + assert_eq!(Direction::North, line.direction()); |
| 336 | + } |
186 | 337 | } |
0 commit comments