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) -> CurveProjection
pub fn project_point(&self, point: Point2D, tol: Tolerance) -> CurveProjection
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<CurveProjection>
pub fn project_points( &self, points: &[Point2D], tol: Tolerance, ) -> Vec<CurveProjection>
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 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 is_periodic(&self) -> bool
fn is_periodic(&self) -> bool
period is Some.