summaryrefslogtreecommitdiff
path: root/2019-worms/src/geometry/vec.rs
blob: 375a0f9ff748f305e0240a85f2a7f0d252b0b887 (plain)
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
use std::ops::*;

#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq)]
pub struct Vec2d {
    pub x: i8,
    pub y: i8,
}

impl Vec2d {
    pub const fn new(x: i8, y: i8) -> Vec2d {
        Vec2d { x, y }
    }
    pub fn magnitude_squared(&self) -> i8 {
        self.x
            .saturating_pow(2)
            .saturating_add(self.y.saturating_pow(2))
    }
}

impl Add for Vec2d {
    type Output = Self;

    fn add(self, other: Self) -> Self {
        Vec2d {
            x: self.x.saturating_add(other.x),
            y: self.y.saturating_add(other.y),
        }
    }
}

impl Sub for Vec2d {
    type Output = Self;

    fn sub(self, other: Self) -> Self {
        Vec2d {
            x: self.x.saturating_sub(other.x),
            y: self.y.saturating_sub(other.y),
        }
    }
}

impl Mul<i8> for Vec2d {
    type Output = Self;

    fn mul(self, other: i8) -> Self {
        Vec2d {
            x: self.x.saturating_mul(other),
            y: self.y.saturating_mul(other),
        }
    }
}

impl Neg for Vec2d {
    type Output = Self;

    fn neg(self) -> Self {
        Vec2d {
            x: -self.x,
            y: -self.y,
        }
    }
}