Skip to main content

Segment2

Struct Segment2 

pub struct Segment2 { /* private fields */ }
Expand description

A finite line segment between two 2D points.

Implementations§

§

impl Segment2

pub const fn new(start: Point2, end: Point2) -> Segment2

Creates a segment from start to end.

pub fn try_new(start: Point2, end: Point2) -> Result<Segment2, GeometryError>

Creates a segment from start to end with finite point coordinates.

§Errors

Returns GeometryError::NonFiniteComponent when either input point contains a non-finite coordinate.

§Examples
use use_geometry::{GeometryError, Point2, Segment2};

let segment = Segment2::try_new(Point2::new(0.0, 0.0), Point2::new(4.0, 2.0))?;
assert_eq!(segment.midpoint(), Point2::new(2.0, 1.0));

pub const fn start(self) -> Point2

Returns the segment start point.

pub const fn end(self) -> Point2

Returns the segment end point.

pub fn length(self) -> f64

Returns the segment length.

pub fn length_squared(self) -> f64

Returns the squared segment length.

pub const fn midpoint(self) -> Point2

Returns the segment midpoint.

pub const fn vector(self) -> Vector2

Returns the segment vector from start to end.

pub const fn point_at(self, t: f64) -> Point2

Returns the point at parameter t along the segment.

§Examples
use use_geometry::{Point2, Segment2};

let segment = Segment2::new(Point2::new(0.0, 0.0), Point2::new(4.0, 2.0));
assert_eq!(segment.point_at(0.25), Point2::new(1.0, 0.5));

pub const fn reversed(self) -> Segment2

Returns the segment with its endpoints reversed.

§Examples
use use_geometry::{Point2, Segment2};

let segment = Segment2::new(Point2::new(1.0, 2.0), Point2::new(4.0, 6.0));

assert_eq!(segment.reversed().start(), Point2::new(4.0, 6.0));
assert_eq!(segment.reversed().end(), Point2::new(1.0, 2.0));

pub fn is_degenerate(self) -> bool

Returns true when the segment collapses to a single point.

pub fn is_degenerate_with_tolerance( self, tolerance: f64, ) -> Result<bool, GeometryError>

Returns true when the segment length is within tolerance of zero.

§Errors

Returns GeometryError::NonFiniteTolerance when tolerance is NaN or infinite.

Returns GeometryError::NegativeTolerance when tolerance is negative.

§Examples
use use_geometry::{GeometryError, Point2, Segment2};

let segment = Segment2::new(Point2::new(2.0, 2.0), Point2::new(2.0, 2.0));
assert!(segment.is_degenerate_with_tolerance(0.0)?);

pub const fn aabb(self) -> Aabb2

Returns the segment bounding box.

Trait Implementations§

§

impl Clone for Segment2

§

fn clone(&self) -> Segment2

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
§

impl Debug for Segment2

§

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

Formats the value using the given formatter. Read more
§

impl PartialEq for Segment2

§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl Copy for Segment2

§

impl StructuralPartialEq for Segment2

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.