spotlightGeometry.test.ts
10.4 KB
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
import { describe, expect, it } from 'vitest';
import { hotspotSpan, M_PER_DEG_LAT, M_PER_EQUATOR } from './hotspotGeometry';
import {
addCell,
addPoint,
corridorOf,
emptyBounds,
finishBounds,
hottestCell,
metersPerDegLng,
weightedCenter,
type SpotlightCell,
} from './spotlightGeometry';
/**
* 聚焦展示的形状几何。
*
* 这里钉住的不是「好不好看」,而是「屏上涂出来的那些深浅,加起来是不是榜上那一行的数」:
* 走廊切片只搬运格子的起数,一个都不新增、也不丢(corridorOf 的段起数之和 = 输入格子起数之和)。
* 这一条一旦破了就是「地图上数一遍 780、榜上写 812」那种一被查出来就全盘失信的错,
* 所以每段测试都同时核对「形状」和「数」。
*/
const LAT = 22.5431;
const LNG0 = 114.05;
/** 一把 100 米的尺:Worker 回传的道路格子就是这个边长 */
const CELL_M = 100;
/** 用米制格子坐标造点:(i, j) 是「往东 i 格、往北 j 格」,读数时不用管经纬度换算 */
function cell(i: number, j: number, n: number): SpotlightCell {
return {
lng: LNG0 + (i * CELL_M) / metersPerDegLng(LAT),
lat: LAT + (j * CELL_M) / M_PER_DEG_LAT,
n,
};
}
/** 两点间的米制距离(等距圆柱近似,几百米尺度上够用) */
function metersBetween(a: { lng: number; lat: number }, b: { lng: number; lat: number }): number {
const dx = (a.lng - b.lng) * metersPerDegLng((a.lat + b.lat) / 2);
const dy = (a.lat - b.lat) * M_PER_DEG_LAT;
return Math.hypot(dx, dy);
}
function totalOf(cells: SpotlightCell[]): number {
return cells.reduce((s, c) => s + c.n, 0);
}
describe('metersPerDegLng · 与黑点盘的米制换算同源', () => {
it('赤道上就是 1° 经线米数,纬度越高越短', () => {
expect(metersPerDegLng(0)).toBeCloseTo(M_PER_EQUATOR, 6);
expect(metersPerDegLng(45)).toBeCloseTo(M_PER_EQUATOR * Math.SQRT1_2, 6);
expect(metersPerDegLng(22.5431)).toBeLessThan(metersPerDegLng(0));
});
it('与 hotspotSpan 给出同一个答案(少一处各写一遍,就多一处对不上)', () => {
const span = hotspotSpan(LAT, 300);
expect(300 / span.halfLng).toBeCloseTo(metersPerDegLng(LAT), 6);
expect(300 / span.halfLat).toBeCloseTo(M_PER_DEG_LAT, 6);
});
});
describe('weightedCenter · 落点均值中心', () => {
it('按起数加权,不是按格子个数取中间', () => {
const cells = [cell(0, 0, 1), cell(10, 0, 199)];
const c = weightedCenter(cells)!;
// 几乎全在那 199 起上:中心离重的一格不到半格
expect(metersBetween(c, cells[1])).toBeLessThan(CELL_M);
expect(metersBetween(c, cells[0])).toBeGreaterThan(9 * CELL_M);
});
it('带上的 n 是总权重,等于这一档的起数', () => {
const cells = [cell(0, 0, 3), cell(2, 1, 5), cell(-4, 3, 8)];
expect(weightedCenter(cells)!.n).toBe(totalOf(cells));
});
it('没有一起(或空列表)时给 null,调用方据此不画标签', () => {
expect(weightedCenter([])).toBeNull();
expect(weightedCenter([cell(0, 0, 0), cell(1, 1, 0)])).toBeNull();
});
});
describe('hottestCell · 标签锚在哪一格', () => {
it('取起数最多的那一格', () => {
const cells = [cell(0, 0, 4), cell(1, 0, 40), cell(2, 0, 7)];
expect(hottestCell(cells)).toBe(cells[1]);
});
it('并列时取先出现的那一格(Worker 已按起数倒序,所以永远是最靠前的那一档)', () => {
const cells = [cell(0, 0, 9), cell(1, 0, 9), cell(2, 0, 9)];
expect(hottestCell(cells)).toBe(cells[0]);
});
it('空列表给 null', () => {
expect(hottestCell([])).toBeNull();
});
});
describe('corridorOf · 由落点格子描出走向', () => {
it('一段直路:切片数等于格子数,每段的读数就是那一格的起数', () => {
const cells = [cell(0, 0, 12), cell(1, 0, 7), cell(2, 0, 30), cell(3, 0, 5)];
const paths = corridorOf(cells, CELL_M);
expect(paths.length).toBe(1);
const nodes = paths[0].nodes;
expect(nodes.length).toBe(cells.length);
expect(nodes.map((v) => v.n)).toEqual([12, 7, 30, 5]);
// 自西向东排:图层按这个顺序连线,反了就把路画成了掉头
for (let k = 1; k < nodes.length; k += 1)
expect(nodes[k].lng).toBeGreaterThan(nodes[k - 1].lng);
});
it('段起数之和 = 输入格子起数之和(地图涂出来的深浅一加起来必须等于榜上那一行)', () => {
const cells = [
cell(0, 0, 3),
cell(1, 1, 21),
cell(2, 0, 7),
cell(3, 3, 45),
cell(9, 1, 2),
cell(-5, -2, 11),
];
const paths = corridorOf(cells, CELL_M);
expect(paths.reduce((s, p) => s + p.total, 0)).toBe(totalOf(cells));
expect(paths.reduce((s, p) => s + p.nodes.reduce((t, v) => t + v.n, 0), 0)).toBe(
totalOf(cells),
);
});
it('断续:隔到 4 格(约 400 米)分成两条走廊,硬连成一条就是把两段说成一段', () => {
const cells = [cell(0, 0, 1), cell(1, 0, 1), cell(2, 0, 1), cell(6, 0, 1)];
const paths = corridorOf(cells, CELL_M);
expect(paths.length).toBe(2);
expect(paths[0].nodes.length).toBe(3);
expect(paths[1].nodes.length).toBe(1);
expect(paths[0].total + paths[1].total).toBe(4);
});
it('连得上:隔 3 格(约 300 米)仍算同一条路,多半只是地址写法造成的空洞', () => {
const cells = [cell(0, 0, 1), cell(1, 0, 1), cell(2, 0, 1), cell(5, 0, 1)];
const paths = corridorOf(cells, CELL_M);
expect(paths.length).toBe(1);
expect(paths[0].nodes.length).toBe(4);
expect(paths[0].total).toBe(4);
});
it('拐弯:走廊跟着折,每一段都落在原本那一格的附近(不是首末连直线)', () => {
// 一条 L 形:东西向四格 + 从端点往北四格
const cells = [
cell(0, 0, 1),
cell(1, 0, 1),
cell(2, 0, 1),
cell(3, 0, 1),
cell(4, 1, 1),
cell(4, 2, 1),
cell(4, 3, 1),
cell(4, 4, 1),
];
const paths = corridorOf(cells, CELL_M);
expect(paths.length).toBe(1);
const nodes = paths[0].nodes;
// 每一段都贴着某个落点:走廊若被拉成首末直线,转角那几段会飘到几百米外
for (const v of nodes) {
const nearest = Math.min(...cells.map((c) => metersBetween(v, c)));
expect(nearest).toBeLessThan(1.2 * CELL_M);
}
// 西北端与东南端都还在:走向被拉直时一端会被丢掉
expect(metersBetween(nodes[0], cells[0])).toBeLessThan(CELL_M);
expect(metersBetween(nodes[nodes.length - 1], cells[7])).toBeLessThan(CELL_M);
});
it('格子挤在同一层楼时并片,读数相加而不是各写一遍', () => {
// 两格只差半格(< 切片宽度):归进同一片,屏上就是一个读数
const cells = [cell(0, 0, 4), cell(0.5, 0, 6)];
const paths = corridorOf(cells, CELL_M);
expect(paths.reduce((s, p) => s + p.total, 0)).toBe(10);
expect(paths.reduce((s, p) => s + p.nodes.length, 0)).toBeLessThan(cells.length);
});
it('只有一格时描不出走向,给一条只有这一格的走廊', () => {
const paths = corridorOf([cell(2, 3, 17)], CELL_M);
expect(paths).toEqual([{ nodes: [cell(2, 3, 17)], total: 17 }]);
});
it('全同一点(协方差为 0)不退化成 NaN,主轴按东西向摆', () => {
const cells = [cell(1, 1, 5), cell(1, 1, 5), cell(1, 1, 5)];
const paths = corridorOf(cells, CELL_M);
expect(paths.length).toBe(1);
expect(paths[0].total).toBe(15);
for (const v of paths[0].nodes) {
expect(Number.isFinite(v.lng)).toBe(true);
expect(Number.isFinite(v.lat)).toBe(true);
expect(metersBetween(v, cells[0])).toBeLessThan(1e-6);
}
});
it('没有一起或没有格子时不给走廊(不画形状,也不报「全都在」)', () => {
expect(corridorOf([], CELL_M)).toEqual([]);
expect(corridorOf([cell(0, 0, 0), cell(1, 0, 0)], CELL_M)).toEqual([]);
});
it('输出的经纬度是真实度数,且落在输入格子围出的范围里', () => {
const cells = [cell(-3, -2, 4), cell(0, 0, 9), cell(4, 3, 2)];
const paths = corridorOf(cells, CELL_M);
const lo = {
lng: Math.min(...cells.map((c) => c.lng)),
lat: Math.min(...cells.map((c) => c.lat)),
};
const hi = {
lng: Math.max(...cells.map((c) => c.lng)),
lat: Math.max(...cells.map((c) => c.lat)),
};
for (const p of paths) {
for (const v of p.nodes) {
expect(v.lng).toBeGreaterThanOrEqual(lo.lng - 1e-9);
expect(v.lng).toBeLessThanOrEqual(hi.lng + 1e-9);
expect(v.lat).toBeGreaterThanOrEqual(lo.lat - 1e-9);
expect(v.lat).toBeLessThanOrEqual(hi.lat + 1e-9);
}
}
});
});
describe('范围累加器 · 取景用的并集', () => {
it('一格都没并进来时给 null,调用方据此不改视野', () => {
expect(finishBounds(emptyBounds())).toBeNull();
});
it('格子按四条边并入,不是按中心(按中心取景会把最外一圈涂到屏幕外)', () => {
const b = emptyBounds();
addCell(b, cell(0, 0, 1), CELL_M);
const bounds = finishBounds(b)!;
const w = (bounds.maxLng - bounds.minLng) * metersPerDegLng(LAT);
const h = (bounds.maxLat - bounds.minLat) * M_PER_DEG_LAT;
expect(w).toBeCloseTo(CELL_M, 6);
expect(h).toBeCloseTo(CELL_M, 6);
});
it('多格取并集:中心与边角一起算进来', () => {
const b = emptyBounds();
addCell(b, cell(0, 0, 1), CELL_M);
addCell(b, cell(4, 3, 1), CELL_M);
const bounds = finishBounds(b)!;
// 从最西南那格的西南角到最东北那格的东北角 = 横向 5 格、纵向 4 格。
// 宽度只准到厘米:每格的半宽按它自己那一档纬度换算,北边那格略窄,这是等距圆柱近似的本相
expect((bounds.maxLng - bounds.minLng) * metersPerDegLng(LAT)).toBeCloseTo(5 * CELL_M, 2);
expect((bounds.maxLat - bounds.minLat) * M_PER_DEG_LAT).toBeCloseTo(4 * CELL_M, 3);
});
it('addPoint 只扩不缩,且能被格子范围包住', () => {
const b = emptyBounds();
addPoint(b, LNG0, LAT);
expect(finishBounds(b)).toEqual({
minLng: LNG0,
maxLng: LNG0,
minLat: LAT,
maxLat: LAT,
});
addPoint(b, LNG0 - 0.01, LAT + 0.02);
const after = finishBounds(b)!;
expect(after.minLng).toBeLessThan(LNG0);
expect(after.maxLat).toBeGreaterThan(LAT);
// 已并进来的点不会被后面的点「挤回去」
addPoint(b, LNG0 - 0.001, LAT + 0.001);
expect(finishBounds(b)).toEqual(after);
});
});