|
| 1 | +use crate::utils::orientation::Orientation; |
1 | 2 | use crate::utils::point::Point; |
2 | 3 |
|
3 | | -#[derive(Debug, Copy, Clone)] |
| 4 | +#[derive(Debug, Copy, Clone, PartialEq)] |
4 | 5 | pub struct Line { |
5 | 6 | start: Point, |
6 | 7 | end: Point, |
@@ -46,6 +47,52 @@ impl Line { |
46 | 47 | Some(Point::new(x as isize, y as isize)) |
47 | 48 | } |
48 | 49 |
|
| 50 | + fn orientation(&self) -> Option<Orientation> { |
| 51 | + let a = self.start; |
| 52 | + let b = self.end; |
| 53 | + |
| 54 | + if a.x == b.x { |
| 55 | + return Some(Orientation::Vertical); |
| 56 | + } else if a.y == b.y { |
| 57 | + return Some(Orientation::Horizontal); |
| 58 | + } |
| 59 | + |
| 60 | + None |
| 61 | + } |
| 62 | + |
| 63 | + pub fn is_vertical(&self) -> bool { |
| 64 | + self.orientation() == Some(Orientation::Vertical) |
| 65 | + } |
| 66 | + |
| 67 | + pub fn points(&self) -> Vec<Point> { |
| 68 | + if let Some(orientation) = self.orientation() { |
| 69 | + let start = self.start; |
| 70 | + let end = self.end; |
| 71 | + |
| 72 | + return match orientation { |
| 73 | + Orientation::Horizontal => { |
| 74 | + let mut points = Vec::new(); |
| 75 | + // todo range from unordered ? |
| 76 | + for x in start.x.min(end.x)..=start.x.max(end.x) { |
| 77 | + points.push(Point::new(x, start.y)); |
| 78 | + } |
| 79 | + |
| 80 | + points |
| 81 | + } |
| 82 | + Orientation::Vertical => { |
| 83 | + let mut points = Vec::new(); |
| 84 | + for y in start.y.min(end.y)..=start.y.max(end.y) { |
| 85 | + points.push(Point::new(start.x, y)); |
| 86 | + } |
| 87 | + |
| 88 | + points |
| 89 | + } |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + unimplemented!("Only horizontal and vertical lines are supported") |
| 94 | + } |
| 95 | + |
49 | 96 | #[allow(dead_code)] |
50 | 97 | pub fn is_on(&self, point: &Point) -> bool { |
51 | 98 | let a = self.start; |
@@ -77,4 +124,52 @@ mod tests { |
77 | 124 | assert!(!line.is_on(&Point::new(15, 15))); // Outside the segment |
78 | 125 | assert!(!line.is_on(&Point::new(1, 5))); // Not on the line (not collinear) |
79 | 126 | } |
| 127 | + |
| 128 | + #[test] |
| 129 | + fn points_horizontal() { |
| 130 | + let line = Line::new(Point::new(1, 3), Point::new(4, 3)); |
| 131 | + let expected = vec![ |
| 132 | + Point::new(1, 3), |
| 133 | + Point::new(2, 3), |
| 134 | + Point::new(3, 3), |
| 135 | + Point::new(4, 3), |
| 136 | + ]; |
| 137 | + assert_eq!(line.points(), expected); |
| 138 | + } |
| 139 | + |
| 140 | + #[test] |
| 141 | + fn points_horizontal_reversed() { |
| 142 | + let line = Line::new(Point::new(4, 3), Point::new(1, 3)); |
| 143 | + let expected = vec![ |
| 144 | + Point::new(1, 3), |
| 145 | + Point::new(2, 3), |
| 146 | + Point::new(3, 3), |
| 147 | + Point::new(4, 3), |
| 148 | + ]; |
| 149 | + assert_eq!(line.points(), expected); |
| 150 | + } |
| 151 | + |
| 152 | + #[test] |
| 153 | + fn points_vertical() { |
| 154 | + let line = Line::new(Point::new(2, 1), Point::new(2, 4)); |
| 155 | + let expected = vec![ |
| 156 | + Point::new(2, 1), |
| 157 | + Point::new(2, 2), |
| 158 | + Point::new(2, 3), |
| 159 | + Point::new(2, 4), |
| 160 | + ]; |
| 161 | + assert_eq!(line.points(), expected); |
| 162 | + } |
| 163 | + |
| 164 | + #[test] |
| 165 | + fn points_vertical_reversed() { |
| 166 | + let line = Line::new(Point::new(2, 4), Point::new(2, 1)); |
| 167 | + let expected = vec![ |
| 168 | + Point::new(2, 1), |
| 169 | + Point::new(2, 2), |
| 170 | + Point::new(2, 3), |
| 171 | + Point::new(2, 4), |
| 172 | + ]; |
| 173 | + assert_eq!(line.points(), expected); |
| 174 | + } |
80 | 175 | } |
0 commit comments