Expand description
2D geometry, with a focus on curves.
The kurbo library contains data structures and algorithms for curves and vector paths. It was designed to serve the needs of 2D graphics applications, but it is intended to be general enough to be useful for other applications. It can be used as “vocabulary types” for representing curves and paths, and also contains a number of computational geometry methods.
§Examples
Basic UI-style geometry:
use kurbo::{Insets, Point, Rect, Size, Vec2};
let pt = Point::new(10.0, 10.0);
let vector = Vec2::new(5.0, -5.0);
let pt2 = pt + vector;
assert_eq!(pt2, Point::new(15.0, 5.0));
let rect = Rect::from_points(pt, pt2);
assert_eq!(rect, Rect::from_origin_size((10.0, 5.0), (5.0, 5.0)));
let insets = Insets::uniform(1.0);
let inset_rect = rect - insets;
assert_eq!(inset_rect.size(), Size::new(3.0, 3.0));Finding the closest position on a Shape’s perimeter to a Point:
use kurbo::{Circle, ParamCurve, ParamCurveNearest, Point, Shape};
const DESIRED_ACCURACY: f64 = 0.1;
/// Given a shape and a point, returns the closest position on the shape's
/// perimeter, or `None` if the shape is malformed.
fn closest_perimeter_point(shape: impl Shape, pt: Point) -> Option<Point> {
let mut best: Option<(Point, f64)> = None;
for segment in shape.path_segments(DESIRED_ACCURACY) {
let nearest = segment.nearest(pt, DESIRED_ACCURACY);
if best.map(|(_, best_d)| nearest.distance_sq < best_d).unwrap_or(true) {
best = Some((segment.eval(nearest.t), nearest.distance_sq))
}
}
best.map(|(point, _)| point)
}
let circle = Circle::new((5.0, 5.0), 5.0);
let hit_point = Point::new(5.0, -2.0);
let expectation = Point::new(5.0, 0.0);
let hit = closest_perimeter_point(circle, hit_point).unwrap();
assert!(hit.distance(expectation) <= DESIRED_ACCURACY);§Feature Flags
The following crate feature flags are available:
std(enabled by default): Get floating point functions from the standard library (likely using your target’s libc).libm: Use floating point implementations from libm. This is useful forno_stdenvironments. However, note that thelibmcrate is not as efficient as the standard library.mint: EnableFrom/Intoconversion of Kurbo and mint types, enabling interoperability with other graphics libraries.euclid: EnableFrom/Intoconversion of Kurbo and euclid types. Note that if you’re using both Kurbo and euclid at the same time, you must also enable one of euclid’sstdorlibmfeatures.serde: Implementserde::Deserializeandserde::Serializeon various types.schemars: Add best-effort support for using Kurbo types in JSON schemas using schemars.
At least one of std and libm is required; std overrides libm.
Note that Kurbo does require that an allocator is available (i.e. it uses alloc).
Modules§
- common
- Common mathematical operations
- offset
- Computation of offset curves of cubic Béziers.
- simplify
- Simplification of a Bézier path.
Structs§
- Affine
- A 2D affine transform.
- Arc
- A single elliptical arc segment.
- BezPath
- A Bézier path.
- Circle
- A circle.
- Circle
Segment - A segment of a circle.
- Const
Point - A trivial “curve” that is just a constant.
- Cubic
Bez - A single cubic Bézier segment.
- Cubic
BezIter - An iterator for cubic beziers.
- Curve
FitSample - A sample point of a curve for fitting.
- Ellipse
- An ellipse.
- Insets
- Insets from the edges of a rectangle.
- Line
- A single line.
- Line
Intersection - An intersection of a
Lineand aPathSeg. - MinDistance
- The minimum distance between two Bézier curves.
- Moments
- A collect of moment integrals for a path, used for computing shape properties like area, centroid, and inertia.
- Nearest
- The nearest position on a curve to some point.
- Path
SegIter - An iterator for path segments.
- Point
- A 2D point.
- QuadBez
- A single quadratic Bézier segment.
- Quad
BezIter - An iterator for quadratic beziers.
- Quad
Spline - A quadratic Bézier spline in B-spline format.
- Rect
- A rectangle.
- Rounded
Rect - A rectangle with rounded corners.
- Rounded
Rect Radii - Radii for each corner of a rounded rectangle.
- Segments
- An iterator that transforms path elements to path segments.
- Size
- A 2D size.
- Stroke
- The visual style of a stroke.
- Stroke
Ctx - A structure that is used for creating strokes.
- Stroke
Opts - Options for path stroking.
- SvgArc
- A single SVG arc segment.
- Translate
Scale - A transformation consisting of a uniform scaling followed by a translation.
- Triangle
- Triangle
- Vec2
- A 2D vector.
Enums§
- Axis
- An axis in the plane.
- Cap
- Defines the shape to be drawn at the ends of a stroke.
- Cusp
Type - Classification result for cusp detection.
- Join
- Defines the connection between two segments of a stroke.
- PathEl
- The element of a Bézier path.
- PathSeg
- A segment of a Bézier path.
- Stroke
OptLevel - Optimization level for computing stroke outlines.
- SvgParse
Error - An error which can be returned when parsing an SVG.
Constants§
- DEFAULT_
ACCURACY - A default value for methods that take an ‘accuracy’ argument.
- MAX_
EXTREMA - The maximum number of extrema that can be reported in the
ParamCurveExtrematrait.
Traits§
- Param
Curve - A curve parameterized by a scalar.
- Param
Curve Arclen - A parameterized curve that can have its arc length measured.
- Param
Curve Area - A parameterized curve that can have its signed area measured.
- Param
Curve Curvature - A parameterized curve that reports its curvature.
- Param
Curve Deriv - A differentiable parameterized curve.
- Param
Curve Extrema - A parameterized curve that reports its extrema.
- Param
Curve Fit - The source curve for curve fitting.
- Param
Curve Moments - A trait for types that can provide moment integrals for a path using Green’s theorem.
- Param
Curve Nearest - A parameterized curve that reports the nearest point.
- Shape
- A generic trait for open and closed shapes.
Functions§
- cubics_
to_ quadratic_ splines - Convert multiple cubic Bézier curves to quadratic splines.
- dash
- Create a new dashing iterator.
- fit_
to_ bezpath - Generate a Bézier path that fits the source curve.
- fit_
to_ bezpath_ opt - Generate a highly optimized Bézier path that fits the source curve.
- fit_
to_ cubic - Fit a single cubic to a range of the source curve.
- flatten
- Flatten the path, invoking the callback repeatedly.
- segments
- Transform an iterator over path elements into one over path segments.
- stroke
- Expand a stroke into a fill.
- stroke_
with - Expand a stroke into a fill.
Type Aliases§
- Dashes
- Collection of values representing lengths in a dash pattern.