1use std::{fmt, ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}};
2use num_traits::{Float, NumCast, Zero};
3use crate::base::Orientation;
4
5#[derive(Copy, Clone, Default, PartialEq, Eq, Hash)]
6#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7pub struct Vec2<T> {
8 pub x: T,
9 pub y: T,
10}
11
12impl<T: Zero> Vec2<T> {
13 pub fn zero() -> Self {
14 Self {
15 x: T::zero(),
16 y: T::zero(),
17 }
18 }
19
20 pub fn is_zero(&self) -> bool {
21 self.x.is_zero() && self.y.is_zero()
22 }
23}
24
25#[macro_export]
26macro_rules! vec2 {
27 ($x:expr, $y:expr) => {
28 Vec2::new($x, $y)
29 };
30}
31
32impl<T: Copy> From<Vec2<T>> for (T, T) {
33 #[inline]
34 fn from(v: Vec2<T>) -> Self {
35 (v.x, v.y)
36 }
37}
38
39impl<T: Copy> From<&Vec2<T>> for (T, T) {
40 #[inline]
41 fn from(v: &Vec2<T>) -> Self {
42 (v.x, v.y)
43 }
44}
45
46impl<T> Vec2<T> {
47 pub fn new(x: T, y: T) -> Self {
56 Vec2 { x, y }
57 }
58}
59
60impl<T: Copy + Zero + PartialOrd + Sub<Output = T>> Vec2<T> {
61 pub fn norm1(&self) -> T {
70 let xabs = if self.x < T::zero() {
71 T::zero() - self.x
72 } else {
73 self.x
74 };
75
76 let yabs = if self.y < T::zero() {
77 T::zero() - self.y
78 } else {
79 self.y
80 };
81
82 xabs + yabs
83 }
84}
85
86impl<T: Copy + Zero + PartialOrd + Mul<Output = T> + Sub<Output = T>> Vec2<T> {
87 pub fn orientation_of(&self, other: Self) -> Orientation {
105 let p = self.cross_prod(other);
106
107 if p < T::zero() {
108 Orientation::ClockWise
109 } else if p > T::zero() {
110 Orientation::CounterClockWise
111 } else {
112 Orientation::Straight
113 }
114 }
115}
116
117impl<T: Copy + Mul<Output = T> + Sub<Output = T>> Vec2<T> {
118 #[inline]
132 pub fn cross_prod(&self, other: Self) -> T {
133 self.x * other.y - other.x * self.y
134 }
135}
136
137impl<T: Copy + Mul<Output = T> + Add<Output = T>> Vec2<T> {
138 #[inline]
147 pub fn norm2_squared(&self) -> T {
148 self.x * self.x + self.y * self.y
149 }
150
151 pub fn dot(&self, other: Self) -> T {
164 self.x * other.x + self.y * other.y
165 }
166}
167
168
169impl<T> Vec2<T>
170where
171 T: Copy + Float + Mul<Output = T> + Add<Output = T>,
172{
173 #[inline]
186 pub fn norm2(&self) -> T {
187 (self.norm2_squared()).sqrt()
188 }
189
190 #[inline]
195 pub fn normalized(&self) -> Self {
196 *self / self.norm2()
197 }
198}
199
200impl<T: Copy + NumCast> Vec2<T> {
201 #[inline]
216 pub fn length<F: Float>(&self) -> F {
217 let x = F::from(self.x).unwrap();
218 let y = F::from(self.y).unwrap();
219
220 x.hypot(y)
221 }
222}
223
224impl<T> Vec2<T>
225where
226 T: Copy + NumCast,
227{
228 #[inline]
230 pub fn to_float<F>(&self) -> Vec2<F>
231 where
232 F: Float + NumCast,
233 {
234 Vec2 {
235 x: F::from(self.x).unwrap(),
236 y: F::from(self.y).unwrap(),
237 }
238 }
239}
240
241impl<T> Add for Vec2<T>
242where
243 T: Add<Output = T>,
244{
245 type Output = Self;
246
247 #[inline]
248 fn add(self, rhs: Self) -> Self {
249 Vec2 {
250 x: self.x + rhs.x,
251 y: self.y + rhs.y,
252 }
253 }
254}
255
256impl<T> AddAssign for Vec2<T>
257where
258 T: AddAssign,
259{
260 #[inline]
261 fn add_assign(&mut self, rhs: Self) {
262 self.x += rhs.x;
263 self.y += rhs.y;
264 }
265}
266
267impl<T> Sub for Vec2<T>
269where
270 T: Sub<Output = T>,
271{
272 type Output = Self;
273
274 #[inline]
275 fn sub(self, rhs: Self) -> Self {
276 Vec2 {
277 x: self.x - rhs.x,
278 y: self.y - rhs.y,
279 }
280 }
281}
282
283impl<T> SubAssign for Vec2<T>
284where
285 T: SubAssign,
286{
287 #[inline]
288 fn sub_assign(&mut self, rhs: Self) {
289 self.x -= rhs.x;
290 self.y -= rhs.y;
291 }
292}
293
294impl<T> Neg for Vec2<T>
295where
296 T: Neg<Output = T>,
297{
298 type Output = Self;
299
300 #[inline]
301 fn neg(self) -> Self {
302 Vec2 {
303 x: -self.x,
304 y: -self.y,
305 }
306 }
307}
308
309impl<T, M> Mul<M> for Vec2<T>
311where
312 T: Mul<M, Output = T>,
313 M: Copy,
314{
315 type Output = Self;
316
317 fn mul(self, rhs: M) -> Self {
318 Vec2 {
319 x: self.x * rhs,
320 y: self.y * rhs,
321 }
322 }
323}
324
325impl<T, M> MulAssign<M> for Vec2<T>
327where
328 T: Copy + MulAssign<M>,
329 M: Copy,
330{
331 #[inline]
332 fn mul_assign(&mut self, rhs: M) {
333 self.x *= rhs;
334 self.y *= rhs;
335 }
336}
337
338impl<T, D> Div<D> for Vec2<T>
340where
341 T: Copy + Div<D, Output = T>,
342 D: Copy,
343{
344 type Output = Self;
345
346 #[inline]
347 fn div(self, rhs: D) -> Self {
348 Vec2 {
349 x: self.x / rhs,
350 y: self.y / rhs,
351 }
352 }
353}
354
355impl<T, D> DivAssign<D> for Vec2<T>
357where
358 T: Copy + DivAssign<D>,
359 D: Copy,
360{
361 #[inline]
362 fn div_assign(&mut self, rhs: D) {
363 self.x /= rhs;
364 self.y /= rhs;
365 }
366}
367
368impl<T> std::iter::Sum for Vec2<T>
371where
372 T: Zero + Add<Output = T>,
373{
374 fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
375 iter.fold(Self::zero(), |acc, v| acc + v)
376 }
377}
378
379
380impl<T: Copy> From<(T, T)> for Vec2<T> {
381 #[inline]
382 fn from(coords: (T, T)) -> Self {
383 Vec2::new(coords.0, coords.1)
384 }
385}
386
387impl<'a, T: Copy> From<&'a (T, T)> for Vec2<T> {
388 #[inline]
389 fn from(coords: &'a (T, T)) -> Self {
390 Vec2::new(coords.0, coords.1)
391 }
392}
393
394impl<'a, T: Copy> From<&'a Vec2<T>> for Vec2<T> {
395 #[inline]
396 fn from(v: &'a Vec2<T>) -> Self {
397 Vec2::new(v.x, v.y)
398 }
399}
400
401impl<T: Copy> From<[T; 2]> for Vec2<T> {
402 #[inline]
403 fn from(coords: [T; 2]) -> Self {
404 Vec2::new(coords[0], coords[1])
405 }
406}
407
408
409impl<T> fmt::Debug for Vec2<T>
410where
411 T: fmt::Debug,
412{
413 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
414 write!(f, "({:?}, {:?})", &self.x, &self.y)
415 }
416}
417
418impl<T> fmt::Display for Vec2<T>
419where
420 T: fmt::Display + Copy,
421{
422 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
423 write!(f, "({}, {})", self.x, self.y)
424 }
425}
426