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
use num::{One, Zero};
use point::Point2D;
use rect::Rect;
use size::Size2D;
use std::ops::{Add, Mul, Sub};
#[derive(Clone, Copy)]
#[cfg_attr(feature = "plugins", derive(HeapSizeOf, Deserialize, Serialize))]
pub struct Matrix2D<T> {
m11: T, m12: T,
m21: T, m22: T,
m31: T, m32: T
}
impl<T:Add<T, Output=T> +
Copy +
Clone +
Mul<T, Output=T> +
One +
PartialOrd +
Sub<T, Output=T> +
Zero> Matrix2D<T> {
pub fn new(m11: T, m12: T, m21: T, m22: T, m31: T, m32: T) -> Matrix2D<T> {
Matrix2D {
m11: m11, m12: m12,
m21: m21, m22: m22,
m31: m31, m32: m32
}
}
pub fn mul(&self, m: &Matrix2D<T>) -> Matrix2D<T> {
Matrix2D::new(m.m11*self.m11 + m.m12*self.m21,
m.m11*self.m12 + m.m12*self.m22,
m.m21*self.m11 + m.m22*self.m21,
m.m21*self.m12 + m.m22*self.m22,
m.m31*self.m11 + m.m32*self.m21 + self.m31,
m.m31*self.m12 + m.m32*self.m22 + self.m32)
}
pub fn translate(&self, x: T, y: T) -> Matrix2D<T> {
let (_0, _1): (T, T) = (Zero::zero(), One::one());
let matrix = Matrix2D::new(_1.clone(), _0.clone(),
_0.clone(), _1.clone(),
x, y);
return self.mul(&matrix);
}
pub fn scale(&self, x: T, y: T) -> Matrix2D<T> {
Matrix2D::new(self.m11 * x, self.m12.clone(),
self.m21.clone(), self.m22 * y,
self.m31.clone(), self.m32.clone())
}
pub fn identity() -> Matrix2D<T> {
let (_0, _1): (T, T) = (Zero::zero(), One::one());
return Matrix2D::new(_1.clone(), _0.clone(),
_0.clone(), _1.clone(),
_0.clone(), _0.clone());
}
pub fn to_array(&self) -> [T; 6] {
[
self.m11.clone(), self.m12.clone(),
self.m21.clone(), self.m22.clone(),
self.m31.clone(), self.m32.clone()
]
}
#[inline]
pub fn transform_point(&self, point: &Point2D<T>) -> Point2D<T> {
Point2D::new(point.x * self.m11 + point.y * self.m21 + self.m31,
point.x * self.m12 + point.y * self.m22 + self.m32)
}
#[inline]
pub fn transform_rect(&self, rect: &Rect<T>) -> Rect<T> {
let top_left = self.transform_point(&rect.origin);
let top_right = self.transform_point(&rect.top_right());
let bottom_left = self.transform_point(&rect.bottom_left());
let bottom_right = self.transform_point(&rect.bottom_right());
let (mut min_x, mut min_y) = (top_left.x.clone(), top_left.y.clone());
let (mut max_x, mut max_y) = (min_x.clone(), min_y.clone());
for point in [ top_right, bottom_left, bottom_right ].iter() {
if point.x < min_x {
min_x = point.x.clone()
}
if point.x > max_x {
max_x = point.x.clone()
}
if point.y < min_y {
min_y = point.y.clone()
}
if point.y > max_y {
max_y = point.y.clone()
}
}
Rect::new(Point2D::new(min_x.clone(), min_y.clone()),
Size2D::new(max_x - min_x, max_y - min_y))
}
}