Transformation
# 概述
Transformation
类在 Leaflet 中用于处理二维仿射变换,表示二维坐标的变换,它包含四个属性:_a
、_b
、_c
和 _d
。这些属性用于表示变换矩阵的元素。
# 源码分析
# 源码实现
Transformation
的源码实现如下:
// 构造函数
export function Transformation(a, b, c, d) {
// a,c:表示x和y轴的缩放因子;b,d表示x和y轴的平移量
if (Util.isArray(a)) {
this._a = a[0];
this._b = a[1];
this._c = a[2];
this._d = a[3];
return;
}
this._a = a;
this._b = b;
this._c = c;
this._d = d;
}
// 构造函数的原型上扩展方法
Transformation.prototype = {
// 转换方法,将一个点进行变换,并返回变换后的点
transform: function (point, scale) {
// scale:缩放因子,默认为1
return this._transform(point.clone(), scale);
},
_transform: function (point, scale) {
scale = scale || 1;
point.x = scale * (this._a * point.x + this._b);
point.y = scale * (this._c * point.y + this._d);
return point;
},
// 逆向变换,将变换后的点还原成原始坐标系
untransform: function (point, scale) {
scale = scale || 1;
return new Point(
(point.x / scale - this._b) / this._a,
(point.y / scale - this._d) / this._c
);
},
};
// 工厂函数
export function toTransformation(a, b, c, d) {
return new Transformation(a, b, c, d);
}
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
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
# 总结
通过 Transformation
类,Leaflet 高效地处理了地图坐标的线性变换,支撑了地图瓦片加载、图层叠加等核心功能。
编辑 (opens new window)
上次更新: 2025/04/18, 07:17:03