use std::sync::OnceLock;
use std::time::{Duration, Instant};
pub const STEPS: u8 = 10;
pub const STEP: Duration = Duration::from_millis(90);
pub const FRAMES: [&str; STEPS as usize] = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
pub const STILL: &str = "●";
pub fn clock() -> Duration {
static START: OnceLock<Instant> = OnceLock::new();
START.get_or_init(Instant::now).elapsed()
}
pub fn step_at(time: Duration) -> u8 {
((time.as_millis() / STEP.as_millis()) % u128::from(STEPS)) as u8
}
pub fn until_next(time: Duration) -> Duration {
let step = STEP.as_millis();
let into = time.as_millis() % step;
Duration::from_millis((step - into) as u64)
}
pub fn inked(step: Option<u8>, u: f32, v: f32) -> (bool, bool) {
let (x, y) = (u * 24.0 - 12.0, v * 24.0 - 12.0);
let distance = (x * x + y * y).sqrt();
let Some(step) = step else {
return (false, distance < 6.0);
};
const RADIUS: f32 = 9.0;
const HALF: f32 = 1.6;
let ring = (distance - RADIUS).abs() < HALF;
let at = |x: f32, y: f32| (x.atan2(-y) / std::f32::consts::TAU).rem_euclid(1.0);
let start = f32::from(step) / f32::from(STEPS);
let into = (at(x, y) - start).rem_euclid(1.0);
let arc = if into <= 0.25 {
ring
} else {
let end = |turn: f32| {
let angle = turn * std::f32::consts::TAU;
let (ex, ey) = (RADIUS * angle.sin(), -RADIUS * angle.cos());
((x - ex).powi(2) + (y - ey).powi(2)).sqrt() < HALF
};
end(start) || end(start + 0.25)
};
(ring, arc)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_turn_is_ten_steps_in_under_a_second() {
assert_eq!(STEP * u32::from(STEPS), Duration::from_millis(900));
assert_eq!(step_at(Duration::ZERO), 0);
assert_eq!(step_at(Duration::from_millis(89)), 0);
assert_eq!(step_at(Duration::from_millis(90)), 1);
assert_eq!(step_at(Duration::from_millis(899)), 9);
assert_eq!(step_at(Duration::from_millis(900)), 0, "round again");
assert_eq!(until_next(Duration::from_millis(0)), STEP);
assert_eq!(
until_next(Duration::from_millis(100)),
Duration::from_millis(80)
);
assert_eq!(
until_next(Duration::from_millis(179)),
Duration::from_millis(1)
);
}
fn arc_middle(step: u8) -> f32 {
let (mut sx, mut sy, mut count) = (0.0, 0.0, 0);
for j in 0..96 {
for i in 0..96 {
let (u, v) = ((i as f32 + 0.5) / 96.0, (j as f32 + 0.5) / 96.0);
if inked(Some(step), u, v).1 {
sx += u - 0.5;
sy += v - 0.5;
count += 1;
}
}
}
assert!(count > 0);
(f32::atan2(sx, -sy) / std::f32::consts::TAU).rem_euclid(1.0)
}
#[test]
fn the_arc_is_a_quarter_of_the_ring_and_goes_round_a_tenth_a_step() {
assert!((arc_middle(0) - 0.125).abs() < 0.01, "{}", arc_middle(0));
for step in 1..STEPS {
let moved = (arc_middle(step) - arc_middle(step - 1)).rem_euclid(1.0);
assert!((moved - 0.1).abs() < 0.01, "step {step} moved {moved}");
}
let (mut ring, mut arc, mut off) = (0, 0, 0);
for j in 0..96 {
for i in 0..96 {
let (r, a) = inked(Some(3), (i as f32 + 0.5) / 96.0, (j as f32 + 0.5) / 96.0);
ring += usize::from(r);
arc += usize::from(a && r);
off += usize::from(a && !r);
}
}
let share = arc as f32 / ring as f32;
let ends = 2.0 * 1.6 / 9.0 / std::f32::consts::TAU;
assert!(share > 0.25 && share < 0.25 + ends + 0.01, "{share}");
assert!(off < arc / 50, "{off} of {arc} off the ring");
}
#[test]
fn with_motion_off_it_is_a_still_dot() {
assert_eq!(inked(None, 0.5, 0.5), (false, true));
assert_eq!(inked(None, 0.05, 0.5), (false, false));
assert_eq!(inked(None, 0.2, 0.5), (false, false), "no ring round it");
}
}