aboutsummaryrefslogtreecommitdiff
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
parent87042230b282b5123a1b7489f244952992a4109d (diff)
downloadplayGraph-8ccdc51b4cd62f2930e3495fc6c0065245cc7646.tar.gz
playGraph-8ccdc51b4cd62f2930e3495fc6c0065245cc7646.tar.bz2
playGraph-8ccdc51b4cd62f2930e3495fc6c0065245cc7646.zip
add path service
-rw-r--r--app/js/controllers.js12
-rw-r--r--app/js/services.js40
2 files changed, 46 insertions, 6 deletions
diff --git a/app/js/controllers.js b/app/js/controllers.js
index 0a2931e..32f2ea4 100644
--- a/app/js/controllers.js
+++ b/app/js/controllers.js
@@ -4,7 +4,17 @@
var p = new Point(1,2);
app.controller('pointCtrl', function($scope, pathService){
- pathService.computePlaylist();
+ var constList = [
+ {x:1,y:2},
+ {x:3,y:2},
+ {x:4,y:2}
+ ];
+ var songs = [
+ {x:1,y:0},
+ {x:3,y:0},
+ {x:4,y:8}
+ ];
+ pathService.computePlaylist(songs,constList,60);
$scope.song= [
];
});
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;