1use std::sync::OnceLock;
11use std::time::{Duration, Instant};
12
13pub const STEPS: u8 = 10;
15pub const STEP: Duration = Duration::from_millis(90);
17pub const FRAMES: [&str; STEPS as usize] = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
19pub const STILL: &str = "●";
21
22pub fn clock() -> Duration {
25 static START: OnceLock<Instant> = OnceLock::new();
26 START.get_or_init(Instant::now).elapsed()
27}
28
29pub fn step_at(time: Duration) -> u8 {
31 ((time.as_millis() / STEP.as_millis()) % u128::from(STEPS)) as u8
32}
33
34pub fn until_next(time: Duration) -> Duration {
37 let step = STEP.as_millis();
38 let into = time.as_millis() % step;
39 Duration::from_millis((step - into) as u64)
40}
41
42pub fn inked(step: Option<u8>, u: f32, v: f32) -> (bool, bool) {
48 let (x, y) = (u * 24.0 - 12.0, v * 24.0 - 12.0);
49 let distance = (x * x + y * y).sqrt();
50 let Some(step) = step else {
51 return (false, distance < 6.0);
52 };
53 const RADIUS: f32 = 9.0;
54 const HALF: f32 = 1.6;
55 let ring = (distance - RADIUS).abs() < HALF;
56 let at = |x: f32, y: f32| (x.atan2(-y) / std::f32::consts::TAU).rem_euclid(1.0);
58 let start = f32::from(step) / f32::from(STEPS);
59 let into = (at(x, y) - start).rem_euclid(1.0);
60 let arc = if into <= 0.25 {
61 ring
62 } else {
63 let end = |turn: f32| {
65 let angle = turn * std::f32::consts::TAU;
66 let (ex, ey) = (RADIUS * angle.sin(), -RADIUS * angle.cos());
67 ((x - ex).powi(2) + (y - ey).powi(2)).sqrt() < HALF
68 };
69 end(start) || end(start + 0.25)
70 };
71 (ring, arc)
72}
73
74#[cfg(test)]
75mod tests {
76 use super::*;
77
78 #[test]
79 fn a_turn_is_ten_steps_in_under_a_second() {
80 assert_eq!(STEP * u32::from(STEPS), Duration::from_millis(900));
81 assert_eq!(step_at(Duration::ZERO), 0);
82 assert_eq!(step_at(Duration::from_millis(89)), 0);
83 assert_eq!(step_at(Duration::from_millis(90)), 1);
84 assert_eq!(step_at(Duration::from_millis(899)), 9);
85 assert_eq!(step_at(Duration::from_millis(900)), 0, "round again");
86 assert_eq!(until_next(Duration::from_millis(0)), STEP);
87 assert_eq!(
88 until_next(Duration::from_millis(100)),
89 Duration::from_millis(80)
90 );
91 assert_eq!(
92 until_next(Duration::from_millis(179)),
93 Duration::from_millis(1)
94 );
95 }
96
97 fn arc_middle(step: u8) -> f32 {
99 let (mut sx, mut sy, mut count) = (0.0, 0.0, 0);
100 for j in 0..96 {
101 for i in 0..96 {
102 let (u, v) = ((i as f32 + 0.5) / 96.0, (j as f32 + 0.5) / 96.0);
103 if inked(Some(step), u, v).1 {
104 sx += u - 0.5;
105 sy += v - 0.5;
106 count += 1;
107 }
108 }
109 }
110 assert!(count > 0);
111 (f32::atan2(sx, -sy) / std::f32::consts::TAU).rem_euclid(1.0)
112 }
113
114 #[test]
115 fn the_arc_is_a_quarter_of_the_ring_and_goes_round_a_tenth_a_step() {
116 assert!((arc_middle(0) - 0.125).abs() < 0.01, "{}", arc_middle(0));
118 for step in 1..STEPS {
119 let moved = (arc_middle(step) - arc_middle(step - 1)).rem_euclid(1.0);
120 assert!((moved - 0.1).abs() < 0.01, "step {step} moved {moved}");
121 }
122 let (mut ring, mut arc, mut off) = (0, 0, 0);
125 for j in 0..96 {
126 for i in 0..96 {
127 let (r, a) = inked(Some(3), (i as f32 + 0.5) / 96.0, (j as f32 + 0.5) / 96.0);
128 ring += usize::from(r);
129 arc += usize::from(a && r);
130 off += usize::from(a && !r);
131 }
132 }
133 let share = arc as f32 / ring as f32;
134 let ends = 2.0 * 1.6 / 9.0 / std::f32::consts::TAU;
135 assert!(share > 0.25 && share < 0.25 + ends + 0.01, "{share}");
136 assert!(off < arc / 50, "{off} of {arc} off the ring");
137 }
138
139 #[test]
140 fn with_motion_off_it_is_a_still_dot() {
141 assert_eq!(inked(None, 0.5, 0.5), (false, true));
142 assert_eq!(inked(None, 0.05, 0.5), (false, false));
143 assert_eq!(inked(None, 0.2, 0.5), (false, false), "no ring round it");
144 }
145}