pub struct BezPath(/* private fields */);
Expand description
A Bézier path.
These docs assume basic familiarity with Bézier curves; for an introduction, see Pomax’s wonderful A Primer on Bézier Curves.
This path can contain lines, quadratics (QuadBez
) and cubics
(CubicBez
), and may contain multiple subpaths.
§Elements and Segments
A Bézier path can be represented in terms of either ‘elements’ (PathEl
)
or ‘segments’ (PathSeg
). Elements map closely to how Béziers are
generally used in PostScript-style drawing APIs; they can be thought of as
instructions for drawing the path. Segments more directly describe the
path itself, with each segment being an independent line or curve.
These different representations are useful in different contexts. For tasks like drawing, elements are a natural fit, but when doing hit-testing or subdividing, we need to have access to the segments.
Internally, a BezPath
is a list of PathEl
s; as such it implements
FromIterator<PathEl>
and Extend<PathEl>
:
use kurbo::{BezPath, Rect, Shape, Vec2};
let accuracy = 0.1;
let rect = Rect::from_origin_size((0., 0.,), (10., 10.));
// these are equivalent
let path1 = rect.to_path(accuracy);
let path2: BezPath = rect.path_elements(accuracy).collect();
// extend a path with another path:
let mut path = rect.to_path(accuracy);
let shifted_rect = rect + Vec2::new(5.0, 10.0);
path.extend(shifted_rect.to_path(accuracy));
You can iterate the elements of a BezPath
with the iter
method,
and the segments with the segments
method:
use kurbo::{BezPath, Line, PathEl, PathSeg, Point, Rect, Shape};
let accuracy = 0.1;
let rect = Rect::from_origin_size((0., 0.,), (10., 10.));
// these are equivalent
let path = rect.to_path(accuracy);
let first_el = PathEl::MoveTo(Point::ZERO);
let first_seg = PathSeg::Line(Line::new((0., 0.), (10., 0.)));
assert_eq!(path.iter().next(), Some(first_el));
assert_eq!(path.segments().next(), Some(first_seg));
In addition, if you have some other type that implements
Iterator<Item=PathEl>
, you can adapt that to an iterator of segments with
the segments
free function.
§Advanced functionality
In addition to the basic API, there are several useful pieces of advanced
functionality available on BezPath
:
flatten
does Bézier flattening, converting a curve to a series of line segmentsintersect_line
computes intersections of a path with a line, useful for things like subdividing
Implementations§
Source§impl BezPath
impl BezPath
Sourcepub fn from_vec(v: Vec<PathEl>) -> BezPath
pub fn from_vec(v: Vec<PathEl>) -> BezPath
Create a path from a vector of path elements.
BezPath
also implements FromIterator<PathEl>
, so it works with collect
:
// a very contrived example:
use kurbo::{BezPath, PathEl};
let path = BezPath::new();
let as_vec: Vec<PathEl> = path.into_iter().collect();
let back_to_path: BezPath = as_vec.into_iter().collect();
Sourcepub fn close_path(&mut self)
pub fn close_path(&mut self)
Push a “close path” element onto the path.
Sourcepub fn iter(&self) -> impl Iterator<Item = PathEl>
pub fn iter(&self) -> impl Iterator<Item = PathEl>
Returns an iterator over the path’s elements.
Sourcepub fn flatten(&self, tolerance: f64, callback: impl FnMut(PathEl))
pub fn flatten(&self, tolerance: f64, callback: impl FnMut(PathEl))
Flatten the path, invoking the callback repeatedly.
Flattening is the action of approximating a curve with a succession of line segments.
The tolerance value controls the maximum distance between the curved input
segments and their polyline approximations. (In technical terms, this is the
Hausdorff distance). The algorithm attempts to bound this distance between
by tolerance
but this is not absolutely guaranteed. The appropriate value
depends on the use, but for antialiased rendering, a value of 0.25 has been
determined to give good results. The number of segments tends to scale as the
inverse square root of tolerance.
The callback will be called in order with each element of the generated path. Because the result is made of polylines, these will be straight-line path elements only, no curves.
This algorithm is based on the blog post Flattening quadratic Béziers but with some refinements. For one, there is a more careful approximation at cusps. For two, the algorithm is extended to work with cubic Béziers as well, by first subdividing into quadratics and then computing the subdivision of each quadratic. However, as a clever trick, these quadratics are subdivided fractionally, and their endpoints are not included.
TODO: write a paper explaining this in more detail.
Note: the flatten
function provides the same
functionality but works with slices and other PathEl
iterators.
Sourcepub fn get_seg(&self, ix: usize) -> Option<PathSeg>
pub fn get_seg(&self, ix: usize) -> Option<PathSeg>
Get the segment at the given element index.
The element index counts PathEl
elements, so
for example includes an initial Moveto
.
Sourcepub fn apply_affine(&mut self, affine: Affine)
pub fn apply_affine(&mut self, affine: Affine)
Apply an affine transform to the path.
Source§impl BezPath
impl BezPath
Sourcepub fn from_path_segments(segments: impl Iterator<Item = PathSeg>) -> BezPath
pub fn from_path_segments(segments: impl Iterator<Item = PathSeg>) -> BezPath
Create a BezPath with segments corresponding to the sequence of
PathSeg
s
Sourcepub fn to_svg(&self) -> String
pub fn to_svg(&self) -> String
Convert the path to an SVG path string representation.
The current implementation doesn’t take any special care to produce a short string (reducing precision, using relative movement).
Trait Implementations§
Source§impl Extend<PathEl> for BezPath
impl Extend<PathEl> for BezPath
Source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = PathEl>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = PathEl>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl FromIterator<PathEl> for BezPath
impl FromIterator<PathEl> for BezPath
Source§impl<'a> IntoIterator for &'a BezPath
Allow iteration over references to BezPath
.
impl<'a> IntoIterator for &'a BezPath
Allow iteration over references to BezPath
.
Note: the semantics are slightly different from simply iterating over the
slice, as it returns PathEl
items, rather than references.
Source§impl IntoIterator for BezPath
impl IntoIterator for BezPath
Source§impl Shape for BezPath
impl Shape for BezPath
Source§type PathElementsIter = IntoIter<PathEl>
type PathElementsIter = IntoIter<PathEl>
path_elements
method.Source§fn path_elements(&self, _tolerance: f64) -> <BezPath as Shape>::PathElementsIter
fn path_elements(&self, _tolerance: f64) -> <BezPath as Shape>::PathElementsIter
Source§fn bounding_box(&self) -> Rect
fn bounding_box(&self) -> Rect
Source§fn as_path_slice(&self) -> Option<&[PathEl]>
fn as_path_slice(&self) -> Option<&[PathEl]>
Source§fn path_segments(&self, tolerance: f64) -> Segments<Self::PathElementsIter>
fn path_segments(&self, tolerance: f64) -> Segments<Self::PathElementsIter>
Source§fn as_rounded_rect(&self) -> Option<RoundedRect>
fn as_rounded_rect(&self) -> Option<RoundedRect>
Source§impl ToGTGeometry for BezPath
impl ToGTGeometry for BezPath
impl StructuralPartialEq for BezPath
Auto Trait Implementations§
impl Freeze for BezPath
impl RefUnwindSafe for BezPath
impl Send for BezPath
impl Sync for BezPath
impl Unpin for BezPath
impl UnwindSafe for BezPath
Blanket Implementations§
Source§impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for S
impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for S
Source§fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<Swp, Dwp, T>,
fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<Swp, Dwp, T>,
Source§fn adapt_into(self) -> D
fn adapt_into(self) -> D
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, U> ConvertInto<U> for Twhere
U: ConvertFrom<T>,
impl<T, U> ConvertInto<U> for Twhere
U: ConvertFrom<T>,
Source§fn convert_into(self) -> U
fn convert_into(self) -> U
Source§fn convert_unclamped_into(self) -> U
fn convert_unclamped_into(self) -> U
Source§fn try_convert_into(self) -> Result<U, OutOfBounds<U>>
fn try_convert_into(self) -> Result<U, OutOfBounds<U>>
OutOfBounds
error is returned which contains the unclamped color. Read moreSource§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<I, F> SumWithAccumulator<F> for Iwhere
I: IntoIterator<Item = F>,
impl<I, F> SumWithAccumulator<F> for Iwhere
I: IntoIterator<Item = F>,
Source§fn sum_with_accumulator<Acc>(self) -> Fwhere
Acc: SumAccumulator<F>,
F: Zero,
fn sum_with_accumulator<Acc>(self) -> Fwhere
Acc: SumAccumulator<F>,
F: Zero,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.