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.
Conceptually, a BezPath
contains zero or more subpaths. Each subpath
always begins with a MoveTo
, then has zero or more LineTo
, QuadTo
,
and CurveTo
elements, and optionally ends with a ClosePath
.
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 pop(&mut self) -> Option<PathEl>
pub fn pop(&mut self) -> Option<PathEl>
Removes the last PathEl
from the path and returns it, or None
if the path is empty.
sourcepub fn line_to<P: Into<Point>>(&mut self, p: P)
pub fn line_to<P: Into<Point>>(&mut self, p: P)
Push a “line to” element onto the path.
Will panic with a debug assert when the path is empty and there is no “move to” element on the path.
If line_to
is called immediately after close_path
then the current
subpath starts at the initial point of the previous subpath.
sourcepub fn quad_to<P: Into<Point>>(&mut self, p1: P, p2: P)
pub fn quad_to<P: Into<Point>>(&mut self, p1: P, p2: P)
Push a “quad to” element onto the path.
Will panic with a debug assert when the path is empty and there is no “move to” element on the path.
If quad_to
is called immediately after close_path
then the current
subpath starts at the initial point of the previous subpath.
sourcepub fn curve_to<P: Into<Point>>(&mut self, p1: P, p2: P, p3: P)
pub fn curve_to<P: Into<Point>>(&mut self, p1: P, p2: P, p3: P)
Push a “curve to” element onto the path.
Will panic with a debug assert when the path is empty and there is no “move to” element on the path.
If curve_to
is called immediately after close_path
then the current
subpath starts at the initial point of the previous subpath.
sourcepub fn close_path(&mut self)
pub fn close_path(&mut self)
Push a “close path” element onto the path.
Will panic with a debug assert when the path is empty and there is no “move to” element on the path.
sourcepub fn elements_mut(&mut self) -> &mut [PathEl]
pub fn elements_mut(&mut self) -> &mut [PathEl]
Get the path elements (mut version).
sourcepub fn iter(&self) -> impl Iterator<Item = PathEl> + Clone + '_
pub fn iter(&self) -> impl Iterator<Item = PathEl> + Clone + '_
Returns an iterator over the path’s elements.
sourcepub fn segments(&self) -> impl Iterator<Item = PathSeg> + Clone + '_
pub fn segments(&self) -> impl Iterator<Item = PathSeg> + Clone + '_
Iterate over the path segments.
sourcepub fn flatten(&self, tolerance: f64, callback: impl FnMut(PathEl))
👎Deprecated since 0.11.1: use the free function flatten instead
pub fn flatten(&self, tolerance: f64, callback: impl FnMut(PathEl))
Flatten the path, invoking the callback repeatedly.
See flatten
for more discussion.
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.
If you need to access all segments, segments
provides a better
API. This is intended for random access of specific elements, for clients
that require this specifically.
note: This returns the segment that ends at the provided element
index. In effect this means it is 1-indexed: since no segment ends at
the first element (which is presumed to be a MoveTo
) get_seg(0)
will
always return None
.
sourcepub fn apply_affine(&mut self, affine: Affine)
pub fn apply_affine(&mut self, affine: Affine)
Apply an affine transform to the path.
sourcepub fn control_box(&self) -> Rect
pub fn control_box(&self) -> Rect
Returns a rectangle that conservatively encloses the path.
Unlike the bounding_box
method, this uses control points directly
rather than computing tight bounds for curve elements.
sourcepub fn reverse_subpaths(&self) -> BezPath
pub fn reverse_subpaths(&self) -> BezPath
Returns a new path with the winding direction of all subpaths reversed.
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
Available on crate feature std
only.
pub fn to_svg(&self) -> String
std
only.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<'de> Deserialize<'de> for BezPath
impl<'de> Deserialize<'de> for BezPath
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl Extend<PathEl> for BezPath
impl Extend<PathEl> for BezPath
source§fn extend<I: IntoIterator<Item = PathEl>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = PathEl>>(&mut self, iter: I)
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
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 JsonSchema for BezPath
impl JsonSchema for BezPath
source§fn schema_name() -> String
fn schema_name() -> String
source§fn schema_id() -> Cow<'static, str>
fn schema_id() -> Cow<'static, str>
source§fn json_schema(gen: &mut SchemaGenerator) -> Schema
fn json_schema(gen: &mut SchemaGenerator) -> Schema
source§fn is_referenceable() -> bool
fn is_referenceable() -> bool
$ref
keyword. Read moresource§impl<'a> Mul<&'a BezPath> for TranslateScale
impl<'a> Mul<&'a BezPath> for TranslateScale
source§impl Mul<BezPath> for TranslateScale
impl Mul<BezPath> for TranslateScale
source§impl Shape for BezPath
impl Shape for BezPath
source§type PathElementsIter<'iter> = Copied<Iter<'iter, PathEl>>
type PathElementsIter<'iter> = Copied<Iter<'iter, PathEl>>
path_elements
method.source§fn path_elements(&self, _tolerance: f64) -> Self::PathElementsIter<'_>
fn path_elements(&self, _tolerance: f64) -> Self::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>
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<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§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)