aboutsummaryrefslogtreecommitdiff
path: root/evc/src/vec2.rs
blob: b1dd1b468ae67a3af5f22784df398d4849aa8b29 (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
63
64
65
use crate::ser::{Ser, Sink, Source};

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Vec2 {
    pub x: isize,
    pub y: isize,
}
impl Vec2 {
    pub const ZERO: Vec2 = Vec2 { x: 0, y: 0 };
    pub fn downscale(&self, f: isize) -> Self {
        Self {
            x: self.x / f,
            y: self.y / f,
        }
    }
}

impl Ser for Vec2 {
    fn write(&self, sink: &mut impl std::io::Write) -> anyhow::Result<()> {
        sink.put((self.x, self.y))
    }

    fn read(source: &mut impl std::io::Read) -> anyhow::Result<Self> {
        let (x, y) = source.get()?;
        Ok(Vec2 { x, y })
    }
}

impl std::ops::Add for Vec2 {
    type Output = Vec2;
    #[inline]
    fn add(self, rhs: Self) -> Self::Output {
        Vec2 {
            x: self.x + rhs.x,
            y: self.y + rhs.y,
        }
    }
}
impl std::ops::Sub for Vec2 {
    type Output = Vec2;
    #[inline]
    fn sub(self, rhs: Self) -> Self::Output {
        Vec2 {
            x: self.x - rhs.x,
            y: self.y - rhs.y,
        }
    }
}
impl std::ops::Mul for Vec2 {
    type Output = Vec2;
    #[inline]
    fn mul(self, rhs: Self) -> Self::Output {
        Vec2 {
            x: self.x * rhs.x,
            y: self.y * rhs.y,
        }
    }
}

impl From<(isize, isize)> for Vec2 {
    #[inline]
    fn from((x, y): (isize, isize)) -> Self {
        Vec2 { x, y }
    }
}