Trait kurbo::Shape

source ·
pub trait Shape {
    type PathElementsIter<'iter>: Iterator<Item = PathEl> + 'iter
       where Self: 'iter;

Show 14 methods // Required methods fn path_elements(&self, tolerance: f64) -> Self::PathElementsIter<'_>; fn area(&self) -> f64; fn perimeter(&self, accuracy: f64) -> f64; fn winding(&self, pt: Point) -> i32; fn bounding_box(&self) -> Rect; // Provided methods fn to_path(&self, tolerance: f64) -> BezPath { ... } fn into_path(self, tolerance: f64) -> BezPath where Self: Sized { ... } fn path_segments( &self, tolerance: f64, ) -> Segments<Self::PathElementsIter<'_>> { ... } fn contains(&self, pt: Point) -> bool { ... } fn as_line(&self) -> Option<Line> { ... } fn as_rect(&self) -> Option<Rect> { ... } fn as_rounded_rect(&self) -> Option<RoundedRect> { ... } fn as_circle(&self) -> Option<Circle> { ... } fn as_path_slice(&self) -> Option<&[PathEl]> { ... }
}
Expand description

A generic trait for open and closed shapes.

This trait provides conversion from shapes to BezPaths, as well as general geometry functionality like computing area, bounding_boxes, and winding number.

Required Associated Types§

source

type PathElementsIter<'iter>: Iterator<Item = PathEl> + 'iter where Self: 'iter

The iterator returned by the path_elements method.

Required Methods§

source

fn path_elements(&self, tolerance: f64) -> Self::PathElementsIter<'_>

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

All shapes can be represented as Béziers, but in many situations (such as when interfacing with a platform drawing API) there are more efficient native types for specific concrete shapes. In this case, the user should exhaust the as_ methods (as_rect, as_line, etc) before converting to a BezPath, as those are likely to be more efficient.

In many cases, shapes are able to iterate their elements without allocating; however creating a BezPath object always allocates. If you need an owned BezPath you can use to_path instead.

§Tolerance

The tolerance parameter controls the accuracy of conversion of geometric primitives to Bézier curves, as curves such as circles cannot be represented exactly but only approximated. For drawing as in UI elements, a value of 0.1 is appropriate, as it is unlikely to be visible to the eye. For scientific applications, a smaller value might be appropriate. Note that in general the number of cubic Bézier segments scales as tolerance ^ (-1/6).

source

fn area(&self) -> f64

Signed area.

This method only produces meaningful results with closed shapes.

The convention for positive area is that y increases when x is positive. Thus, it is clockwise when down is increasing y (the usual convention for graphics), and anticlockwise when up is increasing y (the usual convention for math).

source

fn perimeter(&self, accuracy: f64) -> f64

Total length of perimeter.

source

fn winding(&self, pt: Point) -> i32

The winding number of a point.

This method only produces meaningful results with closed shapes.

The sign of the winding number is consistent with that of area, meaning it is +1 when the point is inside a positive area shape and -1 when it is inside a negative area shape. Of course, greater magnitude values are also possible when the shape is more complex.

source

fn bounding_box(&self) -> Rect

The smallest rectangle that encloses the shape.

Provided Methods§

source

fn to_path(&self, tolerance: f64) -> BezPath

Convert to a Bézier path.

This always allocates. It is appropriate when both the source shape and the resulting path are to be retained.

If you only need to iterate the elements (such as to convert them to drawing commands for a given 2D graphics API) you should prefer path_elements, which can avoid allocating where possible.

The tolerance parameter is the same as for path_elements.

source

fn into_path(self, tolerance: f64) -> BezPath
where Self: Sized,

Convert into a Bézier path.

This allocates in the general case, but is zero-cost if the shape is already a BezPath.

The tolerance parameter is the same as for path_elements().

source

fn path_segments(&self, tolerance: f64) -> Segments<Self::PathElementsIter<'_>>

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

The allocation behaviour and tolerance parameter are the same as for path_elements()

source

fn contains(&self, pt: Point) -> bool

Returns true if the Point is inside this shape.

This is only meaningful for closed shapes.

source

fn as_line(&self) -> Option<Line>

If the shape is a line, make it available.

source

fn as_rect(&self) -> Option<Rect>

If the shape is a rectangle, make it available.

