pub struct Bezier {
pub start: DVec2,
pub end: DVec2,
pub handles: BezierHandles,
}
Expand description
Representation of a bezier curve with 2D points.
Fields§
§start: DVec2
Start point of the bezier curve.
end: DVec2
Start point of the bezier curve.
handles: BezierHandles
Handles of the bezier curve.
Implementations§
source§impl Bezier
impl Bezier
Functionality relating to core Bezier
operations, such as constructors and abs_diff_eq
.
sourcepub fn from_linear_coordinates(x1: f64, y1: f64, x2: f64, y2: f64) -> Self
pub fn from_linear_coordinates(x1: f64, y1: f64, x2: f64, y2: f64) -> Self
Create a linear bezier using the provided coordinates as the start and end points.
sourcepub fn from_linear_dvec2(p1: DVec2, p2: DVec2) -> Self
pub fn from_linear_dvec2(p1: DVec2, p2: DVec2) -> Self
Create a linear bezier using the provided DVec2s as the start and end points.
sourcepub fn from_quadratic_coordinates(
x1: f64,
y1: f64,
x2: f64,
y2: f64,
x3: f64,
y3: f64
) -> Self
pub fn from_quadratic_coordinates( x1: f64, y1: f64, x2: f64, y2: f64, x3: f64, y3: f64 ) -> Self
Create a quadratic bezier using the provided coordinates as the start, handle, and end points.
sourcepub fn from_quadratic_dvec2(p1: DVec2, p2: DVec2, p3: DVec2) -> Self
pub fn from_quadratic_dvec2(p1: DVec2, p2: DVec2, p3: DVec2) -> Self
Create a quadratic bezier using the provided DVec2s as the start, handle, and end points.
sourcepub fn from_cubic_coordinates(
x1: f64,
y1: f64,
x2: f64,
y2: f64,
x3: f64,
y3: f64,
x4: f64,
y4: f64
) -> Self
pub fn from_cubic_coordinates( x1: f64, y1: f64, x2: f64, y2: f64, x3: f64, y3: f64, x4: f64, y4: f64 ) -> Self
Create a cubic bezier using the provided coordinates as the start, handles, and end points.
sourcepub fn from_cubic_dvec2(p1: DVec2, p2: DVec2, p3: DVec2, p4: DVec2) -> Self
pub fn from_cubic_dvec2(p1: DVec2, p2: DVec2, p3: DVec2, p4: DVec2) -> Self
Create a cubic bezier using the provided DVec2s as the start, handles, and end points.
sourcepub fn quadratic_through_points(
start: DVec2,
point_on_curve: DVec2,
end: DVec2,
t: Option<f64>
) -> Self
pub fn quadratic_through_points( start: DVec2, point_on_curve: DVec2, end: DVec2, t: Option<f64> ) -> Self
Create a quadratic bezier curve that goes through 3 points, where the middle point will be at the corresponding position t
on the curve.
t
- A representation of how far along the curve the provided point should occur at. The default value is 0.5. Note that whent = 0
ort = 1
, the expectation is that thepoint_on_curve
should be equal tostart
andend
respectively. In these cases, if the provided values are not equal, this function will use thepoint_on_curve
as thestart
/end
instead.
sourcepub fn cubic_through_points(
start: DVec2,
point_on_curve: DVec2,
end: DVec2,
t: Option<f64>,
midpoint_separation: Option<f64>
) -> Self
pub fn cubic_through_points( start: DVec2, point_on_curve: DVec2, end: DVec2, t: Option<f64>, midpoint_separation: Option<f64> ) -> Self
Create a cubic bezier curve that goes through 3 points, where the middle point will be at the corresponding position t
on the curve.
t
- A representation of how far along the curve the provided point should occur at. The default value is 0.5. Note that whent = 0
ort = 1
, the expectation is that thepoint_on_curve
should be equal tostart
andend
respectively. In these cases, if the provided values are not equal, this function will use thepoint_on_curve
as thestart
/end
instead.midpoint_separation
- A representation of how wide the resulting curve will be aroundt
on the curve. This parameter designates the distance between thee1
ande2
defined in the projection identity section of Pomax’s bezier curve primer. It is an optional parameter and the default value is the distance between the pointsB
andC
defined in the primer.
sourcepub fn svg_curve_argument(&self) -> String
pub fn svg_curve_argument(&self) -> String
Return the string argument used to create a curve in an SVG path
, excluding the start point.
sourcepub fn write_curve_argument(&self, svg: &mut String) -> Result
pub fn write_curve_argument(&self, svg: &mut String) -> Result
Write the curve argument to the string
sourcepub fn curve_to_svg(&self, svg: &mut String, attributes: String)
pub fn curve_to_svg(&self, svg: &mut String, attributes: String)
Appends to the svg
mutable string with an SVG shape representation of the curve.
sourcepub fn handle_lines_to_svg(&self, svg: &mut String, attributes: String)
pub fn handle_lines_to_svg(&self, svg: &mut String, attributes: String)
Appends to the svg
mutable string with an SVG shape representation of the handle lines.
sourcepub fn anchors_to_svg(&self, svg: &mut String, attributes: String)
pub fn anchors_to_svg(&self, svg: &mut String, attributes: String)
Appends to the svg
mutable string with an SVG shape representation of the anchors.
sourcepub fn handles_to_svg(&self, svg: &mut String, attributes: String)
pub fn handles_to_svg(&self, svg: &mut String, attributes: String)
Appends to the svg
mutable string with an SVG shape representation of the handles.
sourcepub fn to_svg(
&self,
svg: &mut String,
curve_attributes: String,
anchor_attributes: String,
handle_attributes: String,
handle_line_attributes: String
)
pub fn to_svg( &self, svg: &mut String, curve_attributes: String, anchor_attributes: String, handle_attributes: String, handle_line_attributes: String )
Appends to the svg
mutable string with an SVG shape representation that includes the curve, the handle lines, the anchors, and the handles.
sourcepub fn abs_diff_eq(&self, other: &Bezier, max_abs_diff: f64) -> bool
pub fn abs_diff_eq(&self, other: &Bezier, max_abs_diff: f64) -> bool
Returns true if the corresponding points of the two Bezier
s are within the provided absolute value difference from each other.
The points considered includes the start, end, and any relevant handles.
source§impl Bezier
impl Bezier
Functionality relating to looking up properties of the Bezier
or points along the Bezier
.
sourcepub fn euclidean_to_parametric(&self, ratio: f64, error: f64) -> f64
pub fn euclidean_to_parametric(&self, ratio: f64, error: f64) -> f64
Convert a euclidean distance ratio along the Bezier
curve to a parametric t
-value.
sourcepub fn euclidean_to_parametric_with_total_length(
&self,
euclidean_t: f64,
error: f64,
total_length: f64
) -> f64
pub fn euclidean_to_parametric_with_total_length( &self, euclidean_t: f64, error: f64, total_length: f64 ) -> f64
Convert a euclidean distance ratio along the Bezier
curve to a parametric t
-value.
For performance reasons, this version of the [euclidean_to_parametric
] function allows the caller to
provide the total length of the curve so it doesn’t have to be calculated every time the function is called.
sourcepub fn evaluate(&self, t: TValue) -> DVec2
pub fn evaluate(&self, t: TValue) -> DVec2
Calculate the coordinates of the point t
along the curve.
Expects t
to be within the inclusive range [0, 1]
.
sourcepub fn compute_lookup_table(
&self,
steps: Option<usize>,
tvalue_type: Option<TValueType>
) -> Vec<DVec2>
pub fn compute_lookup_table( &self, steps: Option<usize>, tvalue_type: Option<TValueType> ) -> Vec<DVec2>
Return a selection of equidistant points on the bezier curve.
If no value is provided for steps
, then the function will default steps
to be 10.
sourcepub fn length(&self, num_subdivisions: Option<usize>) -> f64
pub fn length(&self, num_subdivisions: Option<usize>) -> f64
Return an approximation of the length of the bezier curve.
num_subdivisions
- Number of subdivisions used to approximate the curve. The default value is 1000.
sourcepub fn project(&self, point: DVec2, options: Option<ProjectionOptions>) -> f64
pub fn project(&self, point: DVec2, options: Option<ProjectionOptions>) -> f64
Returns the parametric t
-value that corresponds to the closest point on the curve to the provided point.
Uses a searching algorithm akin to binary search that can be customized using the optional ProjectionOptions struct.
source§impl Bezier
impl Bezier
Functionality for the getters and setters of the various points in a Bezier
sourcepub fn set_handle_start(&mut self, h1: DVec2)
pub fn set_handle_start(&mut self, h1: DVec2)
Set the coordinates of the first handle point. This represents the only handle in a quadratic segment. If used on a linear segment, it will be changed to a quadratic.
sourcepub fn set_handle_end(&mut self, h2: DVec2)
pub fn set_handle_end(&mut self, h2: DVec2)
Set the coordinates of the second handle point. This will convert both linear and quadratic segments into cubic ones. For a linear segment, the first handle will be set to the start point.
sourcepub fn handle_start(&self) -> Option<DVec2>
pub fn handle_start(&self) -> Option<DVec2>
Get the coordinates of the bezier segment’s first handle point. This represents the only handle in a quadratic segment.
sourcepub fn handle_end(&self) -> Option<DVec2>
pub fn handle_end(&self) -> Option<DVec2>
Get the coordinates of the second handle point. This will return None
for a quadratic segment.
sourcepub fn get_points(&self) -> impl Iterator<Item = DVec2>
pub fn get_points(&self) -> impl Iterator<Item = DVec2>
Get an iterator over the coordinates of all points in a vector.
- For a linear segment, the order of the points will be:
start
,end
. - For a quadratic segment, the order of the points will be:
start
,handle
,end
. - For a cubic segment, the order of the points will be:
start
,handle_start
,handle_end
,end
.
source§impl Bezier
impl Bezier
Functionality that solve for various curve information such as derivative, tangent, intersect, etc.
sourcepub fn de_casteljau_points(&self, t: TValue) -> Vec<Vec<DVec2>>
pub fn de_casteljau_points(&self, t: TValue) -> Vec<Vec<DVec2>>
Returns a list of lists of points representing the De Casteljau points for all iterations at the point t
along the curve using De Casteljau’s algorithm.
The i
th element of the list represents the set of points in the i
th iteration.
More information on the algorithm can be found in the De Casteljau section in Pomax’s primer.
sourcepub fn derivative(&self) -> Option<Bezier>
pub fn derivative(&self) -> Option<Bezier>
Returns a Bezier representing the derivative of the original curve.
- This function returns
None
for a linear segment.
sourcepub fn tangent(&self, t: TValue) -> DVec2
pub fn tangent(&self, t: TValue) -> DVec2
Returns a normalized unit vector representing the tangent at the point t
along the curve.
sourcepub fn tangents_to_point(self, point: DVec2) -> Vec<f64>
pub fn tangents_to_point(self, point: DVec2) -> Vec<f64>
Find the t
-value(s) such that the tangent(s) at t
pass through the specified point.
sourcepub fn normal(&self, t: TValue) -> DVec2
pub fn normal(&self, t: TValue) -> DVec2
Returns a normalized unit vector representing the direction of the normal at the point t
along the curve.
sourcepub fn normals_to_point(self, point: DVec2) -> Vec<f64>
pub fn normals_to_point(self, point: DVec2) -> Vec<f64>
Find the t
-value(s) such that the normal(s) at t
pass through the specified point.
sourcepub fn curvature(&self, t: TValue) -> f64
pub fn curvature(&self, t: TValue) -> f64
Returns the curvature, a scalar value for the derivative at the point t
along the curve.
Curvature is 1 over the radius of a circle with an equivalent derivative.
sourcepub fn local_extrema(&self) -> [impl Iterator<Item = f64>; 2]
pub fn local_extrema(&self) -> [impl Iterator<Item = f64>; 2]
Returns two lists of t
-values representing the local extrema of the x
and y
parametric curves respectively.
The list of t
-values returned are filtered such that they fall within the range [0, 1]
.
sourcepub fn bounding_box(&self) -> [DVec2; 2]
pub fn bounding_box(&self) -> [DVec2; 2]
Return the min and max corners that represent the bounding box of the curve.
sourcepub fn bounding_box_of_anchors_and_handles(&self) -> [DVec2; 2]
pub fn bounding_box_of_anchors_and_handles(&self) -> [DVec2; 2]
Return the min and max corners that represent the bounding box enclosing this Bezier’s two anchor points and any handles.
sourcepub fn is_contained_within(&self, min_corner: DVec2, max_corner: DVec2) -> bool
pub fn is_contained_within(&self, min_corner: DVec2, max_corner: DVec2) -> bool
Returns true
if the bounding box of the bezier is contained entirely within a rectangle defined by its minimum and maximum corners.
sourcepub fn find_tvalues_for_x(&self, x: f64) -> impl Iterator<Item = f64>
pub fn find_tvalues_for_x(&self, x: f64) -> impl Iterator<Item = f64>
Returns an Iterator
containing all possible parametric t
-values at the given x
-coordinate.
sourcepub fn unrestricted_inflections(&self) -> impl Iterator<Item = f64>
pub fn unrestricted_inflections(&self) -> impl Iterator<Item = f64>
Returns list of t
-values representing the inflection points of the curve.
The inflection points are defined to be points at which the second derivative of the curve is equal to zero.
sourcepub fn inflections(&self) -> Vec<f64>
pub fn inflections(&self) -> Vec<f64>
Returns list of parametric t
-values representing the inflection points of the curve.
The list of t
-values returned are filtered such that they fall within the range [0, 1]
.
sourcepub fn intersections(
&self,
other: &Bezier,
error: Option<f64>,
minimum_separation: Option<f64>
) -> Vec<f64>
pub fn intersections( &self, other: &Bezier, error: Option<f64>, minimum_separation: Option<f64> ) -> Vec<f64>
Returns a list of filtered parametric t
values that correspond to intersection points between the current bezier curve and the provided one
such that the difference between adjacent t
values in sorted order is greater than some minimum separation value. If the difference
between 2 adjacent t
values is less than the minimum difference, the filtering takes the larger t
value and discards the smaller t
value.
The returned t
values are with respect to the current bezier, not the provided parameter.
If the provided curve is linear, then zero intersection points will be returned along colinear segments.
error
- For intersections where the provided bezier is non-linear,error
defines the threshold for bounding boxes to be considered an intersection point.minimum_separation
- The minimum difference between adjacentt
values in sorted order
sourcepub fn line_test_crossings(
&self,
point_on_line: DVec2,
direction_vector: DVec2
) -> impl Iterator<Item = f64> + '_
pub fn line_test_crossings( &self, point_on_line: DVec2, direction_vector: DVec2 ) -> impl Iterator<Item = f64> + '_
Returns a list of t
values that correspond to points on this Bezier segment where they intersect with the given line. (direction_vector
does not need to be normalized.)
If this needs to be called frequently with a line of the same rotation angle, consider instead using [line_test_crossings_prerotated
] and moving this function’s setup code into your own logic before the repeated call.
sourcepub fn line_test_crossings_prerotated(
&self,
point_on_line: DVec2,
rotation_matrix: DMat2,
rotated_bezier: Self
) -> impl Iterator<Item = f64> + '_
pub fn line_test_crossings_prerotated( &self, point_on_line: DVec2, rotation_matrix: DMat2, rotated_bezier: Self ) -> impl Iterator<Item = f64> + '_
Returns a list of t
values that correspond to points on this Bezier segment where they intersect with the given infinite line.
This version of the function is for better performance when calling it frequently without needing to change the rotation between each call.
If that isn’t important, use [line_test_crossings
] which wraps this and provides an easier interface by taking a line rotation vector.
Instead, this version requires a rotation matrix for the line’s rotation and a version of this Bezier segment that has had its rotation already applied.
sourcepub fn ray_test_crossings(
&self,
ray_start: DVec2,
ray_direction: DVec2
) -> impl Iterator<Item = f64> + '_
pub fn ray_test_crossings( &self, ray_start: DVec2, ray_direction: DVec2 ) -> impl Iterator<Item = f64> + '_
Returns a list of t
values that correspond to points on this Bezier segment where they intersect with the given ray. (ray_direction
does not need to be normalized.)
If this needs to be called frequently with a ray of the same rotation angle, consider instead using [ray_test_crossings_prerotated
] and moving this function’s setup code into your own logic before the repeated call.
sourcepub fn ray_test_crossings_prerotated(
&self,
ray_start: DVec2,
rotation_matrix: DMat2,
rotated_bezier: Self
) -> impl Iterator<Item = f64> + '_
pub fn ray_test_crossings_prerotated( &self, ray_start: DVec2, rotation_matrix: DMat2, rotated_bezier: Self ) -> impl Iterator<Item = f64> + '_
Returns a list of t
values that correspond to points on this Bezier segment where they intersect with the given infinite ray.
This version of the function is for better performance when calling it frequently without needing to change the rotation between each call.
If that isn’t important, use [ray_test_crossings
] which wraps this and provides an easier interface by taking a ray direction vector.
Instead, this version requires a rotation matrix for the ray’s rotation and a version of this Bezier segment that has had its rotation already applied.
sourcepub fn self_intersections(&self, error: Option<f64>) -> Vec<[f64; 2]>
pub fn self_intersections(&self, error: Option<f64>) -> Vec<[f64; 2]>
Returns a list of parametric t
values that correspond to the self intersection points of the current bezier curve. For each intersection point, the returned t
value is the smaller of the two that correspond to the point.
error
- For intersections with non-linear beziers,error
defines the threshold for bounding boxes to be considered an intersection point.
sourcepub fn rectangle_intersections(
&self,
corner1: DVec2,
corner2: DVec2
) -> Vec<f64>
pub fn rectangle_intersections( &self, corner1: DVec2, corner2: DVec2 ) -> Vec<f64>
Returns a list of parametric t
values that correspond to the intersection points between the curve and a rectangle defined by opposite corners.
source§impl Bezier
impl Bezier
Functionality that transform Beziers, such as split, reduce, offset, etc.
sourcepub fn to_quadratic(&self) -> Bezier
pub fn to_quadratic(&self) -> Bezier
Returns a quadratic approximation of the given Bezier. For cubic Bezier, which typically cannot be represented by a single quadratic segment, this function simply takes the average of the cubic handles to be the new quadratic handle.
sourcepub fn split(&self, t: TValue) -> [Bezier; 2]
pub fn split(&self, t: TValue) -> [Bezier; 2]
Returns the pair of Bezier curves that result from splitting the original curve at the point t
along the curve.
sourcepub fn trim(&self, t1: TValue, t2: TValue) -> Bezier
pub fn trim(&self, t1: TValue, t2: TValue) -> Bezier
Returns the Bezier curve representing the sub-curve between the two provided points.
It will start at the point corresponding to the smaller of t1
and t2
, and end at the point corresponding to the larger of t1
and t2
.
sourcepub fn apply_transformation(
&self,
transformation_function: impl Fn(DVec2) -> DVec2
) -> Bezier
pub fn apply_transformation( &self, transformation_function: impl Fn(DVec2) -> DVec2 ) -> Bezier
Returns a Bezier curve that results from applying the transformation function to each point in the Bezier.
sourcepub fn rotate(&self, angle: f64) -> Bezier
pub fn rotate(&self, angle: f64) -> Bezier
Returns a Bezier curve that results from rotating the curve around the origin by the given angle (in radians).
sourcepub fn rotate_about_point(&self, angle: f64, pivot: DVec2) -> Bezier
pub fn rotate_about_point(&self, angle: f64, pivot: DVec2) -> Bezier
Returns a Bezier curve that results from rotating the curve around the provided point by the given angle (in radians).
sourcepub fn translate(&self, translation: DVec2) -> Bezier
pub fn translate(&self, translation: DVec2) -> Bezier
Returns a Bezier curve that results from translating the curve by the given DVec2
.
sourcepub fn reduce(&self, step_size: Option<f64>) -> Vec<Bezier>
pub fn reduce(&self, step_size: Option<f64>) -> Vec<Bezier>
Split the curve into a number of scalable subcurves. This function may introduce gaps if subsections of the curve are not reducible. The function takes the following parameter:
step_size
- Dictates the granularity at which the function searches for reducible subcurves. The default value is0.01
. A small granularity may increase the chance the function does not introduce gaps, but will increase computation time.
sourcepub fn graduated_scale(&self, start_distance: f64, end_distance: f64) -> Bezier
pub fn graduated_scale(&self, start_distance: f64, end_distance: f64) -> Bezier
Version of the scale
function which scales the curve such that the start of the scaled curve is start_distance
from the original curve, while the end of
of the scaled curve is end_distance
from the original curve. The curve transitions from start_distance
to end_distance
gradually, proportional to the
distance along the equation (t
-value) of the curve.
sourcepub fn offset<ManipulatorGroupId: Identifier>(
&self,
distance: f64
) -> Subpath<ManipulatorGroupId>
pub fn offset<ManipulatorGroupId: Identifier>( &self, distance: f64 ) -> Subpath<ManipulatorGroupId>
Offset will break down the Bezier into reducible subcurves, and scale each subcurve a set distance from the original curve. Note that not all bezier curves are possible to offset, so this function first reduces the curve to scalable segments and then offsets those segments. A proof for why this is true can be found in the Curve offsetting section of Pomax’s bezier curve primer. Offset takes the following parameter:
distance
- The offset’s distance from the curve. Positive values will offset the curve in the same direction as the endpoint normals, while negative values will offset in the opposite direction.
sourcepub fn graduated_offset<ManipulatorGroupId: Identifier>(
&self,
start_distance: f64,
end_distance: f64
) -> Subpath<ManipulatorGroupId>
pub fn graduated_offset<ManipulatorGroupId: Identifier>( &self, start_distance: f64, end_distance: f64 ) -> Subpath<ManipulatorGroupId>
Version of the offset
function which scales the offset such that the start of the offset is start_distance
from the original curve, while the end of
of the offset is end_distance
from the original curve. The curve transitions from start_distance
to end_distance
gradually, proportional to the
distance along the equation (t
-value) of the curve. Similarly to the offset
function, the returned result is an approximation.
sourcepub fn outline<ManipulatorGroupId: Identifier>(
&self,
distance: f64,
cap: Cap
) -> Subpath<ManipulatorGroupId>
pub fn outline<ManipulatorGroupId: Identifier>( &self, distance: f64, cap: Cap ) -> Subpath<ManipulatorGroupId>
Outline will return a vector of Beziers that creates an outline around the curve at the designated distance away from the curve.
It makes use of the offset
function, thus restrictions applicable to offset
are relevant to this function as well.
The ‘caps’, the linear segments at opposite ends of the outline, intersect the original curve at the midpoint of the cap.
Outline takes the following parameter:
distance
- The outline’s distance from the curve.
sourcepub fn graduated_outline<ManipulatorGroupId: Identifier>(
&self,
start_distance: f64,
end_distance: f64,
cap: Cap
) -> Subpath<ManipulatorGroupId>
pub fn graduated_outline<ManipulatorGroupId: Identifier>( &self, start_distance: f64, end_distance: f64, cap: Cap ) -> Subpath<ManipulatorGroupId>
Version of the outline
function which draws the outline at the specified distances away from the curve.
The outline begins start_distance
away, and gradually move to being end_distance
away.
sourcepub fn skewed_outline<ManipulatorGroupId: Identifier>(
&self,
distance1: f64,
distance2: f64,
distance3: f64,
distance4: f64,
cap: Cap
) -> Subpath<ManipulatorGroupId>
pub fn skewed_outline<ManipulatorGroupId: Identifier>( &self, distance1: f64, distance2: f64, distance3: f64, distance4: f64, cap: Cap ) -> Subpath<ManipulatorGroupId>
Version of the graduated_outline
function that allows for the 4 corners of the outline to be different distances away from the curve.
sourcepub fn arcs(&self, arcs_options: ArcsOptions) -> Vec<CircleArc>
pub fn arcs(&self, arcs_options: ArcsOptions) -> Vec<CircleArc>
Approximate a bezier curve with circular arcs. The algorithm can be customized using the ArcsOptions structure.