use super::{Hue, STEPS};
pub type Point = (f32, f32);
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Rib {
pub out: Point,
pub back: Point,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Shape {
Trace {
path: Vec<Point>,
width: f32,
fill: Hue,
},
Lune { ribs: Vec<Rib>, fill: Hue },
Disc {
cx: f32,
cy: f32,
radius: f32,
fill: Hue,
},
}
impl Shape {
pub fn fill(&self) -> Hue {
match self {
Self::Trace { fill, .. } | Self::Lune { fill, .. } | Self::Disc { fill, .. } => *fill,
}
}
pub fn outline(ribs: &[Rib]) -> Vec<Point> {
let mut points: Vec<Point> = ribs.iter().map(|rib| rib.out).collect();
points.extend(ribs.iter().rev().map(|rib| rib.back));
points
}
}
pub(super) fn arc(from: Point, to: Point, bulge: f32) -> Vec<Point> {
let (side, sag) = (bulge.signum(), bulge.abs());
let (dx, dy) = (to.0 - from.0, to.1 - from.1);
let span = dx.hypot(dy);
let half = span / 2.0;
let radius = half.mul_add(half, sag * sag) / (2.0 * sag);
let (nx, ny) = (-dy / span * side, dx / span * side);
let centre = (
(from.0 + to.0).mul_add(0.5, -nx * (radius - sag)),
(from.1 + to.1).mul_add(0.5, -ny * (radius - sag)),
);
let start = (from.1 - centre.1).atan2(from.0 - centre.0);
let sweep = 2.0 * half.atan2(radius - sag);
(0..=STEPS)
.map(|step| {
let along = f32::from(step) / f32::from(STEPS);
let angle = side.mul_add(-(sweep * along), start);
(
radius.mul_add(angle.cos(), centre.0),
radius.mul_add(angle.sin(), centre.1),
)
})
.collect()
}
pub(super) fn lune(from: Point, to: Point, out: f32, back: f32, fill: Hue) -> Shape {
Shape::Lune {
ribs: arc(from, to, out)
.into_iter()
.zip(arc(from, to, back))
.map(|(out, back)| Rib { out, back })
.collect(),
fill,
}
}