Struct LineSegment

Source
pub struct LineSegment<T> { /* private fields */ }

Implementations§

Source§

impl<T> LineSegment<T>

Source

pub fn new(p1: Complex<T>, p2: Complex<T>) -> Self

Source§

impl<T> LineSegment<T>
where T: Ccwable,

Source

pub fn dir(&self) -> Complex<T>

Examples found in repository?
crates/competitive/src/geometry/line.rs (line 68)
67    pub fn is_parallel(&self, other: &Self) -> bool {
68        Approx(self.dir().cross(other.dir())) == Approx(T::zero())
69    }
70    pub fn is_orthogonal(&self, other: &Self) -> bool {
71        Approx(self.dir().dot(other.dir())) == Approx(T::zero())
72    }
73    pub fn intersect(&self, other: &Self) -> bool {
74        self.ccw(other.p1) as i8 * self.ccw(other.p2) as i8 <= 0
75            && other.ccw(self.p1) as i8 * other.ccw(self.p2) as i8 <= 0
76    }
77    pub fn intersect_point(&self, p: Complex<T>) -> bool {
78        self.ccw(p) == Ccw::OnSegment
79    }
80}
81impl<T> LineSegment<T>
82where
83    T: Ccwable + Float,
84{
85    pub fn projection(&self, p: Complex<T>) -> Complex<T> {
86        let e = self.dir().unit();
87        self.p1 + e * (p - self.p1).dot(e)
88    }
89    pub fn reflection(&self, p: Complex<T>) -> Complex<T> {
90        let d = self.projection(p) - p;
91        p + d + d
92    }
93    pub fn cross_point(&self, other: &Self) -> Option<Complex<T>> {
94        if self.intersect(other) {
95            let a = self.dir().cross(other.dir());
96            let b = self.dir().cross(self.p2 - other.p1);
97            if Approx(a.abs()) == Approx(T::zero()) && Approx(b.abs()) == Approx(T::zero()) {
98                Some(other.p1)
99            } else {
100                Some(other.p1 + (other.dir() * b / a))
101            }
102        } else {
103            None
104        }
105    }
Source

pub fn ccw(&self, p: Complex<T>) -> Ccw

Examples found in repository?
crates/competitive/src/geometry/line.rs (line 74)
73    pub fn intersect(&self, other: &Self) -> bool {
74        self.ccw(other.p1) as i8 * self.ccw(other.p2) as i8 <= 0
75            && other.ccw(self.p1) as i8 * other.ccw(self.p2) as i8 <= 0
76    }
77    pub fn intersect_point(&self, p: Complex<T>) -> bool {
78        self.ccw(p) == Ccw::OnSegment
79    }
Source

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

Source

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

Source

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

Examples found in repository?
crates/competitive/src/geometry/line.rs (line 94)
93    pub fn cross_point(&self, other: &Self) -> Option<Complex<T>> {
94        if self.intersect(other) {
95            let a = self.dir().cross(other.dir());
96            let b = self.dir().cross(self.p2 - other.p1);
97            if Approx(a.abs()) == Approx(T::zero()) && Approx(b.abs()) == Approx(T::zero()) {
98                Some(other.p1)
99            } else {
100                Some(other.p1 + (other.dir() * b / a))
101            }
102        } else {
103            None
104        }
105    }
106    pub fn distance_point(&self, p: Complex<T>) -> T {
107        let r = self.projection(p);
108        if self.intersect_point(r) {
109            (r - p).abs()
110        } else {
111            (self.p1 - p).abs().min((self.p2 - p).abs())
112        }
113    }
114    pub fn distance(&self, other: &Self) -> T {
115        if self.intersect(other) {
116            T::zero()
117        } else {
118            let d1 = self.distance_point(other.p1);
119            let d2 = self.distance_point(other.p2);
120            let d3 = other.distance_point(self.p1);
121            let d4 = other.distance_point(self.p2);
122            d1.min(d2).min(d3).min(d4)
123        }
124    }
Source

pub fn intersect_point(&self, p: Complex<T>) -> bool

Examples found in repository?
crates/competitive/src/geometry/line.rs (line 108)
106    pub fn distance_point(&self, p: Complex<T>) -> T {
107        let r = self.projection(p);
108        if self.intersect_point(r) {
109            (r - p).abs()
110        } else {
111            (self.p1 - p).abs().min((self.p2 - p).abs())
112        }
113    }
Source§

impl<T> LineSegment<T>
where T: Ccwable + Float,

Source

pub fn projection(&self, p: Complex<T>) -> Complex<T>

Examples found in repository?
crates/competitive/src/geometry/line.rs (line 90)
89    pub fn reflection(&self, p: Complex<T>) -> Complex<T> {
90        let d = self.projection(p) - p;
91        p + d + d
92    }
93    pub fn cross_point(&self, other: &Self) -> Option<Complex<T>> {
94        if self.intersect(other) {
95            let a = self.dir().cross(other.dir());
96            let b = self.dir().cross(self.p2 - other.p1);
97            if Approx(a.abs()) == Approx(T::zero()) && Approx(b.abs()) == Approx(T::zero()) {
98                Some(other.p1)
99            } else {
100                Some(other.p1 + (other.dir() * b / a))
101            }
102        } else {
103            None
104        }
105    }
106    pub fn distance_point(&self, p: Complex<T>) -> T {
107        let r = self.projection(p);
108        if self.intersect_point(r) {
109            (r - p).abs()
110        } else {
111            (self.p1 - p).abs().min((self.p2 - p).abs())
112        }
113    }
Source

pub fn reflection(&self, p: Complex<T>) -> Complex<T>

Source

pub fn cross_point(&self, other: &Self) -> Option<Complex<T>>

Source

pub fn distance_point(&self, p: Complex<T>) -> T

Examples found in repository?
crates/competitive/src/geometry/line.rs (line 118)
114    pub fn distance(&self, other: &Self) -> T {
115        if self.intersect(other) {
116            T::zero()
117        } else {
118            let d1 = self.distance_point(other.p1);
119            let d2 = self.distance_point(other.p2);
120            let d3 = other.distance_point(self.p1);
121            let d4 = other.distance_point(self.p2);
122            d1.min(d2).min(d3).min(d4)
123        }
124    }
Source

pub fn distance(&self, other: &Self) -> T

Trait Implementations§

Source§

impl<T: Clone> Clone for LineSegment<T>

Source§

fn clone(&self) -> LineSegment<T>

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
Source§

impl<T: Debug> Debug for LineSegment<T>

Source§

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

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

impl<T: PartialEq> PartialEq for LineSegment<T>

Source§

fn eq(&self, other: &LineSegment<T>) -> 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.
Source§

impl<T> StructuralPartialEq for LineSegment<T>

Auto Trait Implementations§

§

impl<T> Freeze for LineSegment<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for LineSegment<T>
where T: RefUnwindSafe,

§

impl<T> Send for LineSegment<T>
where T: Send,

§

impl<T> Sync for LineSegment<T>
where T: Sync,

§

impl<T> Unpin for LineSegment<T>
where T: Unpin,

§

impl<T> UnwindSafe for LineSegment<T>
where T: UnwindSafe,

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.