ascii_forge/
math.rs

1use std::ops::{Add, AddAssign, Sub, SubAssign};
2
3/// A 2d Vector that has no math, is only used as a pretty version of a tuple of u16s
4/// Can be made from (u16, u16).
5/// Using a single u16.into() will create a vec2 where both values are the same.
6#[derive(Default, Debug, Eq, PartialEq, PartialOrd, Ord, Copy, Clone)]
7pub struct Vec2 {
8    pub x: u16,
9    pub y: u16,
10}
11
12impl From<(u16, u16)> for Vec2 {
13    fn from(value: (u16, u16)) -> Self {
14        vec2(value.0, value.1)
15    }
16}
17
18impl From<u16> for Vec2 {
19    fn from(value: u16) -> Self {
20        vec2(value, value)
21    }
22}
23
24impl<V: Into<Vec2>> Add<V> for Vec2 {
25    type Output = Vec2;
26    fn add(mut self, rhs: V) -> Self::Output {
27        let rhs = rhs.into();
28        self.x += rhs.x;
29        self.y += rhs.y;
30        self
31    }
32}
33
34impl<V: Into<Vec2>> AddAssign<V> for Vec2 {
35    fn add_assign(&mut self, rhs: V) {
36        let rhs = rhs.into();
37        self.x += rhs.x;
38        self.y += rhs.y;
39    }
40}
41
42impl<V: Into<Vec2>> Sub<V> for Vec2 {
43    type Output = Vec2;
44    fn sub(mut self, rhs: V) -> Self::Output {
45        let rhs = rhs.into();
46        self.x -= rhs.x;
47        self.y -= rhs.y;
48        self
49    }
50}
51
52impl<V: Into<Vec2>> SubAssign<V> for Vec2 {
53    fn sub_assign(&mut self, rhs: V) {
54        let rhs = rhs.into();
55        self.x -= rhs.x;
56        self.y -= rhs.y;
57    }
58}
59
60/// Creates a Vec2 from the given inputs.
61pub fn vec2(x: u16, y: u16) -> Vec2 {
62    Vec2 { x, y }
63}