windows_numerics/
vector2.rs1use super::*;
2
3impl Vector2 {
4 pub fn new(X: f32, Y: f32) -> Self {
5 Self { X, Y }
6 }
7 pub fn zero() -> Self {
8 Self { X: 0f32, Y: 0f32 }
9 }
10 pub fn one() -> Self {
11 Self { X: 1f32, Y: 1f32 }
12 }
13 pub fn unit_x() -> Self {
14 Self { X: 1.0, Y: 0.0 }
15 }
16 pub fn unit_y() -> Self {
17 Self { X: 0.0, Y: 1.0 }
18 }
19 pub fn dot(&self, rhs: &Self) -> f32 {
20 self.X * rhs.X + self.Y * rhs.Y
21 }
22 pub fn length_squared(&self) -> f32 {
23 self.dot(self)
24 }
25 #[cfg(feature = "std")]
26 pub fn length(&self) -> f32 {
27 self.length_squared().sqrt()
28 }
29 #[cfg(feature = "std")]
30 pub fn distance(&self, value: &Self) -> f32 {
31 (self - value).length()
32 }
33 pub fn distance_squared(&self, value: &Self) -> f32 {
34 (self - value).length_squared()
35 }
36 #[cfg(feature = "std")]
37 pub fn normalize(&self) -> Self {
38 self / self.length()
39 }
40
41 fn impl_add(&self, rhs: &Self) -> Self {
42 Self {
43 X: self.X + rhs.X,
44 Y: self.Y + rhs.Y,
45 }
46 }
47 fn impl_sub(&self, rhs: &Self) -> Self {
48 Self {
49 X: self.X - rhs.X,
50 Y: self.Y - rhs.Y,
51 }
52 }
53 fn impl_div(&self, rhs: &Self) -> Self {
54 Self {
55 X: self.X / rhs.X,
56 Y: self.Y / rhs.Y,
57 }
58 }
59 fn impl_div_f32(&self, rhs: f32) -> Self {
60 Self {
61 X: self.X / rhs,
62 Y: self.Y / rhs,
63 }
64 }
65 fn impl_mul(&self, rhs: &Self) -> Self {
66 Self {
67 X: self.X * rhs.X,
68 Y: self.Y * rhs.Y,
69 }
70 }
71 fn impl_mul_f32(&self, rhs: f32) -> Self {
72 Self {
73 X: self.X * rhs,
74 Y: self.Y * rhs,
75 }
76 }
77}
78
79impl core::ops::Add<Vector2> for Vector2 {
80 type Output = Vector2;
81 fn add(self, rhs: Vector2) -> Vector2 {
82 self.impl_add(&rhs)
83 }
84}
85impl core::ops::Add<&Vector2> for Vector2 {
86 type Output = Vector2;
87 fn add(self, rhs: &Vector2) -> Vector2 {
88 self.impl_add(rhs)
89 }
90}
91impl core::ops::Add<Vector2> for &Vector2 {
92 type Output = Vector2;
93 fn add(self, rhs: Vector2) -> Vector2 {
94 self.impl_add(&rhs)
95 }
96}
97impl core::ops::Add<&Vector2> for &Vector2 {
98 type Output = Vector2;
99 fn add(self, rhs: &Vector2) -> Vector2 {
100 self.impl_add(rhs)
101 }
102}
103impl core::ops::Sub<Vector2> for Vector2 {
104 type Output = Vector2;
105 fn sub(self, rhs: Vector2) -> Vector2 {
106 self.impl_sub(&rhs)
107 }
108}
109impl core::ops::Sub<&Vector2> for Vector2 {
110 type Output = Vector2;
111 fn sub(self, rhs: &Vector2) -> Vector2 {
112 self.impl_sub(rhs)
113 }
114}
115impl core::ops::Sub<Vector2> for &Vector2 {
116 type Output = Vector2;
117 fn sub(self, rhs: Vector2) -> Vector2 {
118 self.impl_sub(&rhs)
119 }
120}
121impl core::ops::Sub<&Vector2> for &Vector2 {
122 type Output = Vector2;
123 fn sub(self, rhs: &Vector2) -> Vector2 {
124 self.impl_sub(rhs)
125 }
126}
127impl core::ops::Div<Vector2> for Vector2 {
128 type Output = Vector2;
129 fn div(self, rhs: Vector2) -> Vector2 {
130 self.impl_div(&rhs)
131 }
132}
133impl core::ops::Div<&Vector2> for Vector2 {
134 type Output = Vector2;
135 fn div(self, rhs: &Vector2) -> Vector2 {
136 self.impl_div(rhs)
137 }
138}
139impl core::ops::Div<Vector2> for &Vector2 {
140 type Output = Vector2;
141 fn div(self, rhs: Vector2) -> Vector2 {
142 self.impl_div(&rhs)
143 }
144}
145impl core::ops::Div<&Vector2> for &Vector2 {
146 type Output = Vector2;
147 fn div(self, rhs: &Vector2) -> Vector2 {
148 self.impl_div(rhs)
149 }
150}
151impl core::ops::Div<f32> for Vector2 {
152 type Output = Vector2;
153 fn div(self, rhs: f32) -> Vector2 {
154 self.impl_div_f32(rhs)
155 }
156}
157impl core::ops::Div<f32> for &Vector2 {
158 type Output = Vector2;
159 fn div(self, rhs: f32) -> Vector2 {
160 self.impl_div_f32(rhs)
161 }
162}
163impl core::ops::Mul<Vector2> for Vector2 {
164 type Output = Vector2;
165 fn mul(self, rhs: Vector2) -> Vector2 {
166 self.impl_mul(&rhs)
167 }
168}
169impl core::ops::Mul<&Vector2> for Vector2 {
170 type Output = Vector2;
171 fn mul(self, rhs: &Vector2) -> Vector2 {
172 self.impl_mul(rhs)
173 }
174}
175impl core::ops::Mul<Vector2> for &Vector2 {
176 type Output = Vector2;
177 fn mul(self, rhs: Vector2) -> Vector2 {
178 self.impl_mul(&rhs)
179 }
180}
181impl core::ops::Mul<&Vector2> for &Vector2 {
182 type Output = Vector2;
183 fn mul(self, rhs: &Vector2) -> Vector2 {
184 self.impl_mul(rhs)
185 }
186}
187impl core::ops::Mul<f32> for Vector2 {
188 type Output = Vector2;
189 fn mul(self, rhs: f32) -> Vector2 {
190 self.impl_mul_f32(rhs)
191 }
192}
193impl core::ops::Mul<f32> for &Vector2 {
194 type Output = Vector2;
195 fn mul(self, rhs: f32) -> Vector2 {
196 self.impl_mul_f32(rhs)
197 }
198}