aboutsummaryrefslogtreecommitdiff
path: root/tests/pathAlgoPerf.html
blob: 5c91d906317d6444289c66850c06fa39a55e9899 (plain) (blame)
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
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="../app/lib/angular.js"></script>
    </head>
    <body>
        <div>TODO write content</div>
        <script type="text/javascript">
            var dist = function(point1, point2) {
                var dx = point1.x - point2.x;
                var dy = point1.y - point2.y;
                return Math.sqrt(dx * dx + dy * dy);
            };
            var computePlaylist = function(songList, line, duration) {
                //compute distances from the constrain path

                var threshold = 60;
                angular.forEach(songList, function(song) {
                    song.selected = false;
                });

                var selection = [];
                angular.forEach(line, function(point) {
                    angular.forEach(songList, function(song) {
                        var d = dist(song, point);
                        if (d < threshold && song.selected !== true) {
                            selection.push(song);
                            song.selected = true;
                        }
                    });
                });
                console.log(selection.length);
                return selection;
            };

            var computePlaylist2 = function(songList, line, duration) {
                //compute distances from the constrain path

                var threshold = 60;
                for(var i=songList.length-1; i>0; i--){
                    songList.selected = false;
                };
                var songCopy = songList.slice(0);
                songCopy.sort(function(a,b){return a.x-b.x;});

                var selection = [];
                for(var i=line.length-1; i>0; i--) {
                    var point = line[i];
                    var min = line[i].x-threshold;
                    var max = line[i].x+threshold;
                    var j=songCopy.length-1;
                    while(songCopy[j].x > max && j > 0){
                        j--;
                    }
                    while(songCopy[j].x > min && j > 0){
                        var song = songCopy[j];
                        var d = dist(song, point);
                        if (d < threshold) {
                                songCopy.splice(j,1);
                                selection.push(song);
                                song.selected = true;
                        }
                        j--;
                    };
                };
                console.log(selection.length);
                return selection;
            };
            var computePlaylist3 = function(songList, line, duration) {
                //compute distances from the constrain path

                var threshold = 60;
                for(var i=songList.length-1; i>0; i--){
                    songList.selected = false;
                };
                var songCopy = songList.slice(0);

                var selection = [];
                for(var i=line.length-1; i>0; i--) {
                    var point = line[i];
                    for(var j=songCopy.length-1; j>0; j--){
                        var song = songCopy[j];
                            var d = dist(song, point);
                            if (d < threshold) {
                                songCopy.splice(j,1);
                                selection.push(song);
                                song.selected = true;
                            }
                    };
                };
                console.log(selection.length);
                return selection;
            };
            

            var generateRandCoords = function(amount) {
                var temp = [];
                var randomPoint = function() {
                    return {
                        x: Math.round(Math.random() * 1000),
                        y: Math.round(Math.random() * 1000)
                    };
                };
                for (var i = 0; i < amount; i++) {
                    temp.push(randomPoint());
                }
                ;
                return temp;
            };

            var line = generateRandCoords(100);
            var songs = generateRandCoords(100);

            var timestamp = new Date().getTime();
            computePlaylist(songs, line, 60);
            console.log('execution time', new Date().getTime() - timestamp);

            var timestamp = new Date().getTime();
            computePlaylist2(songs, line, 60);
            console.log('execution time2', new Date().getTime() - timestamp);
            
            var timestamp = new Date().getTime();
            computePlaylist3(songs, line, 60);
            console.log('execution time3', new Date().getTime() - timestamp);
        </script>
    </body>
</html>