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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};

use crate::Number;

/// An offset.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Offset(pub Number, pub Number);

impl Offset {
    // Check if the offset is zero.
    #[inline]
    pub fn is_zero(&self) -> bool {
        self.0 == 0.0 && self.1 == 0.0
    }
}

macro_rules! extremum(
    ($self:expr, $other:expr, $function:ident) => (
        if $self.is_nan() {
            $other
        } else if $other.is_nan() {
            $self
        } else {
            $self.$function($other)
        }
    )
);

impl Offset {
    /// Create an undefined offset.
    #[inline]
    pub fn undefined() -> Self {
        Self(Number::NAN, Number::NAN)
    }

    /// Return the coordinate-wise maximum ignoring undefined values.
    pub fn max(&self, other: Self) -> Self {
        let x = extremum!(self.0, other.0, max);
        let y = extremum!(self.1, other.1, max);
        Self(x, y)
    }

    /// Return the coordinate-wise minimum ignoring undefined values.
    pub fn min(&self, other: Self) -> Self {
        let x = extremum!(self.0, other.0, min);
        let y = extremum!(self.1, other.1, min);
        Self(x, y)
    }
}

impl From<Offset> for (Number, Number) {
    #[inline]
    fn from(offset: Offset) -> Self {
        (offset.0, offset.1)
    }
}

macro_rules! implement(
    (($x:ty, $y:ty)) => (
        impl From<($x, $y)> for Offset {
            #[inline]
            fn from((x, y): ($x, $y)) -> Self {
                Offset(Number::from(x), Number::from(y))
            }
        }
    );
    ($z:ty) => (
        impl From<$z> for Offset {
            #[inline]
            fn from(z: $z) -> Self {
                Offset(Number::from(z), Number::from(z))
            }
        }

        impl Div<$z> for Offset {
            type Output = Offset;

            #[inline]
            fn div(mut self, other: $z) -> Offset {
                self /= other;
                self
            }
        }

        impl DivAssign<$z> for Offset {
            #[inline]
            fn div_assign(&mut self, other: $z) {
                self.0 /= Number::from(other);
                self.1 /= Number::from(other);
            }
        }

        impl Mul<$z> for Offset {
            type Output = Offset;

            #[inline]
            fn mul(mut self, other: $z) -> Offset {
                self *= other;
                self
            }
        }

        impl MulAssign<$z> for Offset {
            #[inline]
            fn mul_assign(&mut self, other: $z) {
                self.0 *= Number::from(other);
                self.1 *= Number::from(other);
            }
        }
    );
);

implement!(f32);
implement!(i16);
implement!((f32, f32));
implement!((i16, i16));

impl<T> Add<T> for Offset
where
    T: Into<Offset>,
{
    type Output = Offset;

    #[inline]
    fn add(mut self, other: T) -> Offset {
        self += other;
        self
    }
}

impl<T> AddAssign<T> for Offset
where
    T: Into<Offset>,
{
    #[inline]
    fn add_assign(&mut self, other: T) {
        let Offset(x, y) = other.into();
        self.0 += x;
        self.1 += y;
    }
}

impl Neg for Offset {
    type Output = Offset;

    #[inline]
    fn neg(mut self) -> Offset {
        self.0 = -self.0;
        self.1 = -self.1;
        self
    }
}

impl<T> Sub<T> for Offset
where
    T: Into<Offset>,
{
    type Output = Offset;

    #[inline]
    fn sub(mut self, other: T) -> Offset {
        self -= other;
        self
    }
}

impl<T> SubAssign<T> for Offset
where
    T: Into<Offset>,
{
    #[inline]
    fn sub_assign(&mut self, other: T) {
        let Offset(x, y) = other.into();
        self.0 -= x;
        self.1 -= y;
    }
}