-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeom.rs
287 lines (244 loc) · 6.33 KB
/
geom.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
use std::ops;
/// A 2-dimensional point.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Point {
pub x: f32,
pub y: f32,
}
impl Point {
/// Constructs a 2-dimensional point.
#[inline]
pub fn new(x: f32, y: f32) -> Point {
Point { x, y }
}
/// Computes the dot product between two points (treated as vectors).
#[inline]
pub fn dot(self, other: Point) -> f32 {
self.x * other.x + self.y * other.y
}
/// Considering the two given points as 3-dimensional vectors lying in the XY-plane, finds the
/// z-coordinate of their cross product.
#[inline]
pub fn cross(self, other: Point) -> f32 {
self.x * other.y - self.y * other.x
}
/// Computes the distance between two points.
#[inline]
pub fn distance(self, other: Point) -> f32 {
(other - self).length()
}
/// Computes the distance of a point from the origin.
#[inline]
pub fn length(self) -> f32 {
self.dot(self).sqrt()
}
/// Finds the vector with the same direction and a length of 1.
#[inline]
pub fn normalized(self) -> Point {
(1.0 / self.length()) * self
}
/// Linearly interpolates between two points by the parameter `t`.
#[inline]
pub fn lerp(t: f32, a: Point, b: Point) -> Point {
(1.0 - t) * a + t * b
}
/// Finds the componentwise minimum of two points.
#[inline]
pub fn min(self, other: Point) -> Point {
Point {
x: self.x.min(other.x),
y: self.y.min(other.y),
}
}
/// Finds the componentwise maximum of two points.
#[inline]
pub fn max(self, other: Point) -> Point {
Point {
x: self.x.max(other.x),
y: self.y.max(other.y),
}
}
}
impl ops::Add for Point {
type Output = Point;
#[inline]
fn add(self, rhs: Point) -> Point {
Point {
x: self.x + rhs.x,
y: self.y + rhs.y,
}
}
}
impl ops::AddAssign for Point {
#[inline]
fn add_assign(&mut self, other: Point) {
*self = *self + other;
}
}
impl ops::Sub for Point {
type Output = Point;
#[inline]
fn sub(self, rhs: Point) -> Point {
Point {
x: self.x - rhs.x,
y: self.y - rhs.y,
}
}
}
impl ops::SubAssign for Point {
#[inline]
fn sub_assign(&mut self, other: Point) {
*self = *self - other;
}
}
impl ops::Mul<Point> for f32 {
type Output = Point;
#[inline]
fn mul(self, rhs: Point) -> Point {
Point {
x: self * rhs.x,
y: self * rhs.y,
}
}
}
impl ops::Mul<f32> for Point {
type Output = Point;
#[inline]
fn mul(self, rhs: f32) -> Point {
rhs * self
}
}
impl ops::MulAssign<f32> for Point {
#[inline]
fn mul_assign(&mut self, rhs: f32) {
*self = rhs * *self;
}
}
impl ops::Neg for Point {
type Output = Point;
#[inline]
fn neg(self) -> Point {
Point {
x: -self.x,
y: -self.y,
}
}
}
/// A 2-dimensional affine transformation.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Affine([f32; 6]);
impl Affine {
/// Constructs an affine transformation from a set of coefficients.
///
/// The coefficients are interpreted as the first two rows of a 3×3 affine transformation matrix
/// in row-major order.
#[inline]
pub fn new(coeffs: [f32; 6]) -> Affine {
Affine(coeffs)
}
/// Gets the coefficients of the transformation.
///
/// The coefficients are the first two rows of the corresponding 3×3 affine transformation
/// matrix in row-major order.
#[inline]
pub fn coeffs(self) -> [f32; 6] {
self.0
}
/// Constructs an identity transformation.
#[inline]
pub fn id() -> Affine {
Affine([1.0, 0.0, 0.0, 0.0, 1.0, 0.0])
}
/// Constructs a translation.
#[inline]
pub fn translate(x: f32, y: f32) -> Affine {
Affine([1.0, 0.0, x, 0.0, 1.0, y])
}
/// Constructs a uniform scaling.
#[inline]
pub fn scale(scale: f32) -> Affine {
Affine([scale, 0.0, 0.0, 0.0, scale, 0.0])
}
/// Constructs a rotation.
#[inline]
pub fn rotate(angle: f32) -> Affine {
let cos = angle.cos();
let sin = angle.sin();
Affine([cos, sin, 0.0, -sin, cos, 0.0])
}
// Gets the linear part of the affine transformation, i.e. without the translation.
#[inline]
pub fn linear(&self) -> Affine {
Affine([self.0[0], self.0[1], 0.0, self.0[3], self.0[4], 0.0])
}
}
impl ops::Mul<Affine> for Affine {
type Output = Affine;
#[inline]
fn mul(self, rhs: Affine) -> Affine {
Affine([
self.0[0] * rhs.0[0] + self.0[1] * rhs.0[3],
self.0[0] * rhs.0[1] + self.0[1] * rhs.0[4],
self.0[0] * rhs.0[2] + self.0[1] * rhs.0[5] + self.0[2],
self.0[3] * rhs.0[0] + self.0[4] * rhs.0[3],
self.0[3] * rhs.0[1] + self.0[4] * rhs.0[4],
self.0[3] * rhs.0[2] + self.0[4] * rhs.0[5] + self.0[5],
])
}
}
impl ops::MulAssign<Affine> for Affine {
#[inline]
fn mul_assign(&mut self, rhs: Affine) {
*self = *self * rhs;
}
}
impl ops::Mul<Point> for Affine {
type Output = Point;
#[inline]
fn mul(self, rhs: Point) -> Point {
Point {
x: self.0[0] * rhs.x + self.0[1] * rhs.y + self.0[2],
y: self.0[3] * rhs.x + self.0[4] * rhs.y + self.0[5],
}
}
}
impl ops::Mul<Affine> for Point {
type Output = Point;
#[inline]
fn mul(self, rhs: Affine) -> Point {
rhs * self
}
}
impl ops::MulAssign<Affine> for Point {
#[inline]
fn mul_assign(&mut self, rhs: Affine) {
*self = rhs * *self;
}
}
impl ops::Mul<Affine> for f32 {
type Output = Affine;
#[inline]
fn mul(self, rhs: Affine) -> Affine {
Affine([
self * rhs.0[0],
self * rhs.0[1],
self * rhs.0[2],
self * rhs.0[3],
self * rhs.0[4],
self * rhs.0[5],
])
}
}
impl ops::Mul<f32> for Affine {
type Output = Affine;
#[inline]
fn mul(self, rhs: f32) -> Affine {
rhs * self
}
}
impl ops::MulAssign<f32> for Affine {
#[inline]
fn mul_assign(&mut self, rhs: f32) {
*self = rhs * *self;
}
}