pub const SMOOTHING: f64 = 0.6;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Segment {
Move(f64, f64),
Line(f64, f64),
Cubic([f64; 6]),
Close,
}
pub type Corner = (f64, f64);
pub fn rounded_rect(
(x0, y0, x1, y1): (f64, f64, f64, f64),
corners: [Corner; 4],
continuous: bool,
reverse: bool,
out: &mut impl FnMut(Segment),
) {
let (w, h) = (x1 - x0, y1 - y0);
let [tl, tr, br, bl] = corners;
struct Spec {
at: (f64, f64),
u: (f64, f64),
v: (f64, f64),
ru: f64,
rv: f64,
limit_u: f64,
limit_v: f64,
}
let limit = |len: f64, r: f64, other: f64| {
if r + other > 0.0 {
len * r / (r + other)
} else {
0.0
}
};
let specs: [Spec; 4] = if reverse {
[
Spec {
at: (x0, y0),
u: (-1.0, 0.0),
v: (0.0, 1.0),
ru: tl.0,
rv: tl.1,
limit_u: limit(w, tl.0, tr.0),
limit_v: limit(h, tl.1, bl.1),
},
Spec {
at: (x0, y1),
u: (0.0, 1.0),
v: (1.0, 0.0),
ru: bl.1,
rv: bl.0,
limit_u: limit(h, bl.1, tl.1),
limit_v: limit(w, bl.0, br.0),
},
Spec {
at: (x1, y1),
u: (1.0, 0.0),
v: (0.0, -1.0),
ru: br.0,
rv: br.1,
limit_u: limit(w, br.0, bl.0),
limit_v: limit(h, br.1, tr.1),
},
Spec {
at: (x1, y0),
u: (0.0, -1.0),
v: (-1.0, 0.0),
ru: tr.1,
rv: tr.0,
limit_u: limit(h, tr.1, br.1),
limit_v: limit(w, tr.0, tl.0),
},
]
} else {
[
Spec {
at: (x1, y0),
u: (1.0, 0.0),
v: (0.0, 1.0),
ru: tr.0,
rv: tr.1,
limit_u: limit(w, tr.0, tl.0),
limit_v: limit(h, tr.1, br.1),
},
Spec {
at: (x1, y1),
u: (0.0, 1.0),
v: (-1.0, 0.0),
ru: br.1,
rv: br.0,
limit_u: limit(h, br.1, tr.1),
limit_v: limit(w, br.0, bl.0),
},
Spec {
at: (x0, y1),
u: (-1.0, 0.0),
v: (0.0, -1.0),
ru: bl.0,
rv: bl.1,
limit_u: limit(w, bl.0, br.0),
limit_v: limit(h, bl.1, tl.1),
},
Spec {
at: (x0, y0),
u: (0.0, -1.0),
v: (1.0, 0.0),
ru: tl.1,
rv: tl.0,
limit_u: limit(h, tl.1, bl.1),
limit_v: limit(w, tl.0, tr.0),
},
]
};
for (i, s) in specs.iter().enumerate() {
let r = s.ru.max(s.rv);
let (su, sv) = if r > 0.0 {
(s.ru / r, s.rv / r)
} else {
(0.0, 0.0)
};
let pt = |a: f64, b: f64| {
(
s.at.0 + s.u.0 * a * su + s.v.0 * b * sv,
s.at.1 + s.u.1 * a * su + s.v.1 * b * sv,
)
};
if r <= 0.0 {
let (x, y) = s.at;
out(if i == 0 {
Segment::Move(x, y)
} else {
Segment::Line(x, y)
});
continue;
}
let budget = (s.limit_u / su).min(s.limit_v / sv).max(r);
let c = params(r, if continuous { SMOOTHING } else { 0.0 }, budget);
let (x, y) = pt(-c.p, 0.0);
out(if i == 0 {
Segment::Move(x, y)
} else {
Segment::Line(x, y)
});
let cubic = |c1: (f64, f64), c2: (f64, f64), e: (f64, f64)| {
let (a, b, c, d, e, f) = (c1.0, c1.1, c2.0, c2.1, e.0, e.1);
Segment::Cubic([a, b, c, d, e, f])
};
let e1 = (-c.p + c.a + c.b + c.c, c.d);
let transitions = c.a + c.b + c.c > 1e-9 * r;
if transitions {
out(cubic(
pt(-c.p + c.a, 0.0),
pt(-c.p + c.a + c.b, 0.0),
pt(e1.0, e1.1),
));
}
let theta = c.arc;
let k = 4.0 / 3.0 * (theta / 4.0).tan() * r;
let start = -std::f64::consts::FRAC_PI_4 - theta / 2.0;
let end = -std::f64::consts::FRAC_PI_4 + theta / 2.0;
let on = |phi: f64| (-r + r * phi.cos(), r + r * phi.sin());
let tangent = |phi: f64| (-phi.sin(), phi.cos());
let (p0, p3) = (on(start), on(end));
let (t0, t3) = (tangent(start), tangent(end));
out(cubic(
pt(p0.0 + k * t0.0, p0.1 + k * t0.1),
pt(p3.0 - k * t3.0, p3.1 - k * t3.1),
pt(p3.0, p3.1),
));
if transitions {
out(cubic(
pt(0.0, c.p - c.a - c.b),
pt(0.0, c.p - c.a),
pt(0.0, c.p),
));
}
}
out(Segment::Close);
}
struct Params {
p: f64,
a: f64,
b: f64,
c: f64,
d: f64,
arc: f64,
}
fn params(r: f64, smoothing: f64, budget: f64) -> Params {
use std::f64::consts::FRAC_PI_2;
let smoothing = smoothing.min(budget / r - 1.0).max(0.0);
let p = ((1.0 + smoothing) * r).min(budget);
let arc = FRAC_PI_2 * (1.0 - smoothing);
let arc_len = (arc / 2.0).sin() * r * std::f64::consts::SQRT_2;
let alpha = (FRAC_PI_2 - arc) / 2.0;
let p3_to_p4 = r * (alpha / 2.0).tan();
let beta = std::f64::consts::FRAC_PI_4 * smoothing;
let c = p3_to_p4 * beta.cos();
let d = c * beta.tan();
let b = ((p - arc_len - c - d) / 3.0).max(0.0);
Params {
p,
a: 2.0 * b,
b,
c,
d,
arc,
}
}
#[cfg(test)]
mod tests {
use super::*;
fn outline(corners: [Corner; 4], continuous: bool, reverse: bool) -> Vec<Segment> {
let mut v = Vec::new();
rounded_rect(
(0.0, 0.0, 100.0, 60.0),
corners,
continuous,
reverse,
&mut |s| v.push(s),
);
v
}
fn end(s: &Segment) -> (f64, f64) {
match *s {
Segment::Move(x, y) | Segment::Line(x, y) => (x, y),
Segment::Cubic(c) => (c[4], c[5]),
Segment::Close => unreachable!(),
}
}
fn close(a: (f64, f64), b: (f64, f64)) -> bool {
(a.0 - b.0).abs() < 1e-9 && (a.1 - b.1).abs() < 1e-9
}
#[test]
fn circular_corners_are_quarter_circles() {
let v = outline([(10.0, 10.0); 4], false, false);
assert_eq!(v.len(), 1 + 1 + 3 * 2 + 1);
assert!(close(end(&v[0]), (90.0, 0.0)));
assert!(close(end(&v[1]), (100.0, 10.0)));
let Segment::Cubic(c) = v[1] else { panic!() };
assert!((c[0] - (90.0 + 5.522_847_5)).abs() < 1e-4 && c[1].abs() < 1e-9);
assert!(close(end(&v[2]), (100.0, 50.0)));
}
#[test]
fn continuous_corners_spread_along_the_edges() {
let v = outline([(10.0, 10.0); 4], true, false);
assert!(close(end(&v[0]), (84.0, 0.0)));
assert!(close(end(&v[3]), (100.0, 16.0)));
let Segment::Cubic(c) = v[1] else { panic!() };
assert_eq!(c[1], 0.0);
let Segment::Cubic(last) = v[3] else { panic!() };
assert_eq!(last[2], 100.0);
assert_eq!(last[4], 100.0);
}
#[test]
fn pills_stay_pills() {
let continuous = outline([(30.0, 30.0); 4], true, false);
let circular = outline([(30.0, 30.0); 4], false, false);
assert_eq!(continuous.len(), circular.len());
for (a, b) in continuous.iter().zip(&circular) {
if let (Segment::Cubic(a), Segment::Cubic(b)) = (a, b) {
for (x, y) in a.iter().zip(b) {
assert!((x - y).abs() < 1e-6);
}
}
}
}
#[test]
fn reversed_outlines_visit_the_corners_the_other_way() {
let v = outline(
[(10.0, 10.0), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0)],
false,
true,
);
assert!(close(end(&v[0]), (10.0, 0.0)));
assert!(close(end(&v[1]), (0.0, 10.0)));
assert!(close(end(&v[2]), (0.0, 60.0)));
assert!(close(end(&v[3]), (100.0, 60.0)));
assert!(close(end(&v[4]), (100.0, 0.0)));
assert_eq!(v.len(), 6);
}
}