rust/integral-geometry/src/lib.rs
changeset 13943 a325ed57ebfe
parent 13942 7e7a03e85ac4
child 13947 85645992bc8a
equal deleted inserted replaced
13942:7e7a03e85ac4 13943:a325ed57ebfe
   168 
   168 
   169 impl EquidistantPoints {
   169 impl EquidistantPoints {
   170     pub fn new(vector: Point) -> Self {
   170     pub fn new(vector: Point) -> Self {
   171         Self {
   171         Self {
   172             vector,
   172             vector,
   173             iteration: 0,
   173             iteration: if vector.x == vector.y { 4 } else { 8 },
   174         }
   174         }
   175     }
   175     }
   176 }
   176 }
   177 
   177 
   178 impl Iterator for EquidistantPoints {
   178 impl Iterator for EquidistantPoints {
   179     type Item = Point;
   179     type Item = Point;
   180 
   180 
   181     fn next(&mut self) -> Option<Self::Item> {
   181     fn next(&mut self) -> Option<Self::Item> {
   182         if self.iteration < 8 {
   182         if self.iteration > 0 {
   183             self.vector.x = -self.vector.x;
   183             self.vector.x = -self.vector.x;
   184             if self.iteration & 1 == 0 {
   184             if self.iteration & 1 == 0 {
   185                 self.vector.y = -self.vector.y;
   185                 self.vector.y = -self.vector.y;
   186             }
   186             }
   187 
   187 
   188             if self.iteration == 4 {
   188             if self.iteration == 4 {
   189                 std::mem::swap(&mut self.vector.x, &mut self.vector.y);
   189                 std::mem::swap(&mut self.vector.x, &mut self.vector.y);
   190             }
   190             }
   191 
   191 
   192             self.iteration += 1;
   192             self.iteration -= 1;
   193 
   193 
   194             Some(self.vector)
   194             Some(self.vector)
   195         } else {
   195         } else {
   196             None
   196             None
   197         }
   197         }
   234             assert_eq!(a, b);
   234             assert_eq!(a, b);
   235         }
   235         }
   236     }
   236     }
   237 
   237 
   238     #[test]
   238     #[test]
   239     fn equidistant() {
   239     fn equidistant_full() {
   240         let n = EquidistantPoints::new(Point::new(1, 3));
   240         let n = EquidistantPoints::new(Point::new(1, 3));
   241         let v = get_points(&[
   241         let v = get_points(&[
   242             (-1, -3),
   242             (-1, -3),
   243             (1, -3),
   243             (1, -3),
   244             (-1, 3),
   244             (-1, 3),
   252 
   252 
   253         for (&a, b) in v.iter().zip(n) {
   253         for (&a, b) in v.iter().zip(n) {
   254             assert_eq!(a, b);
   254             assert_eq!(a, b);
   255         }
   255         }
   256     }
   256     }
   257 }
   257 
       
   258     #[test]
       
   259     fn equidistant_half() {
       
   260         let n = EquidistantPoints::new(Point::new(2, 2));
       
   261         let v = get_points(&[(-2, -2), (2, -2), (-2, 2), (2, 2), (123, 456)]);
       
   262 
       
   263         for (&a, b) in v.iter().zip(n) {
       
   264             assert_eq!(a, b);
       
   265         }
       
   266     }
       
   267 }