manifold3d_types/math/
vec2.rs

1use manifold3d_sys::ManifoldVec2;
2use std::ops::{Add, Sub};
3
4#[derive(Debug, Clone, Copy, PartialEq)]
5pub struct Vec2 {
6    pub x: f64,
7    pub y: f64,
8}
9
10impl Vec2 {
11    pub fn new(x: f64, y: f64) -> Self {
12        Self { x, y }
13    }
14}
15
16impl From<ManifoldVec2> for Vec2 {
17    fn from(value: ManifoldVec2) -> Self {
18        Vec2 {
19            x: value.x,
20            y: value.y,
21        }
22    }
23}
24
25impl Add for Vec2 {
26    type Output = Self;
27
28    fn add(self, rhs: Self) -> Self::Output {
29        Vec2::new(self.x + rhs.x, self.y + rhs.y)
30    }
31}
32
33impl<T> Add<T> for Vec2
34where
35    f64: From<T>,
36    T: num_traits::ToPrimitive,
37{
38    type Output = Self;
39
40    fn add(self, rhs: T) -> Self::Output {
41        let value = f64::from(rhs);
42        Vec2::new(self.x + value, self.y + value)
43    }
44}
45
46impl Sub for Vec2 {
47    type Output = Self;
48
49    fn sub(self, rhs: Self) -> Self::Output {
50        Vec2::new(self.x - rhs.x, self.y - rhs.y)
51    }
52}
53
54impl<T> Sub<T> for Vec2
55where
56    f64: From<T>,
57    T: num_traits::ToPrimitive,
58{
59    type Output = Self;
60
61    fn sub(self, rhs: T) -> Self::Output {
62        let value = f64::from(rhs);
63        Vec2::new(self.x - value, self.y - value)
64    }
65}
66
67#[cfg(feature = "nalgebra_interop")]
68impl From<nalgebra::Vector2<f64>> for Vec2 {
69    fn from(value: nalgebra::Vector2<f64>) -> Self {
70        Vec2 {
71            x: value.x,
72            y: value.y,
73        }
74    }
75}