peace_performance/parse/
pos2.rs1use std::fmt;
2use std::ops;
3
4#[derive(Clone, Copy, Default, PartialEq)]
6pub struct Pos2 {
7 pub x: f32,
8 pub y: f32,
9}
10
11impl Pos2 {
12 #[inline]
13 pub fn zero() -> Self {
14 Self::default()
15 }
16
17 #[inline]
18 pub fn length_squared(&self) -> f32 {
19 self.dot(*self)
20 }
21
22 #[inline]
23 pub fn length(&self) -> f32 {
24 self.x.hypot(self.y)
25 }
26
27 #[inline]
28 pub fn dot(&self, other: Self) -> f32 {
29 self.x.mul_add(other.x, self.y * other.y)
30 }
31
32 #[inline]
33 pub fn distance(&self, other: Self) -> f32 {
34 (*self - other).length()
35 }
36
37 #[inline]
38 pub fn normalize(self) -> Pos2 {
39 self / self.length()
40 }
41}
42
43impl ops::Add<Pos2> for Pos2 {
44 type Output = Self;
45
46 #[inline]
47 fn add(self, rhs: Self) -> Self::Output {
48 Self {
49 x: self.x + rhs.x,
50 y: self.y + rhs.y,
51 }
52 }
53}
54
55impl ops::Sub<Pos2> for Pos2 {
56 type Output = Self;
57
58 #[inline]
59 fn sub(self, rhs: Self) -> Self::Output {
60 Self {
61 x: self.x - rhs.x,
62 y: self.y - rhs.y,
63 }
64 }
65}
66
67impl ops::Mul<f32> for Pos2 {
68 type Output = Self;
69
70 #[inline]
71 fn mul(self, rhs: f32) -> Self::Output {
72 Self {
73 x: self.x * rhs,
74 y: self.y * rhs,
75 }
76 }
77}
78
79impl ops::Div<f32> for Pos2 {
80 type Output = Self;
81
82 #[inline]
83 fn div(self, rhs: f32) -> Self::Output {
84 Self {
85 x: self.x / rhs,
86 y: self.y / rhs,
87 }
88 }
89}
90
91impl ops::AddAssign for Pos2 {
92 fn add_assign(&mut self, other: Self) {
93 *self = Self {
94 x: self.x + other.x,
95 y: self.y + other.y,
96 };
97 }
98}
99
100impl fmt::Display for Pos2 {
101 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
102 write!(f, "{:?}", self)
103 }
104}
105
106impl fmt::Debug for Pos2 {
107 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
108 write!(f, "({}, {})", self.x, self.y)
109 }
110}