ultron_core/
util.rs

1use nalgebra::Point2;
2use std::fmt;
3
4pub fn cast_point(point: Point2<i32>) -> Point2<usize> {
5    Point2::new(point.x.try_into().unwrap(), point.y.try_into().unwrap())
6}
7
8pub fn normalize_points<T>(p1: Point2<T>, p2: Point2<T>) -> (Point2<T>, Point2<T>)
9where
10    T: Copy + Clone + PartialEq + fmt::Debug + Ord + 'static,
11{
12    let min_x = p1.x.min(p2.x);
13    let min_y = p1.y.min(p2.y);
14    let max_x = p1.x.max(p2.x);
15    let max_y = p1.y.max(p2.y);
16    (Point2::new(min_x, min_y), Point2::new(max_x, max_y))
17}
18
19#[allow(clippy::if_same_then_else)]
20#[allow(clippy::comparison_chain)]
21/// order the point by y (top is first) and then by x (left is first)
22pub fn reorder_top_down_left_right<T>(p1: Point2<T>, p2: Point2<T>) -> (Point2<T>, Point2<T>)
23where
24    T: Copy + Clone + PartialEq + fmt::Debug + Ord + 'static,
25{
26    if p1.y == p2.y {
27        if p1.x == p2.x {
28            (p1, p2)
29        } else if p1.x < p2.x {
30            (p1, p2)
31        } else if p2.x < p1.x {
32            (p2, p1)
33        } else {
34            unreachable!()
35        }
36    } else if p1.y < p2.y {
37        (p1, p2)
38    } else if p2.y < p1.y {
39        (p2, p1)
40    } else {
41        unreachable!()
42    }
43}