source

fn as_rounded_rect(&self) -> Option<RoundedRect>

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

source

fn as_circle(&self) -> Option<Circle>

If the shape is a circle, make it available.

source

fn as_path_slice(&self) -> Option<&[PathEl]>

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

Note: when GAT’s land, a method like path_elements would be able to iterate through the slice with no extra allocation, without making any assumption that storage is contiguous.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<'a> Shape for &'a [PathEl]

Implements Shape for a slice of PathEl, provided that the first element of the slice is not a PathEl::ClosePath. If it is, several of these functions will panic.

If the slice starts with LineTo, QuadTo, or CurveTo, it will be treated as a MoveTo.

source§

fn area(&self) -> f64

Signed area.

source§

fn winding(&self, pt: Point) -> i32

Winding number of point.

source§

type PathElementsIter<'iter> = Copied<Iter<'a, PathEl>> where 'a: 'iter

source§

fn path_elements(&self, _tolerance: f64) -> Self::PathElementsIter<'_>

source§

fn to_path(&self, _tolerance: f64) -> BezPath

source§

fn perimeter(&self, accuracy: f64) -> f64

source§

fn bounding_box(&self) -> Rect

source§

fn as_path_slice(&self) -> Option<&[PathEl]>

source§

impl<'a, T: Shape> Shape for &'a T

Blanket implementation so impl Shape will accept owned or reference.

source§

type PathElementsIter<'iter> = <T as Shape>::PathElementsIter<'iter> where T: 'iter, 'a: 'iter

source§

fn path_elements(&self, tolerance: f64) -> Self::PathElementsIter<'_>

source§

fn to_path(&self, tolerance: f64) -> BezPath

source§

fn path_segments(&self, tolerance: f64) -> Segments<Self::PathElementsIter<'_>>

source§

fn area(&self) -> f64

source§

fn perimeter(&self, accuracy: f64) -> f64

source§

fn winding(&self, pt: Point) -> i32

source§

fn bounding_box(&self) -> Rect

source§

fn as_line(&self) -> Option<Line>

source§

fn as_rect(&self) -> Option<Rect>

source§

fn as_rounded_rect(&self) -> Option<RoundedRect>

source§

fn as_circle(&self) -> Option<Circle>

source§

fn as_path_slice(&self) -> Option<&[PathEl]>

source§

impl<const N: usize> Shape for [PathEl; N]

Implements Shape for an array of PathEl, provided that the first element of the array is not a PathEl::ClosePath. If it is, several of these functions will panic.

If the array starts with LineTo, QuadTo, or CurveTo, it will be treated as a MoveTo.

source§

fn area(&self) -> f64

Signed area.

source§

fn winding(&self, pt: Point) -> i32

Winding number of point.

source§

type PathElementsIter<'iter> = Copied<Iter<'iter, PathEl>>

source§

fn path_elements(&self, _tolerance: f64) -> Self::PathElementsIter<'_>

source§

fn to_path(&self, _tolerance: f64) -> BezPath

source§

fn perimeter(&self, accuracy: f64) -> f64

source§

fn bounding_box(&self) -> Rect

source§

fn as_path_slice(&self) -> Option<&[PathEl]>

Implementors§

source§

impl Shape for PathSeg

source§

impl Shape for Arc

source§

type PathElementsIter<'iter> = Chain<Once<PathEl>, ArcAppendIter>

source§

impl Shape for BezPath

source§

impl Shape for Circle

source§

type PathElementsIter<'iter> = CirclePathIter

source§

impl Shape for CircleSegment

source§

type PathElementsIter<'iter> = Chain<Chain<Chain<Chain<Once<PathEl>, Once<PathEl>>, ArcAppendIter>, Once<PathEl>>, ArcAppendIter>

source§

impl Shape for CubicBez

source§

impl Shape for Ellipse

source§

type PathElementsIter<'iter> = Chain<Once<PathEl>, ArcAppendIter>

source§

impl Shape for Line

source§

type PathElementsIter<'iter> = LinePathIter

source§

impl Shape for QuadBez

source§

impl Shape for Rect

source§

type PathElementsIter<'iter> = RectPathIter

source§

impl Shape for RoundedRect

source§

type PathElementsIter<'iter> = RoundedRectPathIter