dists/
spiral.rs

1use axgeom::*;
2
3#[derive(Clone)]
4pub struct Spiral {
5    point: [f32; 2],
6    rad: f32,
7    start: f32,
8    rate: f32,
9    width: f32,
10}
11
12pub struct SpiralInt(Spiral);
13impl Iterator for SpiralInt {
14    type Item = Vec2<i32>;
15    fn next(&mut self) -> Option<Vec2<i32>> {
16        self.0.next().map(|a| a.inner_as())
17    }
18}
19impl std::iter::FusedIterator for SpiralInt {}
20
21pub struct SpiralF64(Spiral);
22impl Iterator for SpiralF64 {
23    type Item = Vec2<f64>;
24    fn next(&mut self) -> Option<Vec2<f64>> {
25        self.0.next().map(|a| vec2(a.x as f64, a.y as f64))
26    }
27}
28impl std::iter::FusedIterator for SpiralF64 {}
29
30impl Spiral {
31    #[deprecated(since = "0.3.1", note = "use spiral_iter() instead")]
32    pub fn new(point: [f32; 2], circular_grow: f32, outward_grow: f32) -> Spiral {
33        Spiral {
34            point,
35            rad: 0.0,
36            start: 1.0,
37            rate: outward_grow,
38            width: circular_grow,
39        }
40    }
41    pub fn get_circular_grow(&self) -> f32 {
42        self.width
43    }
44    pub fn get_outward_grow(&self) -> f32 {
45        self.rate
46    }
47    pub fn as_isize(self) -> SpiralInt {
48        SpiralInt(self)
49    }
50    pub fn as_f64(self) -> SpiralF64 {
51        SpiralF64(self)
52    }
53}
54
55impl std::iter::FusedIterator for Spiral {}
56
57impl Iterator for Spiral {
58    type Item = Vec2<f32>;
59    fn next(&mut self) -> Option<Vec2<f32>> {
60        let length = self.start + self.rate * self.rad;
61
62        let x = self.point[0] + self.rad.cos() * length;
63        let y = self.point[1] + self.rad.sin() * length;
64
65        self.rad += self.width / length;
66
67        Some(vec2(x, y))
68    }
69}