rust/integral-geometry/src/lib.rs
changeset 15182 decb2f1c682b
parent 15025 dc4a12a84c92
child 15214 58a0f2a6527b
equal deleted inserted replaced
15181:d13a38548112 15182:decb2f1c682b
   790         }
   790         }
   791     }
   791     }
   792 }
   792 }
   793 
   793 
   794 pub struct EquidistantPoints {
   794 pub struct EquidistantPoints {
   795     vector: Point,
   795     vector: Vec<Point>,
   796     iteration: u8,
       
   797 }
   796 }
   798 
   797 
   799 impl EquidistantPoints {
   798 impl EquidistantPoints {
   800     pub fn new(vector: Point) -> Self {
   799     pub fn new(vector: Point) -> Self {
   801         Self {
   800         Self {
   802             vector,
   801             vector: if vector.x == vector.y {
   803             iteration: if vector.x == vector.y { 4 } else { 8 },
   802                 vec![
   804         }
   803                     Point::new(vector.x, vector.x),
   805     }
   804                     Point::new(vector.x, -vector.x),
   806 }
   805                     Point::new(-vector.x, -vector.x),
   807 
   806                     Point::new(-vector.x, vector.x),
   808 impl Iterator for EquidistantPoints {
   807                 ]
       
   808             } else {
       
   809                 vec![
       
   810                     Point::new(vector.x, vector.y),
       
   811                     Point::new(vector.x, -vector.y),
       
   812                     Point::new(-vector.x, -vector.y),
       
   813                     Point::new(-vector.x, vector.y),
       
   814                     Point::new(vector.y, vector.x),
       
   815                     Point::new(vector.y, -vector.x),
       
   816                     Point::new(-vector.y, -vector.x),
       
   817                     Point::new(-vector.y, vector.x),
       
   818                 ]
       
   819             },
       
   820         }
       
   821     }
       
   822 }
       
   823 
       
   824 impl IntoIterator for EquidistantPoints {
   809     type Item = Point;
   825     type Item = Point;
   810 
   826     type IntoIter = std::vec::IntoIter<Point>;
   811     fn next(&mut self) -> Option<Self::Item> {
   827 
   812         if self.iteration > 0 {
   828     fn into_iter(self) -> Self::IntoIter {
   813             self.vector.x = -self.vector.x;
   829         self.vector.into_iter()
   814             if self.iteration & 1 == 0 {
       
   815                 self.vector.y = -self.vector.y;
       
   816             }
       
   817 
       
   818             if self.iteration == 4 {
       
   819                 std::mem::swap(&mut self.vector.x, &mut self.vector.y);
       
   820             }
       
   821 
       
   822             self.iteration -= 1;
       
   823 
       
   824             Some(self.vector)
       
   825         } else {
       
   826             None
       
   827         }
       
   828     }
   830     }
   829 }
   831 }
   830 
   832 
   831 pub struct BezierCurveSegments {
   833 pub struct BezierCurveSegments {
   832     segment: Line,
   834     segment: Line,
   919         assert_eq!(line, v);
   921         assert_eq!(line, v);
   920     }
   922     }
   921 
   923 
   922     #[test]
   924     #[test]
   923     fn equidistant_full() {
   925     fn equidistant_full() {
   924         let n: Vec<Point> = EquidistantPoints::new(Point::new(1, 3)).collect();
   926         let n: Vec<Point> = EquidistantPoints::new(Point::new(1, 3))
       
   927             .into_iter()
       
   928             .collect();
   925         let v = get_points(&[
   929         let v = get_points(&[
       
   930             (1, 3),
       
   931             (1, -3),
   926             (-1, -3),
   932             (-1, -3),
   927             (1, -3),
       
   928             (-1, 3),
   933             (-1, 3),
   929             (1, 3),
   934             (3, 1),
       
   935             (3, -1),
   930             (-3, -1),
   936             (-3, -1),
   931             (3, -1),
       
   932             (-3, 1),
   937             (-3, 1),
   933             (3, 1),
       
   934         ]);
   938         ]);
   935 
   939 
   936         assert_eq!(n, v);
   940         assert_eq!(n, v);
   937     }
   941     }
   938 
   942 
   939     #[test]
   943     #[test]
   940     fn equidistant_half() {
   944     fn equidistant_half() {
   941         let n: Vec<Point> = EquidistantPoints::new(Point::new(2, 2)).collect();
   945         let n: Vec<Point> = EquidistantPoints::new(Point::new(2, 2))
   942         let v = get_points(&[(-2, -2), (2, -2), (-2, 2), (2, 2)]);
   946             .into_iter()
       
   947             .collect();
       
   948         let v = get_points(&[(2, 2), (2, -2), (-2, -2), (-2, 2)]);
   943 
   949 
   944         assert_eq!(n, v);
   950         assert_eq!(n, v);
   945     }
   951     }
   946 
   952 
   947     #[test]
   953     #[test]