Struct Line2
pub struct Line2 { /* private fields */ }Expand description
An infinite 2D line represented by two sample points.
Implementations§
§impl Line2
impl Line2
pub fn try_new(a: Point2, b: Point2) -> Result<Line2, GeometryError>
pub fn try_new(a: Point2, b: Point2) -> Result<Line2, GeometryError>
Creates a validated line from two distinct sample points with finite coordinates.
§Errors
Returns GeometryError::NonFiniteComponent when either input point
contains a non-finite coordinate.
Returns GeometryError::IdenticalPoints when a == b.
pub fn try_from_points(a: Point2, b: Point2) -> Result<Line2, GeometryError>
pub fn try_from_points(a: Point2, b: Point2) -> Result<Line2, GeometryError>
Creates a validated line from two distinct finite sample points.
§Errors
Returns GeometryError::NonFiniteComponent when either input point
contains a non-finite coordinate.
Returns GeometryError::IdenticalPoints when a == b.
§Examples
use use_geometry::{GeometryError, Line2, Point2};
let line = Line2::try_from_points(Point2::new(0.0, 0.0), Point2::new(2.0, 2.0))?;
assert!(line.contains_point(Point2::new(4.0, 4.0)));
assert!(matches!(
Line2::try_from_points(Point2::new(1.0, 1.0), Point2::new(1.0, 1.0)),
Err(GeometryError::IdenticalPoints)
));pub fn try_from_point_direction(
point: Point2,
direction: Vector2,
) -> Result<Line2, GeometryError>
pub fn try_from_point_direction( point: Point2, direction: Vector2, ) -> Result<Line2, GeometryError>
Creates a validated line from a point and non-zero direction vector.
§Errors
Returns GeometryError::NonFiniteComponent when point or direction
contains a non-finite value.
Returns GeometryError::ZeroDirectionVector when direction is zero.
§Examples
use use_geometry::{GeometryError, Line2, Point2, Vector2};
let line = Line2::try_from_point_direction(Point2::new(1.0, 2.0), Vector2::new(3.0, 4.0))?;
assert_eq!(line.b(), Point2::new(4.0, 6.0));pub fn contains_point(self, point: Point2) -> bool
pub fn contains_point(self, point: Point2) -> bool
Returns true when point lies on the infinite line.
pub fn contains_point_with_tolerance(
self,
point: Point2,
tolerance: f64,
) -> Result<bool, GeometryError>
pub fn contains_point_with_tolerance( self, point: Point2, tolerance: f64, ) -> Result<bool, GeometryError>
Returns true when point lies within tolerance of the line.
§Errors
Returns GeometryError::NonFiniteTolerance when tolerance is NaN
or infinite.
Returns GeometryError::NegativeTolerance when tolerance is negative.
pub fn try_slope(self) -> Result<Option<f64>, GeometryError>
pub fn try_slope(self) -> Result<Option<f64>, GeometryError>
Returns the slope when both line points contain only finite coordinates.
§Errors
Returns GeometryError::NonFiniteComponent when either point contains
a non-finite coordinate.
§Examples
use use_geometry::{GeometryError, Line2, Point2};
let diagonal = Line2::try_new(Point2::new(1.0, 1.0), Point2::new(3.0, 5.0))?;
let vertical = Line2::try_new(Point2::new(2.0, 1.0), Point2::new(2.0, 5.0))?;
assert_eq!(diagonal.try_slope()?, Some(2.0));
assert_eq!(vertical.try_slope()?, None);