pub struct Line2D { /* private fields */ }Expand description
An infinite line in 2D: an origin point and a unit direction, evaluated
as origin + u * direction.
§Examples
use geomcore::{Line2D, Point2D, Vector2D};
let line = Line2D::new(Point2D::ORIGIN, Vector2D::X).unwrap();
assert_eq!(line.eval_point(3.0), Point2D::new(3.0, 0.0));Implementations§
Source§impl Line2D
impl Line2D
Sourcepub fn new(
origin: Point2D,
direction: Vector2D,
) -> Result<Line2D, LineConstructionError>
pub fn new( origin: Point2D, direction: Vector2D, ) -> Result<Line2D, LineConstructionError>
Creates a new line from an origin and a direction.
The direction is normalized. Returns
LineConstructionError::NullDirection if direction cannot be
normalized (zero length).
§Examples
use geomcore::{Line2D, Point2D, Vector2D};
let line = Line2D::new(Point2D::ORIGIN, Vector2D::new(0.0, 5.0)).unwrap();
assert_eq!(line.direction(), Vector2D::Y);Sourcepub fn from_axis(axis: Axis2D) -> Line2D
pub fn from_axis(axis: Axis2D) -> Line2D
Creates a line from an axis directly.
§Examples
use geomcore::{Axis2D, Line2D, Point2D, Vector2D};
let axis = Axis2D::new(Point2D::ORIGIN, Vector2D::X).unwrap();
let line = Line2D::from_axis(axis);
assert_eq!(line.axis(), axis);Sourcepub fn from_two_points(
p1: Point2D,
p2: Point2D,
) -> Result<Line2D, LineConstructionError>
pub fn from_two_points( p1: Point2D, p2: Point2D, ) -> Result<Line2D, LineConstructionError>
Creates a line through two points; the direction points from p1 to
p2.
The two points must be distinct. Returns
LineConstructionError::ConfusedPoints if p2 - p1 cannot be
normalized (the points are coincident or too close to distinguish).
§Examples
use geomcore::{Line2D, Point2D};
let line = Line2D::from_two_points(Point2D::ORIGIN, Point2D::new(0.0, 4.0)).unwrap();
assert_eq!(line.origin(), Point2D::ORIGIN);Sourcepub fn origin(&self) -> Point2D
pub fn origin(&self) -> Point2D
Returns the origin point of the line.
§Examples
use geomcore::{Line2D, Point2D, Vector2D};
let line = Line2D::new(Point2D::new(1.0, 2.0), Vector2D::X).unwrap();
assert_eq!(line.origin(), Point2D::new(1.0, 2.0));Sourcepub fn direction(&self) -> Vector2D
pub fn direction(&self) -> Vector2D
Returns the unit direction of the line.
§Examples
use geomcore::{Line2D, Point2D, Vector2D};
let line = Line2D::new(Point2D::ORIGIN, Vector2D::new(0.0, 5.0)).unwrap();
assert_eq!(line.direction(), Vector2D::Y);Sourcepub fn axis(&self) -> Axis2D
pub fn axis(&self) -> Axis2D
Returns the line’s underlying axis.
§Examples
use geomcore::{Axis2D, Line2D, Point2D, Vector2D};
let axis = Axis2D::new(Point2D::ORIGIN, Vector2D::X).unwrap();
let line = Line2D::from_axis(axis);
assert_eq!(line.axis(), axis);Sourcepub fn eval_point(&self, u: f64) -> Point2D
pub fn eval_point(&self, u: f64) -> Point2D
Evaluates the point on the line at parameter u: origin + u * direction.
§Examples
use geomcore::{Line2D, Point2D, Vector2D};
let line = Line2D::new(Point2D::ORIGIN, Vector2D::X).unwrap();
assert_eq!(line.eval_point(3.0), Point2D::new(3.0, 0.0));Sourcepub fn eval_points(&self, us: &[f64]) -> Vec<Point2D>
pub fn eval_points(&self, us: &[f64]) -> Vec<Point2D>
Evaluates the points on the line at each parameter in us.
§Examples
use geomcore::{Line2D, Point2D, Vector2D};
let line = Line2D::new(Point2D::ORIGIN, Vector2D::X).unwrap();
let points = line.eval_points(&[0.0, 1.0, 2.0]);
assert_eq!(points, vec![Point2D::new(0.0, 0.0), Point2D::new(1.0, 0.0), Point2D::new(2.0, 0.0)]);Sourcepub fn eval_derivative(&self, u: f64, order: u32) -> Vector2D
pub fn eval_derivative(&self, u: f64, order: u32) -> Vector2D
Evaluates the derivative of the given order at parameter u.
The line is affine in u, so the first derivative is the constant
direction vector and every derivative of order 2 or higher is zero.
u does not affect the result (included for API consistency with
curved parametric types).
§Panics
Panics if order == 0; use Line2D::eval_point to evaluate the
position itself.
§Examples
use geomcore::{Line2D, Point2D, Vector2D};
let line = Line2D::new(Point2D::ORIGIN, Vector2D::new(0.0, 3.0)).unwrap();
assert_eq!(line.eval_derivative(1.5, 1), Vector2D::Y);
assert_eq!(line.eval_derivative(1.5, 2), Vector2D::ZERO);Sourcepub fn parameter_of(&self, point: Point2D) -> f64
pub fn parameter_of(&self, point: Point2D) -> f64
Recovers the parameter u of a point on (or near) the line: the
signed projection (point - origin) . direction.
§Examples
use geomcore::{Line2D, Point2D, Vector2D};
let line = Line2D::new(Point2D::ORIGIN, Vector2D::X).unwrap();
assert_eq!(line.parameter_of(Point2D::new(2.5, 0.0)), 2.5);Sourcepub fn contains(&self, point: Point2D, tol: Tolerance) -> bool
pub fn contains(&self, point: Point2D, tol: Tolerance) -> bool
Returns whether point lies on the line: the inverse parameter is
recovered with Line2D::parameter_of and re-evaluated, and the
point counts as contained when the re-evaluated point is within
tol.confusion of it.
§Examples
use geomcore::{Line2D, Point2D, Tolerance, Vector2D};
let line = Line2D::new(Point2D::ORIGIN, Vector2D::X).unwrap();
let tol = Tolerance::DEFAULT;
assert!(line.contains(Point2D::new(2.5, 0.0), tol));
assert!(!line.contains(Point2D::new(2.5, 1.0), tol));Sourcepub fn project_point(&self, point: Point2D, tol: Tolerance) -> CurveProjection2D
pub fn project_point(&self, point: Point2D, tol: Tolerance) -> CurveProjection2D
Projects point onto the line, returning the parameter of the
closest point and its distance. Distances within tol.confusion
snap to 0.0, matching Line2D::contains.
§Examples
use geomcore::{Line2D, Point2D, Tolerance, Vector2D};
let line = Line2D::new(Point2D::ORIGIN, Vector2D::X).unwrap();
let proj = line.project_point(Point2D::new(2.5, 1.0), Tolerance::DEFAULT);
assert_eq!(proj.parameter, 2.5);
assert_eq!(proj.distance, 1.0);Sourcepub fn project_points(
&self,
points: &[Point2D],
tol: Tolerance,
) -> Vec<CurveProjection2D>
pub fn project_points( &self, points: &[Point2D], tol: Tolerance, ) -> Vec<CurveProjection2D>
Projects each point in points onto the line.
Default-style batch wrapper over Line2D::project_point: one
native call per batch, mirroring Line2D::eval_points.
Trait Implementations§
impl Copy for Line2D
Source§impl Intersect for Line2D
impl Intersect for Line2D
Source§type Output = CurveCurveIntersection2D
type Output = CurveCurveIntersection2D
Self with Rhs.Source§impl Intersect<Circle2D> for Line2D
impl Intersect<Circle2D> for Line2D
Source§type Output = CurveCurveIntersection2D
type Output = CurveCurveIntersection2D
Self with Rhs.Source§impl Intersect<Curve2D> for Line2D
impl Intersect<Curve2D> for Line2D
Source§type Output = CurveCurveIntersection2D
type Output = CurveCurveIntersection2D
Self with Rhs.Source§impl Intersect<Line2D> for Circle2D
impl Intersect<Line2D> for Circle2D
Source§type Output = CurveCurveIntersection2D
type Output = CurveCurveIntersection2D
Self with Rhs.Source§impl Intersect<Line2D> for Curve2D
impl Intersect<Line2D> for Curve2D
Source§type Output = CurveCurveIntersection2D
type Output = CurveCurveIntersection2D
Self with Rhs.Source§impl<X: CurveOperand2D> Intersect<Line2D> for TrimmedCurve2D<X>
impl<X: CurveOperand2D> Intersect<Line2D> for TrimmedCurve2D<X>
Source§type Output = CurveCurveIntersection2D
type Output = CurveCurveIntersection2D
Self with Rhs.Source§impl<X: CurveOperand2D> Intersect<TrimmedCurve2D<X>> for Line2Dwhere
<Line2D as CurveOperand2D>::Basis: ExactCurveCurve2D<X::Basis>,
impl<X: CurveOperand2D> Intersect<TrimmedCurve2D<X>> for Line2Dwhere
<Line2D as CurveOperand2D>::Basis: ExactCurveCurve2D<X::Basis>,
Source§type Output = CurveCurveIntersection2D
type Output = CurveCurveIntersection2D
Self with Rhs.Source§fn intersect(
&self,
other: &TrimmedCurve2D<X>,
tol: Tolerance,
) -> CurveCurveIntersection2D
fn intersect( &self, other: &TrimmedCurve2D<X>, tol: Tolerance, ) -> CurveCurveIntersection2D
self with other, deciding tangency and coincidence
with tol.Source§impl IntersectExact for Line2D
impl IntersectExact for Line2D
Source§fn intersect_exact(
&self,
other: &Line2D,
tol: Tolerance,
) -> LineLine2DIntersection
fn intersect_exact( &self, other: &Line2D, tol: Tolerance, ) -> LineLine2DIntersection
Intersects this line with another 2D line.
The 2x2 system solves both parameters at once; directions parallel
within tol.angular fall back to a coincidence check.
§Examples
use geomcore::IntersectExact;
use geomcore::{Line2D, LineLine2DIntersection, Point2D, Tolerance, Vector2D};
let l1 = Line2D::new(Point2D::ORIGIN, Vector2D::X).unwrap();
let l2 = Line2D::new(Point2D::ORIGIN, Vector2D::Y).unwrap();
let tol = Tolerance::DEFAULT;
match l1.intersect_exact(&l2, tol) {
LineLine2DIntersection::Point(s, p, t) => {
assert_eq!((s, t), (0.0, 0.0));
assert_eq!(p, Point2D::ORIGIN);
}
_ => panic!("expected a point"),
}Source§type Output = LineLine2DIntersection
type Output = LineLine2DIntersection
Self with Rhs.Source§impl IntersectExact<Circle2D> for Line2D
impl IntersectExact<Circle2D> for Line2D
Source§fn intersect_exact(
&self,
circle: &Circle2D,
tol: Tolerance,
) -> LineCircle2DIntersection
fn intersect_exact( &self, circle: &Circle2D, tol: Tolerance, ) -> LineCircle2DIntersection
Intersects this line with a 2D circle.
Substituting the unit-speed parametrization into
|X - C|^2 = r^2 gives a quadratic with leading coefficient
exactly 1, classified with solve_quadratic about the line’s
closest approach to the centre; the line is tangent when that
closest point lies within tol.confusion of the circle.
§Examples
use geomcore::IntersectExact;
use geomcore::{Circle2D, Line2D, LineCircle2DIntersection, Point2D, Tolerance, Vector2D};
let line = Line2D::new(Point2D::ORIGIN, Vector2D::X).unwrap();
let circle = Circle2D::new(Point2D::ORIGIN, 2.0).unwrap();
let tol = Tolerance::DEFAULT;
match line.intersect_exact(&circle, tol) {
LineCircle2DIntersection::Points((t1, _), (t2, _)) => {
assert_eq!((t1, t2), (-2.0, 2.0));
}
_ => panic!("expected two points"),
}Source§type Output = LineCircle2DIntersection
type Output = LineCircle2DIntersection
Self with Rhs.Source§impl IntersectExact<Line2D> for Circle2D
impl IntersectExact<Line2D> for Circle2D
Source§impl ParametricCurve2D for Line2D
impl ParametricCurve2D for Line2D
Source§fn eval_point(&self, u: f64) -> Point2D
fn eval_point(&self, u: f64) -> Point2D
u.Source§fn eval_derivative(&self, u: f64, order: u32) -> Vector2D
fn eval_derivative(&self, u: f64, order: u32) -> Vector2D
order at parameter u.Source§fn bounds(&self) -> (f64, f64)
fn bounds(&self) -> (f64, f64)
(first, last). Unbounded
curves use (f64::NEG_INFINITY, f64::INFINITY).Source§fn period(&self) -> Option<f64>
fn period(&self) -> Option<f64>
None if the curve is not periodic.Source§fn eval_points(&self, us: &[f64]) -> Vec<Point2D>
fn eval_points(&self, us: &[f64]) -> Vec<Point2D>
us. Read moreSource§fn eval_d1(&self, u: f64) -> (Point2D, Vector2D)
fn eval_d1(&self, u: f64) -> (Point2D, Vector2D)
u. Read moreSource§fn eval_d2(&self, u: f64) -> (Point2D, Vector2D, Vector2D)
fn eval_d2(&self, u: f64) -> (Point2D, Vector2D, Vector2D)
u. Read moreSource§fn is_periodic(&self) -> bool
fn is_periodic(&self) -> bool
period is Some.Source§impl PointExtrema2D for Line2D
impl PointExtrema2D for Line2D
Source§fn extrema(&self, point: Point2D, tol: Tolerance) -> Vec<CurveProjection2D>
fn extrema(&self, point: Point2D, tol: Tolerance) -> Vec<CurveProjection2D>
point (other stationary points may
be listed too; a valley of equal minima is represented by one of
its points), ordered by ascending distance, the global closest
point first. Distances within tol.confusion snap to 0.0. Read more