Skip to main content

Line2D

Struct Line2D 

Source
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

Source

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

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

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

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

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

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

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

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)]);
Source

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

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

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

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

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§

Source§

impl Clone for Line2D

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Line2D

Source§

impl Debug for Line2D

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<Line2D> for Curve2D

Source§

fn from(curve: Line2D) -> Curve2D

Wraps a Line2D as a Curve2D::Line.

Source§

impl Intersect for Line2D

Source§

type Output = CurveCurveIntersection2D

The result of intersecting Self with Rhs.
Source§

fn intersect(&self, other: &Line2D, tol: Tolerance) -> CurveCurveIntersection2D

Intersects self with other, deciding tangency and coincidence with tol.
Source§

impl Intersect<Circle2D> for Line2D

Source§

type Output = CurveCurveIntersection2D

The result of intersecting Self with Rhs.
Source§

fn intersect( &self, other: &Circle2D, tol: Tolerance, ) -> CurveCurveIntersection2D

Intersects self with other, deciding tangency and coincidence with tol.
Source§

impl Intersect<Curve2D> for Line2D

Source§

type Output = CurveCurveIntersection2D

The result of intersecting Self with Rhs.
Source§

fn intersect(&self, other: &Curve2D, tol: Tolerance) -> CurveCurveIntersection2D

Intersects self with other, deciding tangency and coincidence with tol.
Source§

impl Intersect<Line2D> for Circle2D

Source§

type Output = CurveCurveIntersection2D

The result of intersecting Self with Rhs.
Source§

fn intersect(&self, other: &Line2D, tol: Tolerance) -> CurveCurveIntersection2D

Intersects self with other, deciding tangency and coincidence with tol.
Source§

impl Intersect<Line2D> for Curve2D

Source§

type Output = CurveCurveIntersection2D

The result of intersecting Self with Rhs.
Source§

fn intersect(&self, other: &Line2D, tol: Tolerance) -> CurveCurveIntersection2D

Intersects self with other, deciding tangency and coincidence with tol.
Source§

impl<X: CurveOperand2D> Intersect<Line2D> for TrimmedCurve2D<X>

Source§

type Output = CurveCurveIntersection2D

The result of intersecting Self with Rhs.
Source§

fn intersect(&self, other: &Line2D, tol: Tolerance) -> CurveCurveIntersection2D

Intersects self with other, deciding tangency and coincidence with tol.
Source§

impl<X: CurveOperand2D> Intersect<TrimmedCurve2D<X>> for Line2D
where <Line2D as CurveOperand2D>::Basis: ExactCurveCurve2D<X::Basis>,

Source§

type Output = CurveCurveIntersection2D

The result of intersecting Self with Rhs.
Source§

fn intersect( &self, other: &TrimmedCurve2D<X>, tol: Tolerance, ) -> CurveCurveIntersection2D

Intersects self with other, deciding tangency and coincidence with tol.
Source§

impl IntersectExact for Line2D

Source§

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

The result of intersecting Self with Rhs.
Source§

impl IntersectExact<Circle2D> for Line2D

Source§

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

The result of intersecting Self with Rhs.
Source§

impl IntersectExact<Line2D> for Circle2D

Source§

fn intersect_exact(&self, other: &Line2D, tol: Tolerance) -> Self::Output

The same result as in the other argument order.

Source§

type Output = <Line2D as IntersectExact<Circle2D>>::Output

The result of intersecting Self with Rhs.
Source§

impl ParametricCurve2D for Line2D

Source§

fn eval_point(&self, u: f64) -> Point2D

Evaluates the point on the curve at parameter u.
Source§

fn eval_derivative(&self, u: f64, order: u32) -> Vector2D

Evaluates the derivative of the given order at parameter u.
Source§

fn bounds(&self) -> (f64, f64)

Returns the curve’s parameter bounds as (first, last). Unbounded curves use (f64::NEG_INFINITY, f64::INFINITY).
Source§

fn period(&self) -> Option<f64>

Returns the curve’s period, or None if the curve is not periodic.
Source§

fn eval_points(&self, us: &[f64]) -> Vec<Point2D>

Evaluates the points on the curve at each parameter in us. Read more
Source§

fn eval_d1(&self, u: f64) -> (Point2D, Vector2D)

Evaluates the point and the first derivative at parameter u. Read more
Source§

fn eval_d2(&self, u: f64) -> (Point2D, Vector2D, Vector2D)

Evaluates the point and the first two derivatives at parameter u. Read more
Source§

fn is_periodic(&self) -> bool

Returns whether the curve is periodic, i.e. whether period is Some.
Source§

impl PartialEq for Line2D

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PointExtrema2D for Line2D

Source§

fn extrema(&self, point: Point2D, tol: Tolerance) -> Vec<CurveProjection2D>

Parameters on the curve’s own domain that include every local minimum of the distance from 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
Source§

impl StructuralPartialEq for Line2D

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.