aboutsummaryrefslogtreecommitdiff
path: root/games/rstnode/rst-client/src/graphics/vector2.rs
blob: 5204e77fa4430c9e5b8cc92c910d00e918f6e3f2 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use std::fmt::{self, Display, Formatter};
use std::ops::{Add, Mul, Sub, SubAssign};

/// Just a vector
#[derive(Debug, Default, Copy, Clone, PartialEq, PartialOrd)]
pub struct Vector2 {
    pub x: f32,
    pub y: f32,
}

impl Display for Vector2 {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "[{}, {}]", self.x, self.y)
    }
}

impl Vector2 {
    pub fn new(x: f32, y: f32) -> Self {
        Self { x, y }
    }

    pub fn abs(self) -> Self {
        Self {
            x: self.x.abs(),
            y: self.y.abs(),
        }
    }
}

impl Sub for Vector2 {
    type Output = Vector2;

    fn sub(self, o: Vector2) -> Self::Output {
        Vector2 {
            x: self.x - o.x,
            y: self.y - o.y,
        }
    }
}

impl SubAssign for Vector2 {
    fn sub_assign(&mut self, o: Self) {
        *self = Self {
            x: self.x - o.x,
            y: self.y - o.y,
        }
    }
}

impl Add for Vector2 {
    type Output = Vector2;

    fn add(self, o: Vector2) -> Self::Output {
        Vector2 {
            x: self.x + o.x,
            y: self.y + o.y,
        }
    }
}

impl Mul for Vector2 {
    type Output = Vector2;

    fn mul(self, o: Vector2) -> Self::Output {
        Vector2 {
            x: self.x * o.x,
            y: self.y * o.y,
        }
    }
}

impl From<Vector2> for mint::Point2<f32> {
    fn from(v: Vector2) -> Self {
        [v.x, v.y].into()
    }
}

impl From<mint::Point2<f32>> for Vector2 {
    fn from(v: mint::Point2<f32>) -> Self {
        Self { x: v.x, y: v.y }
    }
}