1mod vec2d;
4mod vec2i;
5
6use std::ops::{Add, Div, Mul, Sub};
7
8pub use self::vec2d::*;
9pub use self::vec2i::*;
10
11#[derive(Default, Debug, Eq, PartialEq, Clone, Copy)]
18pub struct Vec2<T> {
19 pub x: T,
20 pub y: T,
21}
22
23#[macro_export]
60macro_rules! vec2 {
61 () => {
62 $crate::vec::Vec2::default()
63 };
64 ($val:expr) => {
65 $crate::vec::Vec2 { x: $val, y: $val }
66 };
67 ($x:expr, $y:expr) => {
68 $crate::vec::Vec2 { x: $x, y: $y }
69 };
70}
71
72pub trait VectorOperations<T> {
74 fn length(&self) -> f64;
76
77 fn normalize(&self) -> Vec2<f64>;
79
80 fn dot(&self, rhs: &Self) -> T;
82}
83
84impl<T> Add for Vec2<T>
85where
86 T: Add<Output = T>,
87{
88 type Output = Self;
89
90 fn add(self, rhs: Self) -> Self::Output {
91 Self {
92 x: self.x + rhs.x,
93 y: self.y + rhs.y,
94 }
95 }
96}
97
98impl<T> Sub for Vec2<T>
99where
100 T: Sub<Output = T>,
101{
102 type Output = Self;
103
104 fn sub(self, rhs: Self) -> Self::Output {
105 Self {
106 x: self.x - rhs.x,
107 y: self.y - rhs.y,
108 }
109 }
110}
111
112impl<T> Mul<T> for Vec2<T>
113where
114 T: Mul<Output = T> + Clone + Copy,
115{
116 type Output = Self;
117
118 fn mul(self, rhs: T) -> Self::Output {
119 Self {
120 x: self.x * rhs,
121 y: self.y * rhs,
122 }
123 }
124}
125
126impl<T> Div<T> for Vec2<T>
127where
128 T: Div<Output = T> + Clone + Copy,
129{
130 type Output = Self;
131
132 fn div(self, rhs: T) -> Self::Output {
133 Self {
134 x: self.x / rhs,
135 y: self.y / rhs,
136 }
137 }
138}
139
140#[cfg(test)]
141mod tests {
142 use super::*;
143
144 #[test]
145 fn test_vec_add() {
146 assert_eq!(
147 Vec2 { x: 1, y: 0 } + Vec2 { x: 2, y: 1 },
148 Vec2 { x: 3, y: 1 }
149 );
150 }
151
152 #[test]
153 fn test_vec_sub() {
154 assert_eq!(
155 Vec2 { x: 1, y: 0 } - Vec2 { x: 2, y: 1 },
156 Vec2 { x: -1, y: -1 }
157 );
158 }
159
160 #[test]
161 fn test_vec_scale() {
162 assert_eq!(Vec2 { x: 2, y: 5 } * 3, Vec2 { x: 6, y: 15 });
163 }
164
165 #[test]
166 fn test_vec_div() {
167 assert_eq!(Vec2 { x: 2.0, y: 5.0 } / 2.0, Vec2 { x: 1.0, y: 2.5 });
168 }
169
170 #[test]
171 fn test_vec2_macro_empty() {
172 assert_eq!(vec2![], Vec2 { x: 0, y: 0 });
173 }
174
175 #[test]
176 fn test_vec2_macro_splat() {
177 assert_eq!(vec2![42], Vec2 { x: 42, y: 42 });
178 }
179
180 #[test]
181 fn test_vec2_macro_double_args() {
182 assert_eq!(vec2![3, 4], Vec2 { x: 3, y: 4 });
183 }
184}