Struct kurbo::BezPath[][src]

pub struct BezPath(_);
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 PathEls; 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 segments
  • intersect_line computes intersections of a path with a line, useful for things like subdividing

Implementations

Create a new path.

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();

Push a generic path element onto the path.

Push a “move to” element onto the path.

Push a “line to” element onto the path.

Push a “quad to” element onto the path.

Push a “curve to” element onto the path.

Push a “close path” element onto the path.

Get the path elements.

Returns an iterator over the path’s elements.

Iterate over the path segments.

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.

Get the segment at the given element index.

The element index counts PathEl elements, so for example includes an initial Moveto.

Returns true if the path contains no segments.

Apply an affine transform to the path.

Is this path finite?

Is this path NaN?

Create a BezPath with segments corresponding to the sequence of PathSegs

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).

Write the SVG representation of this path to the provided buffer.

Try to parse a bezier path from an SVG path element.

This is implemented on a best-effort basis, intended for cases where the user controls the source of paths, and is not intended as a replacement for a general, robust SVG parser.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Deserialize this value from the given Serde deserializer. Read more

Extends a collection with the contents of an iterator. Read more

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Creates a value from an iterator. Read more

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.

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Serialize this value into the given Serde serializer. Read more

Signed area.

Winding number of point.

The iterator returned by the path_elements method. Read more

Returns an iterator over this shape expressed as PathEls; that is, as Bézier path elements. Read more

Convert to a Bézier path. Read more

Convert into a Bézier path. Read more

Total length of perimeter.

The smallest rectangle that encloses the shape.

If the shape is stored as a slice of path elements, make that available. Read more

Returns an iterator over this shape expressed as Bézier path segments (PathSegs). Read more

Returns true if the Point is inside this shape. Read more

If the shape is a line, make it available.

If the shape is a rectangle, make it available.

If the shape is a rounded rectangle, make it available.

If the shape is a circle, make it available.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.