-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathmath.rs
210 lines (172 loc) · 4.59 KB
/
math.rs
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
use std::{
f32::consts::PI,
fmt::Display,
ops::{Add, AddAssign, Mul, Sub, SubAssign},
};
#[derive(Default, Debug, Copy, Clone, PartialEq)]
pub struct Vec2D {
pub x: f32,
pub y: f32,
}
#[derive(Default, Debug, Copy, Clone, PartialEq)]
pub struct Angle {
pub radians: f32,
}
impl Angle {
pub fn from_radians(radians: f32) -> Self {
Self { radians }
}
pub fn from_degrees(degrees: f32) -> Self {
Self {
radians: degrees * PI / 180.0,
}
}
pub fn cos(&self) -> f32 {
self.radians.cos()
}
pub fn sin(&self) -> f32 {
self.radians.sin()
}
}
impl Mul<f32> for Angle {
type Output = Angle;
fn mul(self, rhs: f32) -> Self::Output {
Angle::from_radians(self.radians * rhs)
}
}
impl Vec2D {
pub fn zero() -> Self {
Self { x: 0.0, y: 0.0 }
}
pub fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
pub fn norm(&self) -> f32 {
(self.x * self.x + self.y * self.y).sqrt()
}
pub fn norm2(&self) -> f32 {
self.x * self.x + self.y * self.y
}
/**
* Get the angle of the vector.
* Angle of 0 is the positive x-axis.
* Angle of PI/2 is the positive y-axis.
*/
pub fn angle(&self) -> Angle {
Angle::from_radians(self.y.atan2(self.x))
}
/**
* Create a vector from an angle.
* Angle of 0 is the positive x-axis.
* Angle of PI/2 is the positive y-axis.
*/
pub fn from_angle(angle: Angle) -> Vec2D {
Vec2D::new(angle.cos(), angle.sin())
}
pub fn snapped_vector_15deg(&self) -> Vec2D {
let current_angle = (self.y / self.x).atan();
let current_norm2 = self.norm2();
let new_angle = (current_angle / 0.261_799_4).round() * 0.261_799_4;
let (a, b) = if new_angle.abs() < PI / 4.0
// 45°
{
let b = (current_norm2 / ((PI / 2.0 - new_angle).tan().powi(2) + 1.0)).sqrt();
let a = (current_norm2 - b * b).sqrt();
(a, b)
} else {
let a = (current_norm2 / (new_angle.tan().powi(2) + 1.0)).sqrt();
let b = (current_norm2 - a * a).sqrt();
(a, b)
};
if self.x >= 0.0 && self.y >= 0.0 {
Vec2D::new(a, b)
} else if self.x < 0.0 && self.y >= 0.0 {
Vec2D::new(-a, b)
} else if self.x >= 0.0 && self.y < 0.0 {
Vec2D::new(a, -b)
} else {
Vec2D::new(-a, -b)
}
}
pub fn is_zero(&self) -> bool {
self.x.abs() < f32::EPSILON && self.y.abs() < f32::EPSILON
}
}
impl Add for Vec2D {
type Output = Vec2D;
fn add(self, rhs: Self) -> Self::Output {
Self::Output {
x: self.x + rhs.x,
y: self.y + rhs.y,
}
}
}
impl AddAssign for Vec2D {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl Sub for Vec2D {
type Output = Vec2D;
fn sub(self, rhs: Self) -> Self::Output {
Self::Output {
x: self.x - rhs.x,
y: self.y - rhs.y,
}
}
}
impl SubAssign for Vec2D {
fn sub_assign(&mut self, rhs: Self) {
*self = *self - rhs;
}
}
impl Mul<f32> for Vec2D {
type Output = Vec2D;
fn mul(self, rhs: f32) -> Self::Output {
Vec2D::new(self.x * rhs, self.y * rhs)
}
}
impl Display for Vec2D {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "({},{})", self.x, self.y)
}
}
pub fn rect_ensure_positive_size(pos: Vec2D, size: Vec2D) -> (Vec2D, Vec2D) {
let (pos_x, size_x) = if size.x > 0.0 {
(pos.x, size.x)
} else {
((pos.x + size.x), size.x.abs())
};
let (pos_y, size_y) = if size.y > 0.0 {
(pos.y, size.y)
} else {
((pos.y + size.y), size.y.abs())
};
(Vec2D::new(pos_x, pos_y), Vec2D::new(size_x, size_y))
}
pub fn rect_ensure_in_bounds(rect: (Vec2D, Vec2D), bounds: (Vec2D, Vec2D)) -> (Vec2D, Vec2D) {
let (mut pos, mut size) = rect;
if pos.x < bounds.0.x {
pos.x = bounds.0.x;
size.x -= bounds.0.x - pos.x;
}
if pos.y < bounds.0.y {
pos.y = bounds.0.y;
size.y -= bounds.0.y - pos.y;
}
if pos.x + size.x > bounds.1.x {
size.x = bounds.1.x - pos.x;
}
if pos.y + size.y > bounds.1.y {
size.y = bounds.1.y - pos.y;
}
(pos, size)
}
pub fn rect_round(rect: (Vec2D, Vec2D)) -> (Vec2D, Vec2D) {
let (mut pos, mut size) = rect;
pos.x = pos.x.round();
pos.y = pos.y.round();
size.x = size.x.round();
size.y = size.y.round();
(pos, size)
}