orbtk_utils/
relative_direction.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use crate::Point;

/// Describes the relative position of the object.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum RelativeDir {
    Top,
    TopRight,
    Right,
    BottomRight,
    Bottom,
    BottomLeft,
    Left,
    TopLeft,
}

impl RelativeDir {
    /// Computes the start and end points of a line that crosses a given field in the `self` direction
    pub fn cross(&self, width: f64, height: f64) -> (Point, Point) {
        let (start, end);
        let mid_width = width / 2.0;
        let mid_height = height / 2.0;
        match self {
            RelativeDir::Top => {
                start = Point::new(mid_width, height);
                end = Point::new(mid_width, 0.0);
            }
            RelativeDir::TopRight => {
                start = Point::new(0.0, height);
                end = Point::new(width, 0.0);
            }
            RelativeDir::Right => {
                start = Point::new(0.0, mid_height);
                end = Point::new(width, mid_height);
            }
            RelativeDir::BottomRight => {
                start = Point::new(0.0, 0.0);
                end = Point::new(width, height);
            }
            RelativeDir::Bottom => {
                start = Point::new(mid_width, 0.0);
                end = Point::new(mid_width, height);
            }
            RelativeDir::BottomLeft => {
                start = Point::new(width, 0.0);
                end = Point::new(0.0, height);
            }
            RelativeDir::Left => {
                start = Point::new(width, mid_height);
                end = Point::new(0.0, mid_height);
            }
            RelativeDir::TopLeft => {
                start = Point::new(width, height);
                end = Point::new(0.0, 0.0);
            }
        }
        (start, end)
    }
}