aboutsummaryrefslogtreecommitdiff
path: root/app/js/services.js
diff options
context:
space:
mode:
authorNicolas Bornand <nicolas74@gmail.com>2014-05-10 18:32:25 +0200
committerNicolas Bornand <nicolas74@gmail.com>2014-05-10 18:32:25 +0200
commit8ccdc51b4cd62f2930e3495fc6c0065245cc7646 (patch)
treed6a74f2c2df352c7f73e57c597e27bef1e6d9375 /app/js/services.js
parent87042230b282b5123a1b7489f244952992a4109d (diff)
downloadplayGraph-8ccdc51b4cd62f2930e3495fc6c0065245cc7646.tar.gz
playGraph-8ccdc51b4cd62f2930e3495fc6c0065245cc7646.tar.bz2
playGraph-8ccdc51b4cd62f2930e3495fc6c0065245cc7646.zip
add path service
Diffstat (limited to 'app/js/services.js')
-rw-r--r--app/js/services.js40
1 files changed, 35 insertions, 5 deletions
diff --git a/app/js/services.js b/app/js/services.js
index 1b203f5..1788915 100644
--- a/app/js/services.js
+++ b/app/js/services.js
@@ -9,13 +9,43 @@ app.service('pathService', function() {
var dy = point1.y - point2.y;
return Math.sqrt(dx * dx + dy * dy);
},
- computePlaylist: function(songList, constrainList) {
+ computePlaylist: function(songList, constrainList, duration) {
+ //compute distances from the constrain path
+ var maxDistOverall = Number.MIN_VALUE;
angular.forEach(songList, function(song){
- var min = Number.MAX_VALUE;
- angular.forEach(constrainList, function(point){
- var d = dist(song, point);
- });
+ song.minDist = Number.MAX_VALUE;
+ for(var i=0; i<constrainList.length;i++){
+ var point = constrainList[i];
+ var d = self.dist(song, point);
+ if(d < song.minDist){
+ song.minDist = d;
+ song.closest = point;
+ song.closestIndex = i;
+ }
+ }
+ if(song.minDist > maxDistOverall){
+ maxDistOverall = song.minDist;
+ }
});
+ //select the songs
+ var selection = [];
+ while(duration > 0 && songList.length > 0){
+ //randomly select one song
+ var index = Math.floor(Math.random()*songList.length);
+ var song = songList[index];
+ var ratio = song.minDist/maxDistOverall;
+ console.log(ratio);
+ //favor the ones closer to the path
+ var weight = 1 - ratio*ratio;
+ if(Math.random() < weight){
+ songList.splice(index, 1);
+ selection.push(song);
+ }
+ duration = 0;
+ }
+ selection.sort(function(item){ return item.closestIndex;});
+ console.log(selection);
+ return selection;
}
};
return self;