1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
use crate::math::Rect;
use alga::general::{ClosedAdd, ClosedSub};
use nalgebra::{RealField, Scalar};
use num_traits::One;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::convert::{From, Into};
use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign};
use std::{fmt, mem};
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize, Eq, Hash)]
pub struct Vec2<T: Scalar + Copy> {
pub x: T,
pub y: T,
}
impl<T: Scalar + Copy> Vec2<T> {
pub fn new(x: T, y: T) -> Self {
Self { x, y }
}
pub fn length(&self) -> T
where
T: RealField,
{
(self.x * self.x + self.y * self.y).sqrt()
}
pub fn rotated_90_clockwise(mut self) -> Vec2<T>
where
T: One + Neg<Output = T> + MulAssign,
{
mem::swap(&mut self.x, &mut self.y);
self.y *= -T::one();
self
}
pub fn rotated_90_counterclockwise(mut self) -> Vec2<T>
where
T: One + Neg<Output = T> + MulAssign,
{
mem::swap(&mut self.x, &mut self.y);
self.x *= -T::one();
self
}
}
// This is sad, but also sadly necessary :/
impl<T> Into<raylib::ffi::Vector2> for Vec2<T>
where
T: Into<f32> + Scalar + Copy,
{
fn into(self) -> raylib::ffi::Vector2 {
raylib::ffi::Vector2 {
x: self.x.into(),
y: self.y.into(),
}
}
}
impl<T> From<raylib::ffi::Vector2> for Vec2<T>
where
T: From<f32> + Scalar + Copy,
{
fn from(v: raylib::ffi::Vector2) -> Self {
Self {
x: T::from(v.x),
y: T::from(v.y),
}
}
}
impl<T: Scalar + Copy> Into<raylib::math::Vector2> for Vec2<T>
where
T: Into<f32>,
{
fn into(self) -> raylib::math::Vector2 {
raylib::math::Vector2 {
x: self.x.into(),
y: self.y.into(),
}
}
}
impl<T: Scalar + Copy> From<raylib::math::Vector2> for Vec2<T>
where
T: From<f32>,
{
fn from(v: raylib::math::Vector2) -> Self {
Self {
x: T::from(v.x),
y: T::from(v.y),
}
}
}
// Begin mathematical operators -----------------------------------------------
// Addition
impl<T: Scalar + ClosedAdd + Copy> Add for Vec2<T> {
type Output = Self;
fn add(self, rhs: Self) -> Self {
Vec2::new(self.x + rhs.x, self.y + rhs.y)
}
}
impl<T: Scalar + ClosedAdd + Copy> Add<(T, T)> for Vec2<T> {
type Output = Self;
fn add(self, (x, y): (T, T)) -> Self {
Vec2::new(self.x + x, self.y + y)
}
}
impl<T: Scalar + ClosedAdd + Copy> Add<T> for Vec2<T> {
type Output = Self;
fn add(self, rhs: T) -> Self {
Vec2::new(self.x + rhs, self.y + rhs)
}
}
impl<T: Scalar + AddAssign + Copy> AddAssign for Vec2<T> {
fn add_assign(&mut self, rhs: Self) {
self.x += rhs.x;
self.y += rhs.y;
}
}
impl<T: Scalar + AddAssign + Copy> AddAssign<(T, T)> for Vec2<T> {
fn add_assign(&mut self, (x, y): (T, T)) {
self.x += x;
self.y += y;
}
}
// Subtraction
impl<T: Scalar + ClosedSub + Copy> Sub for Vec2<T> {
type Output = Self;
fn sub(self, rhs: Self) -> Self {
Vec2::new(self.x - rhs.x, self.y - rhs.y)
}
}
impl<T: Scalar + ClosedSub + Copy> Sub<(T, T)> for Vec2<T> {
type Output = Self;
fn sub(self, (x, y): (T, T)) -> Self {
Vec2::new(self.x - x, self.y - y)
}
}
impl<T: Scalar + ClosedSub + Copy> Sub<T> for Vec2<T> {
type Output = Self;
fn sub(self, rhs: T) -> Self {
Vec2::new(self.x - rhs, self.y - rhs)
}
}
impl<T: Scalar + SubAssign + Copy> SubAssign for Vec2<T> {
fn sub_assign(&mut self, rhs: Self) {
self.x -= rhs.x;
self.y -= rhs.y;
}
}
impl<T: Scalar + SubAssign + Copy> SubAssign<(T, T)> for Vec2<T> {
fn sub_assign(&mut self, (x, y): (T, T)) {
self.x -= x;
self.y -= y;
}
}
// Scalar multiplication
impl<T: Scalar + Add<Output = T> + Mul<Output = T> + Copy> Mul for Vec2<T> {
type Output = T;
fn mul(self, rhs: Self) -> T {
self.x * rhs.x + self.y * rhs.y
}
}
impl<T: Scalar + Mul<Output = T> + Copy> Mul<T> for Vec2<T> {
type Output = Self;
fn mul(self, rhs: T) -> Self {
Vec2::new(self.x * rhs, self.y * rhs)
}
}
impl<T: Scalar + Div<Output = T> + Copy> Div<T> for Vec2<T> {
type Output = Self;
fn div(self, rhs: T) -> Self {
Vec2::new(self.x / rhs, self.y / rhs)
}
}
// End of mathematical operators ----------------------------------------------
// By default, the coordinates are first compared by their y-coordinates, then
// their x-coordinates
impl<T: fmt::Debug> PartialOrd for Vec2<T>
where
T: PartialOrd + Copy + 'static,
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
match self.y.partial_cmp(&other.y) {
Some(Ordering::Equal) | None => self.x.partial_cmp(&other.x),
y_order => y_order,
}
}
}
impl<T: fmt::Debug> Ord for Vec2<T>
where
T: Ord + Copy + 'static,
{
fn cmp(&self, other: &Self) -> Ordering {
match self.y.cmp(&other.y) {
Ordering::Equal => self.x.cmp(&other.x),
y_order => y_order,
}
}
}
// Helper function to determine the absolute positive difference between two
// Values, which don't have to be signed.
fn difference_abs<T>(a: T, b: T) -> T
where
T: ClosedSub + PartialOrd,
{
if a > b {
a - b
} else {
b - a
}
}
// Helper function that removes all points inside the vector that are not
// contained inside the optional limit Rect
fn retain_inside_limits<T: 'static>(items: Vec<Vec2<T>>, limits: Option<Rect<T>>) -> Vec<Vec2<T>>
where
T: PartialOrd + std::fmt::Debug + Copy + Add<Output = T>,
{
// Fast return in case there are no limits
if limits.is_none() {
return items;
}
let limits = limits.unwrap();
// Retain only items that are within the bounds of the limits rect
items
.into_iter()
.filter(|v| {
v.x >= limits.x
&& v.x <= limits.x + limits.w
&& v.y >= limits.y
&& v.y <= limits.y + limits.h
})
.collect()
}
|