1use std::ops::*;
2use serde::{Serialize, Deserialize};
3use crate::traits::*;
4
5#[repr(C)]
6#[derive(Copy, Clone, PartialEq, Debug, Default)]
7#[derive(Serialize, Deserialize)]
8pub struct Vec2<T> {
9 pub x: T,
10 pub y: T,
11}
12
13impl<T> Vec2<T> {
14 pub const fn new(x: T, y: T) -> Self {
15 Self { x, y, }
16 }
17}
18
19impl<T: Scalar> Vec2<T> {
20 #[inline(always)]
21 pub fn of(v: T) -> Self {
22 Self { x: v, y: v }
23 }
24
25 #[inline(always)]
26 pub fn min(self, other: Self) -> Self {
27 Self {
28 x: self.x.min(other.x),
29 y: self.y.min(other.y),
30 }
31 }
32
33 #[inline(always)]
34 pub fn max(self, other: Self) -> Self {
35 Self {
36 x: self.x.max(other.x),
37 y: self.y.max(other.y),
38 }
39 }
40
41 #[inline(always)]
42 pub fn min_component(self) -> T {
43 self.x.min(self.y)
44 }
45
46 #[inline(always)]
47 pub fn max_component(self) -> T {
48 self.y.max(self.y)
49 }
50}
51
52impl<T: Number> Vec2<T> {
53 pub const ZERO: Self = Self::new(T::ZERO, T::ZERO);
54 pub const X: Self = Self::new(T::ONE, T::ZERO);
55 pub const Y: Self = Self::new(T::ZERO, T::ONE);
56
57 #[inline(always)]
58 pub fn dot(self, other: Self) -> T {
59 self.x * other.x + self.y * other.y
60 }
61
62 #[inline(always)]
63 pub fn len2(self) -> T {
64 self.dot(self)
65 }
66
67 #[inline(always)]
68 pub fn distance2(self, other: Self) -> T {
69 (self - other).len2()
70 }
71
72 #[inline(always)]
73 pub fn reflect(self, other: Self) -> Self {
74 self - other * T::TWO * self.dot(other)
75 }
76}
77
78impl<T: Signed> Vec2<T> {
79 #[inline(always)]
80 pub fn abs(self) -> Self {
81 Self {
82 x: self.x.abs(),
83 y: self.y.abs(),
84 }
85 }
86}
87
88impl<T: Float> Vec2<T> {
89 #[inline(always)]
90 pub fn len(self) -> T {
91 self.len2().sqrt()
92 }
93
94 #[inline(always)]
95 pub fn distance(self, other: Self) -> T {
96 self.distance2(other).sqrt()
97 }
98
99 #[inline(always)]
100 pub fn normalized(self) -> Self {
101 self * (T::ONE / self.len())
102 }
103}
104
105impl<T: Number> Add for Vec2<T> {
106 type Output = Self;
107
108 #[inline(always)]
109 fn add(self, other: Self) -> Self::Output {
110 Self {
111 x: self.x + other.x,
112 y: self.y + other.y,
113 }
114 }
115}
116
117impl<T: Number> AddAssign for Vec2<T> {
118 #[inline(always)]
119 fn add_assign(&mut self, other: Self) {
120 self.x += other.x;
121 self.y += other.y;
122 }
123}
124
125impl<T: Number> Sub for Vec2<T> {
126 type Output = Self;
127
128 #[inline(always)]
129 fn sub(self, other: Self) -> Self::Output {
130 Self {
131 x: self.x - other.x,
132 y: self.y - other.y,
133 }
134 }
135}
136
137impl<T: Number> SubAssign for Vec2<T> {
138 #[inline(always)]
139 fn sub_assign(&mut self, other: Self) {
140 self.x -= other.x;
141 self.y -= other.y;
142 }
143}
144
145impl<T: Number> Mul for Vec2<T> {
146 type Output = Self;
147
148 #[inline(always)]
149 fn mul(self, other: Self) -> Self::Output {
150 Self {
151 x: self.x * other.x,
152 y: self.y * other.y,
153 }
154 }
155}
156
157impl<T: Number> MulAssign for Vec2<T> {
158 fn mul_assign(&mut self, other: Self) {
159 self.x *= other.x;
160 self.y *= other.y;
161 }
162}
163
164impl<T: Number> Mul<T> for Vec2<T> {
165 type Output = Self;
166
167 #[inline(always)]
168 fn mul(self, other: T) -> Self::Output {
169 Self {
170 x: self.x * other,
171 y: self.y * other,
172 }
173 }
174}
175
176impl<T: Number> MulAssign<T> for Vec2<T> {
177 #[inline(always)]
178 fn mul_assign(&mut self, other: T) {
179 self.x *= other;
180 self.y *= other;
181 }
182}
183
184impl<T: Number> Div for Vec2<T> {
185 type Output = Self;
186
187 #[inline(always)]
188 fn div(self, other: Self) -> Self {
189 Self {
190 x: self.x / other.x,
191 y: self.y / other.y,
192 }
193 }
194}
195
196impl<T: Number> DivAssign for Vec2<T> {
197 #[inline(always)]
198 fn div_assign(&mut self, other: Self) {
199 self.x /= other.x;
200 self.y /= other.y;
201 }
202}
203
204impl<T: Number> Div<T> for Vec2<T> {
205 type Output = Self;
206
207 #[inline(always)]
208 fn div(self, other: T) -> Self {
209 Self {
210 x: self.x / other,
211 y: self.y / other,
212 }
213 }
214}
215
216impl<T: Number> DivAssign<T> for Vec2<T> {
217 #[inline(always)]
218 fn div_assign(&mut self, other: T) {
219 self.x /= other;
220 self.y /= other;
221 }
222}
223
224impl<T: Signed> Neg for Vec2<T> {
225 type Output = Self;
226
227 #[inline(always)]
228 fn neg(self) -> Self::Output {
229 Self {
230 x: -self.x,
231 y: -self.y,
232 }
233 }
234}
235
236pub type Vec2b = Vec2<bool>;
237pub type Vec2i = Vec2<i32>;
238pub type Vec2u = Vec2<u32>;
239pub type Vec2f = Vec2<f32>;
240pub type Vec2d = Vec2<f64>;