2
schangxiang@126.com
2024-08-16 b47c50a2a514def7374b32d7194b2c599cba5625
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
<!Doctype html>
<html>
<head>
    <script src="http://s1.bdstatic.com/r/www/cache/ecom/esl/1-8-8/esl.js"></script>
    
    <script src="http://ubilabs.github.io/kd-tree-javascript/kdTree.js"></script>
 
    <meta charset="utf-8">
</head>
<style>
    html, body {
        height: 100%;
        margin: 0px;
    }
</style>
<body>
    <canvas id="main"></canvas>
</body>
<script>
    require.config({
        packages: [{
            name: 'echarts',
            location: '../src',
            main: 'echarts'
        }, {
            name: 'zrender',
            location: '../../zrender/src',
            main: 'zrender'
        }]
    });
 
    require(['echarts/data/KDTree'], function (KDTree) {
        var points = [];
        var points2 = [];
        var width = window.innerWidth;
        var height = window.innerHeight;
        var canvas = document.getElementById('main');
        canvas.width = width;
        canvas.height = height;
        var ctx = canvas.getContext('2d');
        for (var i = 0; i < 50000; i++) {
            var point = [Math.round(width * Math.random()), Math.round(height * Math.random())];
            points.push({
                array: point
            });
            points2.push(point);
        }
 
        var time = performance.now();
        var kdTree1 = new KDTree(points);
        console.log(performance.now() - time);
 
        var squaredDistance = function (a, b) {
            a = a.array;
            b = b.array;
            return (a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]);
        }
        var squaredDistance2 = function (a, b) {
            return (a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]);
        }
        var kdTree2 = new kdTree(points2, squaredDistance2, [0, 1]);
        // Find nearest
        var p2 = [Math.round(width * Math.random()), Math.round(height * Math.random())];
        p = {
            array: p2
        };
        var time = performance.now();
        for (var i = 0; i < 50000; i++) {
            var nearest = kdTree1.nearest(p, squaredDistance);
        }
        console.log(performance.now() - time);
        console.log(p, nearest);
 
        // Find nearst N
        var nearestN = [];
        var time = performance.now();
        for (var i = 0; i < 50000; i++) {
            var nearest = kdTree1.nearestN(p, 5, squaredDistance, nearestN);
        }
        console.log(performance.now() - time);
        console.log(p.array, nearestN.map(function(a) {
            return a.array;
        }));
 
        setTimeout(function () {
            var time = performance.now();
            for (var i = 0; i < 50000; i++) {
                var nearest = kdTree2.nearest(p2, 5);
            }
            console.log(performance.now() - time);
            console.log(p2, nearest.map(function(a) {
                return a[0];
            }));
        }, 1000);
 
        // var buildSplitLines = function (node, minx, miny, maxx, maxy) {
        //     switch (node.axis) {
        //         case 0:
        //             var x = node.data[0];
        //             ctx.moveTo(x + 0.5, miny);
        //             ctx.lineTo(x + 0.5, maxy);
        //             ctx.stroke();
        //             node.left && buildSplitLines(node.left, minx, miny, node.data[0], maxy);
        //             node.right && buildSplitLines(node.right, node.data[0], miny, maxx, maxy);
        //             break;
        //         case 1:
        //             var y = node.data[1];
        //             ctx.moveTo(minx, y + 0.5);
        //             ctx.lineTo(maxx, y + 0.5);
        //             ctx.stroke();
        //             node.left && buildSplitLines(node.left, minx, miny, maxx, node.data[1]);
        //             node.right && buildSplitLines(node.right, minx, node.data[1], maxx, maxy);
        //             break;
        //     }
 
        // }
        // ctx.fillStyle = 'black';
        // buildSplitLines(kdTree1.root, 0, 0, width, height);
 
        // ctx.fillStyle = 'red';
        // for (var i = 0; i < points.length; i++) {
        //     var p = points[i];
        //     ctx.fillRect(p[0], p[1], 4, 4);
        // }
    })
</script>
</html>