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
use std::{fmt, ops::{Add, Sub, Neg}};

use rand::{Rng, thread_rng};

use crate::Rotation;

/// A 2D vector on the lighthouse display.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Delta {
    pub dx: i32,
    pub dy: i32,
}

impl Delta {
    /// The empty delta.
    pub const ZERO: Self = Self::new(0, 0);

    /// The vector pointing one pixel to the left.
    pub const LEFT:  Self = Self::new(-1,  0);
    /// The vector pointing one pixel up.
    pub const UP:    Self = Self::new( 0, -1);
    /// The vector pointing one pixel to the right.
    pub const RIGHT: Self = Self::new( 1,  0);
    /// The vector pointing one pixel down.
    pub const DOWN:  Self = Self::new( 0,  1);

    /// Creates a new vector.
    pub const fn new(dx: i32, dy: i32) -> Self {
        Self { dx, dy }
    }

    /// Randomly one of the four cardinal directions with the given rng.
    pub fn random_cardinal_with(rng: &mut impl Rng) -> Self {
        Rotation::random_cardinal_with(rng) * Self::RIGHT
    }

    /// Randomly one of the four cardinal directions with the thread-local rng.
    pub fn random_cardinal() -> Self {
        Self::random_cardinal_with(&mut thread_rng())
    }
}

impl fmt::Display for Delta {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "({}, {})", self.dx, self.dy)
    }
}

impl Add for Delta {
    type Output = Delta;

    fn add(self, rhs: Self) -> Self {
        Self::new(self.dx + rhs.dx, self.dy + rhs.dy)
    }
}

impl Sub for Delta {
    type Output = Delta;

    fn sub(self, rhs: Self) -> Self {
        Self::new(self.dx - rhs.dx, self.dy - rhs.dy)
    }
}

impl Neg for Delta {
    type Output = Delta;

    fn neg(self) -> Self {
        Self::new(-self.dx, -self.dy)
    }
}