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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
use std::ops;

#[derive(Default, Debug, Copy, Clone, Ord, PartialOrd, PartialEq, Eq, Hash)]
pub struct X(u16);
#[derive(Default, Debug, Copy, Clone, Ord, PartialOrd, PartialEq, Eq, Hash)]
pub struct Y(u16);
#[derive(Default, Debug, Copy, Clone, Ord, PartialOrd, PartialEq, Eq, Hash)]
pub struct Window {
    pub position: Point,
    pub size: Point,
}
#[derive(Default, Debug, Copy, Clone, Ord, PartialOrd, PartialEq, Eq, Hash)]
pub struct Point {
    pub x: X,
    pub y: Y,
}

pub fn x(x: u16) -> X {
    X(x)
}
pub fn y(y: u16) -> Y {
    Y(y)
}
pub fn point(x: X, y: Y) -> Point {
    Point { x, y }
}
pub fn window(position: Point, size: Point) -> Window {
    Window { position, size }
}

impl Point {
    pub fn area(&self) -> u32 {
        self.x.0 as u32 * self.y.0 as u32
    }
    pub fn contains(&self, o: &Point) -> bool {
        self.x > o.x && self.y > o.y
    }
    pub fn point_at_idx(&self, idx: u32) -> Option<Point> {
        if idx >= self.area() {
            None
        } else {
            self.x.point_at_idx(idx)
        }
    }
    pub fn idx_at_point(&self, p: Point) -> Option<u32> {
        if p.y >= self.y || p.x >= self.x {
            None
        } else {
            self.x.idx_at_point(p)
        }
    }
}
impl Window {
    pub fn crop(&self, w: &Window) -> Window {
        let pos = self.position + w.position;
        let win = window(pos, w.size);

        &win & self
    }
}
impl X {
    #[inline]
    pub fn max(o1: X, o2: X) -> X {
        if o1 > o2 {
            o1
        } else {
            o2
        }
    }
    #[inline]
    pub fn min(o1: X, o2: X) -> X {
        if o1 > o2 {
            o2
        } else {
            o1
        }
    }
    #[inline]
    pub fn to_primitive(&self) -> u16 {
        self.0
    }
    pub fn point_at_idx(&self, idx: u32) -> Option<Point> {
        if self.0 == 0 {
            None
        } else {
            let px = x((idx % self.0 as u32) as u16);
            let py = y((idx / self.0 as u32) as u16);
            Some(point(px, py))
        }
    }
    pub fn idx_at_point(&self, p: Point) -> Option<u32> {
        if p.x >= *self {
            None
        } else {
            let w = self.0 as u32;
            let x = p.x.0 as u32;
            let y = p.y.0 as u32;
            Some(y * w + x)
        }
    }
}
impl Y {
    #[inline]
    pub fn max(o1: Y, o2: Y) -> Y {
        if o1 > o2 {
            o1
        } else {
            o2
        }
    }
    #[inline]
    pub fn min(o1: Y, o2: Y) -> Y {
        if o1 > o2 {
            o2
        } else {
            o1
        }
    }
    #[inline]
    pub fn to_primitive(&self) -> u16 {
        self.0
    }
}

impl ops::Div<u16> for X {
    type Output = X;
    fn div(self, o: u16) -> Self::Output {
        X(self.0 / o)
    }
}
impl ops::Div<u16> for Y {
    type Output = Y;
    fn div(self, o: u16) -> Self::Output {
        Y(self.0 / o)
    }
}

impl ops::Mul<u16> for X {
    type Output = X;
    fn mul(self, o: u16) -> Self::Output {
        X(self.0.saturating_mul(o))
    }
}

impl ops::Mul<u16> for Y {
    type Output = Y;
    fn mul(self, o: u16) -> Self::Output {
        Y(self.0.saturating_mul(o))
    }
}

impl ops::BitAnd<&Window> for &Window {
    type Output = Window;
    fn bitand(self, o: &Window) -> Self::Output {
        let pos = point(
            X::max(self.position.x, o.position.x),
            Y::max(self.position.y, o.position.y),
        );

        let w = window(
            pos,
            point(
                X::min(
                    self.position.x + self.size.x - pos.x,
                    o.position.x + o.size.x - pos.x,
                ),
                Y::min(
                    self.position.y + self.size.y - pos.y,
                    o.position.y + o.size.y - pos.y,
                ),
            ),
        );

        trace!("intersect windows: {:?} and {:?} => {:?}", self, o, w);

        w
    }
}
impl ops::BitAnd<Point> for Window {
    type Output = Option<Point>;
    fn bitand(self, o: Point) -> Self::Output {
        if self.position.x <= o.x
            && self.position.y <= o.y
            && self.position.x + self.size.x >= o.x
            && self.position.y + self.size.y >= o.y
        {
            Some(o)
        } else {
            None
        }
    }
}

