hotspotBounds.test.ts
3.28 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
import { describe, expect, it } from 'vitest';
import type { HotspotGrid } from './HotspotLayer';
import { hotspotBounds, hotspotsUnionBounds } from './hotspotBounds';
/**
* 这两个函数决定「镜头往哪儿飞」,所以钉的是几何本身,不掺任何地图对象。
*
* 三件事各对应一次真实的坏体验:
* 1. 单片的框要把整片装进去 —— 少一个格子,那片伸出去的枝就在屏外;
* 2. 多片的框是并集 —— 换统计维度后「把所有黑点摆进一屏」靠它,只框住第一片等于漏掉后面几处;
* 3. 只算清单上列出来的域 —— mask 里还留着因单片起数不够被丢弃的小域,
* 把它们算进来,取景会为了没画出来的格子多退一级。
*/
const GRID: HotspotGrid = {
originLng: 114,
originLat: 22.5,
dLng: 0.001,
dLat: 0.001,
w: 10,
h: 8,
};
/** 按 [cx, cy] 列表点亮格子。网格 10×8,一格 0.001° */
function maskOf(cells: Array<[number, number]>): Uint8Array {
const mask = new Uint8Array(GRID.w * GRID.h);
for (const [cx, cy] of cells) mask[cy * GRID.w + cx] = 1;
return mask;
}
/** 一个 L 形域:左上角 (1,1),向右两格、向下两格 */
const L_SHAPE: Array<[number, number]> = [
[1, 1],
[2, 1],
[3, 1],
[1, 2],
[1, 3],
];
/** 远处一个孤立格,构成第二个域 */
const FAR_CELL: Array<[number, number]> = [[8, 6]];
describe('hotspotBounds · 单片包围盒', () => {
it('L 形的框是能装下整个 L 的最小矩形,而不是质心周围的方框', () => {
const mask = maskOf(L_SHAPE);
const b = hotspotBounds(GRID, mask, 1);
expect(b).not.toBeNull();
// 格角:x 从 1 到 4(三格宽),y 从 1 到 4(三格高)
expect(b!.minLng).toBeCloseTo(114 + 1 * 0.001, 10);
expect(b!.maxLng).toBeCloseTo(114 + 4 * 0.001, 10);
expect(b!.minLat).toBeCloseTo(22.5 + 1 * 0.001, 10);
expect(b!.maxLat).toBeCloseTo(22.5 + 4 * 0.001, 10);
});
it('域不存在时给 null(调用方据此不动镜头)', () => {
const mask = maskOf(L_SHAPE);
expect(hotspotBounds(GRID, mask, 99)).toBeNull();
});
it('空白掩码给 null', () => {
expect(hotspotBounds(GRID, new Uint8Array(GRID.w * GRID.h), 1)).toBeNull();
});
});
describe('hotspotsUnionBounds · 多片并集', () => {
it('两片都算:从左上那片一直框到右下那一格', () => {
const mask = maskOf([...L_SHAPE, ...FAR_CELL]);
const b = hotspotsUnionBounds(GRID, mask, [1, 2]);
expect(b).not.toBeNull();
expect(b!.minLng).toBeCloseTo(114 + 1 * 0.001, 10);
expect(b!.maxLng).toBeCloseTo(114 + 9 * 0.001, 10);
expect(b!.minLat).toBeCloseTo(22.5 + 1 * 0.001, 10);
expect(b!.maxLat).toBeCloseTo(22.5 + 7 * 0.001, 10);
});
it('只算清单上列出的域:不列的那一片不参与取景', () => {
const mask = maskOf([...L_SHAPE, ...FAR_CELL]);
const b = hotspotsUnionBounds(GRID, mask, [1]);
expect(b!.maxLng).toBeCloseTo(114 + 4 * 0.001, 10);
expect(b!.maxLat).toBeCloseTo(22.5 + 4 * 0.001, 10);
});
it('空清单 / 全是不存在的编号时给 null —— 没有可框的东西就不动镜头', () => {
const mask = maskOf(L_SHAPE);
expect(hotspotsUnionBounds(GRID, mask, [])).toBeNull();
expect(hotspotsUnionBounds(GRID, mask, [7, 8])).toBeNull();
});
});