aboutsummaryrefslogtreecommitdiff
path: root/src/core/vector2.rs
blob: 0e0165e8b599a6e97e6b8c9f7ab5a7c27f2efcb6 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//! Implements 2d vectors
//!
//! This is implemented generictly with types that fit in the Number trait
use crate::{Float, Number};
use std::ops::{Sub, Add, Mul, Div};
use std::fmt;
use std::cmp::min;

#[derive(Clone, Copy)]
pub struct Vector2<T: Number> {
    pub x: T,
    pub y: T,
}

pub type Vector2f = Vector2<Float>;
pub type Vector2i = Vector2<i32>;


impl<T: Number> Vector2<T> {
    pub fn new(initial: T) -> Vector2<T> {
        Vector2 { x: initial, y: initial }
    }

    pub fn new_xy(x: T, y: T) -> Vector2<T> {
        Vector2 { x, y }
    }
}

impl<T: Number> Sub for Vector2<T> {
    type Output = Self;
    fn sub(self, op: Self) -> Self::Output {
        Self::new_xy(
            self.x - op.x,
            self.y - op.y,
        )
    }
}

impl<T: Number> Add for Vector2<T> {
    type Output = Self;
    fn add(self, op: Self) -> Self::Output {
        Self::new_xy(
            self.x + op.x,
            self.y + op.y,
        )
    }
}

impl<T: Number> Mul for Vector2<T> {
    type Output = Self;
    fn mul(self, op: Self) -> Self::Output {
        Self::new_xy(
            self.x * op.x,
            self.y * op.y,
        )
    }
}

impl<T: Number> Mul<T> for Vector2<T> {
    type Output = Self;
    fn mul(self, op: T) -> Self::Output {
        Self::new_xy(
            self.x * op,
            self.y * op,
        )
    }
}

impl<T: Number> Div for Vector2<T> {
    type Output = Self;
    fn div(self, op: Self) -> Self::Output {
        Self::new_xy(
            self.x / op.x,
            self.y / op.y,
        )
    }
}

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

impl From<Vector2i> for Vector2f {
    fn from(v: Vector2i) -> Self {
        Self {
            x: v.x as Float,
            y: v.y as Float,
        }
    }
}

impl From<Vector2f> for Vector2i {
    fn from(v: Vector2f) -> Self {
        Self {
            x: v.x as i32,
            y: v.y as i32,
        }
    }
}

impl Vector2i {
    pub const ZERO: Self = Vector2i {x: 0, y: 0};

    pub fn cap(&self, x: i32, y: i32) -> Self {
        Self::new_xy(
            min(self.x, x),
            min(self.y, y),
            )
    }
}

impl Vector2f {
    pub fn len(&self) -> Float {
        (self.x*self.x + self.y*self.y).sqrt()
    }

    pub fn ceil(&self) -> Self {
        Self::new_xy(
            self.x.ceil(),
            self.y.ceil()
            )
    }

    pub fn floor(&self) -> Self {
        Self::new_xy(
            self.x.floor(),
            self.y.floor()
            )
    }
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn new_vec2() {
        let v = Vector2::new_xy(2.0, 10.0);

        assert!(v.x == 2.0 && v.y == 10.0);

        let v = Vector2::new(3);

        assert!(v.x == 3 && v.y == 3);
    }

    #[test]
    fn sub_vec2() {
        let v1 = Vector2::new_xy(10, 11);
        let v2 = Vector2::new_xy(2, 3);

        let v3 = v1-v2;
        assert!(v3.x == 8 && v3.y == 8);
    }

    #[test]
    fn add_vec2() {
        let v1 = Vector2::new_xy(10, 11);
        let v2 = Vector2::new_xy(2, 3);

        let v3 = v1+v2;
        assert!(v3.x == 12 && v3.y == 14);
    }
}