You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.5 KiB
Rust

extern crate num_complex;
use num_complex::Complex;
use crate::shapes::*;
/// An iterator containing the shapes to display
pub struct CircleShapes {
modulo: u32,
factor: u32,
position: u32,
circle: Circle,
}
impl CircleShapes {
pub fn new(modulo: u32, factor: u32, circle: Circle) -> CircleShapes {
CircleShapes {
modulo,
factor,
position: 0,
circle,
}
}
}
impl Iterator for CircleShapes {
type Item = Shape;
fn next(&mut self) -> Option<Self::Item> {
if self.position == 0 {
self.position += 1;
Some(Shape::Circle(self.circle))
} else if self.position < self.modulo {
let center = Complex::new(self.circle.center.x, self.circle.center.y);
let pi = std::f64::consts::PI;
let radius = self.circle.radius;
let azimuth_1 = (self.position as f64) * 2. * pi / (self.modulo as f64);
let azimuth_2 = (self.position * self.factor) as f64 * 2. * pi / self.modulo as f64;
let complex_point_1 = Complex::from_polar(radius, azimuth_1) + center;
let complex_point_2 = Complex::from_polar(radius, azimuth_2) + center;
let p1 = Point { x: complex_point_1.re, y: complex_point_1.im };
let p2 = Point { x: complex_point_2.re, y: complex_point_2.im };
self.position += 1;
Some(Shape::Line(Line {p1, p2}))
} else {
None
}
}
}