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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use std::ops::Add;
use std::ops::AddAssign;
use std::ops::Div;
use std::ops::DivAssign;
use std::ops::Mul;
use std::ops::MulAssign;
use std::ops::Rem;
use std::ops::Sub;
use std::ops::SubAssign;

use super::internal::Num;

pub trait NumTuple<N: Num>:
    Add<Output = Self>
    + AddAssign
    + Sub<Output = Self>
    + SubAssign
    + Mul<Output = Self>
    + MulAssign
    + Div<Output = Self>
    + DivAssign
    + Rem<Output = Self>
    + Copy
    + PartialEq
    + Sized
{
    fn new(first: N, second: N) -> Self;

    fn first(&self) -> N;
    fn second(&self) -> N;

    fn set_first(&mut self, n: N);
    fn set_second(&mut self, n: N);
    fn set(&mut self, first: N, second: N);
    fn get(&mut self) -> (N, N);

    fn min(self, other: Self) -> Self {
        Self::new(
            self.first().min(other.first()),
            self.second().min(other.second()),
        )
    }

    fn max(self, other: Self) -> Self {
        Self::new(
            self.first().max(other.first()),
            self.second().max(other.second()),
        )
    }

    fn max_from_zero(self, other: Self) -> Self {
        Self::new(
            self.first().max(other.first()),
            self.second().max(other.second()),
        )
    }

    fn abs(&self) -> Self {
        Self::new(self.first().abs(), self.second().abs())
    }

    fn is_outside(self, xy1: Self, xy2: Self) -> bool {
        !self.is_inside(xy1, xy2)
    }

    fn is_inside(self, xy1: Self, xy2: Self) -> bool {
        let (x_min, y_min) = xy1.min(xy2).get();
        let (x_max, y_max) = xy1.max(xy2).get();

        let x = self.first();
        let y = self.second();

        x_min <= x && x <= x_max && y_min <= y && y <= y_max
    }
}