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
use super::*;
impl<T: Num + Copy> mat3<T> {
/// Construct a uniform scale matrix.
///
/// # Examples
/// ```
/// # use batbox_la::*;
/// let matrix = mat3::scale_uniform(2);
/// assert_eq!(matrix * vec3(1, 2, 1), vec3(2, 4, 1));
/// ```
pub fn scale_uniform(factor: T) -> Self {
Self::scale(vec2(factor, factor))
}
/// Construct matrix that performs uniform scaling around a specified point
pub fn scale_uniform_around(p: vec2<T>, scale: T) -> Self {
mat3::translate(p) * mat3::scale_uniform(scale) * mat3::translate(-p)
}
/// Construct matrix that performs scaling around a specified point
pub fn scale_around(p: vec2<T>, scale: vec2<T>) -> Self {
mat3::translate(p) * mat3::scale(scale) * mat3::translate(-p)
}
/// Construct a scale matrix.
///
/// # Examples
/// ```
/// # use batbox_la::*;
/// let matrix = mat3::scale(vec2(1, 2));
/// assert_eq!(matrix * vec3(1, 2, 1), vec3(1, 4, 1));
/// ```
pub fn scale(factor: vec2<T>) -> Self {
let mut result = Self::zero();
result[(0, 0)] = factor.x;
result[(1, 1)] = factor.y;
result[(2, 2)] = T::ONE;
result
}
/// Construct a translation matrix.
///
/// # Examples
/// ```
/// # use batbox_la::*;
/// let matrix = mat3::translate(vec2(3, 2));
/// assert_eq!(matrix * vec3(1, 2, 1), vec3(4, 4, 1));
/// ```
pub fn translate(dv: vec2<T>) -> Self {
let mut result = Self::identity();
result[(0, 2)] = dv.x;
result[(1, 2)] = dv.y;
result
}
}
impl<T: Float> mat3<T> {
/// Construct rotational matrix
pub fn rotate(angle: Angle<T>) -> Self {
let mut result = Self::identity();
let (sin, cos) = angle.sin_cos();
result[(0, 0)] = cos;
result[(0, 1)] = -sin;
result[(1, 0)] = sin;
result[(1, 1)] = cos;
result
}
/// Construct matrix that performs rotation around a specified point
pub fn rotate_around(p: vec2<T>, angle: Angle<T>) -> Self {
Self::translate(p) * Self::rotate(angle) * Self::translate(-p)
}
}