Skip to content

Instantly share code, notes, and snippets.

@raphlinus
Last active April 10, 2020 20:18
Show Gist options
  • Save raphlinus/37a0ed567a9dcc92f16bbecca802ab4a to your computer and use it in GitHub Desktop.
Save raphlinus/37a0ed567a9dcc92f16bbecca802ab4a to your computer and use it in GitHub Desktop.
Empirical measurement of cubic bez arclength bound
use kurbo::{CubicBez, ParamCurveArclen, Point};
fn randpt() -> Point {
Point::new(rand::random(), rand::random())
}
fn randbez() -> CubicBez {
CubicBez::new(randpt(), randpt(), randpt(), randpt())
}
fn main() {
let thresh = 0.1;
let mut max_ratio = 0.0;
let mut worst_bez = randbez();
for _ in 0..10_000_000 {
let b = randbez();
let true_arclen = b.arclen(1e-12);
let excess = (b.p1 - b.p0).hypot2() + (b.p2 - b.p3).hypot2();
let error = true_arclen - (b.p3 - b.p0).hypot();
let ratio = error * error / excess;
if ratio > max_ratio {
max_ratio = ratio;
worst_bez = b;
}
max_ratio = ratio.max(max_ratio);
if excess < 1.0 * thresh * thresh && error > thresh {
println!("{:?}, {}", b, ratio);
}
}
println!("max ratio: {} {:?}", max_ratio, worst_bez);
// output as SVG
let path_seg: kurbo::PathSeg = (kurbo::Affine::scale(200.0) * worst_bez).into();
let path = kurbo::BezPath::from_path_segments(std::iter::once(path_seg));
println!("{}", path.to_svg());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment