159 None |
159 None |
160 } |
160 } |
161 } |
161 } |
162 } |
162 } |
163 |
163 |
|
164 pub struct EquidistantPoints { |
|
165 vector: Point, |
|
166 iteration: u8, |
|
167 } |
|
168 |
|
169 impl EquidistantPoints { |
|
170 pub fn new(vector: Point) -> Self { |
|
171 Self { |
|
172 vector, |
|
173 iteration: 0, |
|
174 } |
|
175 } |
|
176 } |
|
177 |
|
178 impl Iterator for EquidistantPoints { |
|
179 type Item = Point; |
|
180 |
|
181 fn next(&mut self) -> Option<Self::Item> { |
|
182 if self.iteration < 8 { |
|
183 self.vector.x = -self.vector.x; |
|
184 if self.iteration & 1 == 0 { |
|
185 self.vector.y = -self.vector.y; |
|
186 } |
|
187 |
|
188 if self.iteration == 4 { |
|
189 std::mem::swap(&mut self.vector.x, &mut self.vector.y); |
|
190 } |
|
191 |
|
192 self.iteration += 1; |
|
193 |
|
194 Some(self.vector) |
|
195 } else { |
|
196 None |
|
197 } |
|
198 } |
|
199 } |
|
200 |
164 #[cfg(test)] |
201 #[cfg(test)] |
165 mod tests { |
202 mod tests { |
166 use super::*; |
203 use super::*; |
167 |
204 |
168 fn get_points(coords: &[(i32, i32)]) -> Vec<Point> { |
205 fn get_points(coords: &[(i32, i32)]) -> Vec<Point> { |
169 coords.iter().map(|(x, y)| Point::new(*x, *y)).collect() |
206 coords.iter().map(|(x, y)| Point::new(*x, *y)).collect() |
170 } |
207 } |
171 |
208 |
172 #[test] |
209 #[test] |
173 fn basic() { |
210 fn line_basic() { |
174 let line = LinePoints::new(Point::new(0, 0), Point::new(3, 3)); |
211 let line = LinePoints::new(Point::new(0, 0), Point::new(3, 3)); |
175 let v = get_points(&[(0, 0), (1, 1), (2, 2), (3, 3), (123, 456)]); |
212 let v = get_points(&[(0, 0), (1, 1), (2, 2), (3, 3), (123, 456)]); |
176 |
213 |
177 for (&a, b) in v.iter().zip(line) { |
214 for (&a, b) in v.iter().zip(line) { |
178 assert_eq!(a, b); |
215 assert_eq!(a, b); |
179 } |
216 } |
180 } |
217 } |
181 |
218 |
182 #[test] |
219 #[test] |
183 fn skewed() { |
220 fn line_skewed() { |
184 let line = LinePoints::new(Point::new(0, 0), Point::new(5, -7)); |
221 let line = LinePoints::new(Point::new(0, 0), Point::new(5, -7)); |
185 let v = get_points(&[ |
222 let v = get_points(&[ |
186 (0, 0), |
223 (0, 0), |
187 (1, -1), |
224 (1, -1), |
188 (2, -2), |
225 (2, -2), |