kurbo/
lib.rs

1// Copyright 2018 the Kurbo Authors
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3
4//! 2D geometry, with a focus on curves.
5//!
6//! The kurbo library contains data structures and algorithms for curves and
7//! vector paths. It was designed to serve the needs of 2D graphics applications,
8//! but it is intended to be general enough to be useful for other applications.
9//! It can be used as "vocabulary types" for representing curves and paths, and
10//! also contains a number of computational geometry methods.
11//!
12//! # Examples
13//!
14//! Basic UI-style geometry:
15//! ```
16//! use kurbo::{Insets, Point, Rect, Size, Vec2};
17//!
18//! let pt = Point::new(10.0, 10.0);
19//! let vector = Vec2::new(5.0, -5.0);
20//! let pt2 = pt + vector;
21//! assert_eq!(pt2, Point::new(15.0, 5.0));
22//!
23//! let rect = Rect::from_points(pt, pt2);
24//! assert_eq!(rect, Rect::from_origin_size((10.0, 5.0), (5.0, 5.0)));
25//!
26//! let insets = Insets::uniform(1.0);
27//! let inset_rect = rect - insets;
28//! assert_eq!(inset_rect.size(), Size::new(3.0, 3.0));
29//! ```
30//!
31//! Finding the closest position on a [`Shape`]'s perimeter to a [`Point`]:
32//!
33//! ```
34//! use kurbo::{Circle, ParamCurve, ParamCurveNearest, Point, Shape};
35//!
36//! const DESIRED_ACCURACY: f64 = 0.1;
37//!
38//! /// Given a shape and a point, returns the closest position on the shape's
39//! /// perimeter, or `None` if the shape is malformed.
40//! fn closest_perimeter_point(shape: impl Shape, pt: Point) -> Option<Point> {
41//!     let mut best: Option<(Point, f64)> = None;
42//!     for segment in shape.path_segments(DESIRED_ACCURACY) {
43//!         let nearest = segment.nearest(pt, DESIRED_ACCURACY);
44//!         if best.map(|(_, best_d)| nearest.distance_sq < best_d).unwrap_or(true) {
45//!             best = Some((segment.eval(nearest.t), nearest.distance_sq))
46//!         }
47//!     }
48//!     best.map(|(point, _)| point)
49//! }
50//!
51//! let circle = Circle::new((5.0, 5.0), 5.0);
52//! let hit_point = Point::new(5.0, -2.0);
53//! let expectation = Point::new(5.0, 0.0);
54//! let hit = closest_perimeter_point(circle, hit_point).unwrap();
55//! assert!(hit.distance(expectation) <= DESIRED_ACCURACY);
56//! ```
57//!
58//! # Features
59//!
60//! This crate either uses the standard library or the [`libm`] crate for
61//! math functionality. The `std` feature is enabled by default, but can be
62//! disabled, as long as the `libm` feature is enabled. This is useful for
63//! `no_std` environments. However, note that the `libm` crate is not as
64//! efficient as the standard library, and that this crate still uses the
65//! `alloc` crate regardless.
66//!
67//! [`libm`]: https://docs.rs/libm
68
69// LINEBENDER LINT SET - lib.rs - v1
70// See https://linebender.org/wiki/canonical-lints/
71// These lints aren't included in Cargo.toml because they
72// shouldn't apply to examples and tests
73#![warn(unused_crate_dependencies)]
74#![warn(clippy::print_stdout, clippy::print_stderr)]
75// END LINEBENDER LINT SET
76#![cfg_attr(docsrs, feature(doc_auto_cfg))]
77#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
78#![allow(
79    clippy::unreadable_literal,
80    clippy::many_single_char_names,
81    clippy::excessive_precision,
82    clippy::bool_to_int_with_if
83)]
84// The following lints are part of the Linebender standard set,
85// but resolving them has been deferred for now.
86// Feel free to send a PR that solves one or more of these.
87#![allow(
88    missing_debug_implementations,
89    elided_lifetimes_in_paths,
90    single_use_lifetimes,
91    trivial_numeric_casts,
92    unnameable_types,
93    clippy::use_self,
94    clippy::return_self_not_must_use,
95    clippy::cast_possible_truncation,
96    clippy::wildcard_imports,
97    clippy::shadow_unrelated,
98    clippy::missing_assert_message,
99    clippy::missing_errors_doc,
100    clippy::missing_panics_doc,
101    clippy::exhaustive_enums,
102    clippy::match_same_arms,
103    clippy::partial_pub_fields,
104    clippy::unseparated_literal_suffix,
105    clippy::duplicated_attributes
106)]
107
108#[cfg(not(any(feature = "std", feature = "libm")))]
109compile_error!("kurbo requires either the `std` or `libm` feature");
110
111// Suppress the unused_crate_dependencies lint when both std and libm are specified.
112#[cfg(all(feature = "std", feature = "libm"))]
113use libm as _;
114
115extern crate alloc;
116
117mod affine;
118mod arc;
119mod bezpath;
120mod circle;
121pub mod common;
122mod cubicbez;
123mod ellipse;
124mod fit;
125mod insets;
126mod line;
127mod mindist;
128pub mod offset;
129mod param_curve;
130mod point;
131mod quadbez;
132mod quadspline;
133mod rect;
134mod rounded_rect;
135mod rounded_rect_radii;
136mod shape;
137pub mod simplify;
138mod size;
139mod stroke;
140mod svg;
141mod translate_scale;
142mod triangle;
143mod vec2;
144
145pub use crate::affine::Affine;
146pub use crate::arc::{Arc, ArcAppendIter};
147pub use crate::bezpath::{
148    flatten, segments, BezPath, LineIntersection, MinDistance, PathEl, PathSeg, PathSegIter,
149    Segments,
150};
151pub use crate::circle::{Circle, CirclePathIter, CircleSegment};
152pub use crate::cubicbez::{cubics_to_quadratic_splines, CubicBez, CubicBezIter, CuspType};
153pub use crate::ellipse::Ellipse;
154pub use crate::fit::{
155    fit_to_bezpath, fit_to_bezpath_opt, fit_to_cubic, CurveFitSample, ParamCurveFit,
156};
157pub use crate::insets::Insets;
158pub use crate::line::{ConstPoint, Line, LinePathIter};
159pub use crate::param_curve::{
160    Nearest, ParamCurve, ParamCurveArclen, ParamCurveArea, ParamCurveCurvature, ParamCurveDeriv,
161    ParamCurveExtrema, ParamCurveNearest, DEFAULT_ACCURACY, MAX_EXTREMA,
162};
163pub use crate::point::Point;
164pub use crate::quadbez::{QuadBez, QuadBezIter};
165pub use crate::quadspline::QuadSpline;
166pub use crate::rect::{Rect, RectPathIter};
167pub use crate::rounded_rect::{RoundedRect, RoundedRectPathIter};
168pub use crate::rounded_rect_radii::RoundedRectRadii;
169pub use crate::shape::Shape;
170pub use crate::size::Size;
171pub use crate::stroke::{
172    dash, stroke, Cap, DashIterator, Dashes, Join, Stroke, StrokeOptLevel, StrokeOpts,
173};
174pub use crate::svg::{SvgArc, SvgParseError};
175pub use crate::translate_scale::TranslateScale;
176pub use crate::triangle::{Triangle, TrianglePathIter};
177pub use crate::vec2::Vec2;