mutils/geom/position/
line_position.rs

1use crate::geom::position::HorizontalPosition;
2use crate::geom::position::VerticalPosition;
3use crate::geom::PointPosition;
4
5#[derive(Copy, Clone, PartialEq, Debug)]
6pub struct LinePosition(pub PointPosition, pub PointPosition);
7
8impl LinePosition {
9    pub fn new(start: PointPosition, end: PointPosition) -> Self {
10        Self(start, end)
11    }
12
13    pub fn start(self) -> PointPosition {
14        self.0
15    }
16
17    pub fn end(self) -> PointPosition {
18        self.1
19    }
20
21    pub fn is_entirely_left(self) -> bool {
22        self.start().is_left() && self.end().is_left()
23    }
24
25    pub fn is_entirely_right(self) -> bool {
26        self.start().is_right() && self.end().is_right()
27    }
28
29    pub fn is_entirely_above(self) -> bool {
30        self.start().is_above() && self.end().is_above()
31    }
32
33    pub fn is_entirely_below(self) -> bool {
34        self.start().is_below() && self.end().is_below()
35    }
36
37    pub fn is_on_same_horizontal(self) -> bool {
38        self.start().horizontal() == self.end().horizontal()
39    }
40
41    pub fn is_on_same_vertical(self) -> bool {
42        self.start().vertical() == self.end().vertical()
43    }
44
45    pub fn is_within_same_space(self) -> bool {
46        self.start() == self.end()
47    }
48
49    pub fn is_entirely_inside(self) -> bool {
50        self.start().is_entirely_inside() && self.end().is_entirely_inside()
51    }
52
53    /**
54     * Returns true, if it's guaranteed this does not clip the inside.
55     * `is_entirely_outside` is not the opposite of `is_entirely_inside`.
56     */
57    pub fn is_entirely_outside(self) -> bool {
58        // Is either fully above, or fully below.
59        if self.is_on_same_horizontal() && self.start().horizontal() != HorizontalPosition::Inside {
60            return true;
61        }
62
63        // Is either fully on the left, or fully on the right.
64        if self.is_on_same_vertical() && self.start().vertical() != VerticalPosition::Inside {
65            return true;
66        }
67
68        false
69    }
70}