impl ops::Mul<Y> for X {
    type Output = Point;
    fn mul(self, o: Y) -> Self::Output {
        point(self, o)
    }
}
impl ops::Mul<X> for Y {
    type Output = Point;
    fn mul(self, o: X) -> Self::Output {
        point(o, self)
    }
}
impl ops::Div<X> for Point {
    type Output = Y;
    fn div(self, o: X) -> Self::Output {
        Y((self.x.0 as u32 * self.y.0 as u32 / o.0 as u32) as u16)
    }
}
impl ops::Div<Y> for Point {
    type Output = X;
    fn div(self, o: Y) -> Self::Output {
        X((self.x.0 as u32 * self.y.0 as u32 / o.0 as u32) as u16)
    }
}
impl ops::SubAssign for X {
    fn sub_assign(&mut self, o: Self) {
        self.0 = self.0.saturating_sub(o.0)
    }
}
impl ops::AddAssign for X {
    fn add_assign(&mut self, o: Self) {
        self.0 = self.0.saturating_add(o.0)
    }
}
impl ops::SubAssign for Y {
    fn sub_assign(&mut self, o: Self) {
        self.0 = self.0.saturating_sub(o.0)
    }
}
impl ops::AddAssign for Y {
    fn add_assign(&mut self, o: Self) {
        self.0 = self.0.saturating_add(o.0)
    }
}
impl ops::Sub<X> for X {
    type Output = X;
    fn sub(self, o: X) -> Self::Output {
        X(self.0.saturating_sub(o.0))
    }
}
impl ops::Add<X> for X {
    type Output = X;
    fn add(self, o: X) -> Self::Output {
        X(self.0.saturating_add(o.0))
    }
}
impl ops::Sub<Y> for Y {
    type Output = Y;
    fn sub(self, o: Y) -> Self::Output {
        Y(self.0.saturating_sub(o.0))
    }
}
impl ops::Add<Y> for Y {
    type Output = Y;
    fn add(self, o: Y) -> Self::Output {
        Y(self.0.saturating_add(o.0))
    }
}
impl ops::SubAssign for Point {
    fn sub_assign(&mut self, o: Self) {
        *self = *self - o
    }
}
impl ops::AddAssign for Point {
    fn add_assign(&mut self, o: Self) {
        *self = *self + o
    }
}
impl ops::Sub<Point> for Point {
    type Output = Point;
    fn sub(self, o: Point) -> Self::Output {
        point(self.x - o.x, self.y - o.y)
    }
}
impl ops::Add<Point> for Point {
    type Output = Point;
    fn add(self, pos: Point) -> Self::Output {
        point(self.x + pos.x, self.y + pos.y)
    }
}
impl ops::Sub<X> for Point {
    type Output = Point;
    fn sub(self, o: X) -> Self::Output {
        point(self.x - o, self.y)
    }
}
impl ops::Add<X> for Point {
    type Output = Point;
    fn add(self, o: X) -> Self::Output {
        point(self.x + o, self.y)
    }
}
impl ops::Sub<Y> for Point {
    type Output = Point;
    fn sub(self, o: Y) -> Self::Output {
        point(self.x, self.y - o)
    }
}
impl ops::Add<Y> for Point {
    type Output = Point;
    fn add(self, o: Y) -> Self::Output {
        point(self.x, self.y + o)
    }
}

#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Position {
    pub left: u16,
    pub top: u16,
}
impl Position {
    pub fn add(&self, operand: Self) -> Self {
        Self {
            left: self.left.saturating_add(operand.left),
            top: self.top.saturating_add(operand.top),
        }
    }
}

#[test]
fn test_x_times_y() {
    assert_eq!(X(1) * Y(2), point(X(1), Y(2)));
}
#[test]
fn test_y_times_x() {
    assert_eq!(Y(2) * X(1), point(X(1), Y(2)));
}
#[test]
fn test_to_primitive() {
    assert_eq!(X(3).to_primitive(), 3);
    assert_eq!(Y(2).to_primitive(), 2);
}

#[test]
fn test_window_crop() {
    let op = window(point(x(0), y(0)), point(x(10000), y(30000)));
    let crop = window(point(x(10), y(3)), point(x(100), y(300))).crop(&op);
    assert_eq!(crop.position.x, x(10));
    assert_eq!(crop.position.y, y(3));
    assert_eq!(crop.size.x, x(100));
    assert_eq!(crop.size.y, y(300));

    let op = window(point(x(10), y(3)), point(x(100), y(300)));
    let crop = window(point(x(0), y(0)), point(x(10000), y(30000))).crop(&op);
    assert_eq!(crop.position.x, x(10));
    assert_eq!(crop.position.y, y(3));
    assert_eq!(crop.size.x, x(100));
    assert_eq!(crop.size.y, y(300));
}

#[test]
fn test_size_contains() {
    let p = point(x(3), y(2));
    assert_eq!(p.contains(&point(x(0), y(0))), true);
    assert_eq!(p.contains(&point(x(2), y(1))), true);
    assert_eq!(p.contains(&point(x(3), y(1))), false);
    assert_eq!(p.contains(&point(x(2), y(2))), false);
}

#[test]
fn test_idx_at_point() {
    let p = point(x(3), y(2));
    assert_eq!(p.idx_at_point(point(x(0), y(0))), Some(0));
    assert_eq!(p.idx_at_point(point(x(1), y(0))), Some(1));
    assert_eq!(p.idx_at_point(point(x(1), y(1))), Some(4));
    assert_eq!(p.idx_at_point(point(x(2), y(1))), Some(5));
    assert_eq!(p.idx_at_point(point(x(3), y(1))), None);
    assert_eq!(p.idx_at_point(point(x(1), y(2))), None);

    assert_eq!(p.point_at_idx(0), Some(point(x(0), y(0))));
    assert_eq!(p.point_at_idx(5), Some(point(x(2), y(1))));
    assert_eq!(p.point_at_idx(6), None);
}