3 unstable releases

0.2.0 Sep 26, 2024
0.1.1 Sep 20, 2024
0.1.0 Sep 20, 2024

#393 in Machine learning

37 downloads per month

MIT/Apache

33KB
772 lines

A library for computing statistics on paths

This library provides methods for computing statistics on paths, such as the area, center of mass, variance, covariance, correlation, and slant.

It implements two mechanisms for computing statistics, one based on Green's theorem, and the other using the control only. The library is a straight port of the Python library fontTools.pens.statisticsPen.

While it is expected to be used on kurbo::BezPath objects, it can be used on any object that can iterate over kurbo::PathEl objects.

Example

use kurbo::BezPath;
use greencurves::{ComputeGreenStatistics, CurveStatistics};

let b = BezPath::from_svg("M300 -10Q229 -10 173.5 19.0Q118 48 86.5 109.0Q55 170 55 265Q55 364 88.0 426.0Q121 488 177.5 517.0Q234 546 306 546Q347 546 385.0 537.5Q423 529 447 517L420 444Q396 453 364.0 461.0Q332 469 304 469Q146 469 146 266Q146 169 184.5 117.5Q223 66 299 66Q343 66 376.5 75.0Q410 84 438 97V19Q411 5 378.5 -2.5Q346 -10 300 -10Z").expect("Failed to parse path");
let stats = b.green_statistics();

use approx::assert_relative_eq;

assert_relative_eq!(stats.center_of_mass().x, 214.4132814627106, epsilon = f64::EPSILON);
assert_relative_eq!(stats.center_of_mass().y, 267.5738980976807, epsilon = f64::EPSILON);
assert_relative_eq!(stats.variance().x, 11909.914244819694, epsilon = f64::EPSILON);
assert_relative_eq!(stats.variance().y, 34930.81282036622, epsilon = f64::EPSILON);
assert_relative_eq!(stats.covariance(), 123.24645984253584, epsilon = f64::EPSILON);
assert_relative_eq!(stats.correlation(), 0.006042487913362581, epsilon = f64::EPSILON);
assert_relative_eq!(stats.slant(), 0.0035283020889418774, epsilon = f64::EPSILON);

Dependencies

~1MB
~21K SLoC