aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Odersky <jodersky@gmail.com>2014-05-10 18:43:13 +0200
committerJakob Odersky <jodersky@gmail.com>2014-05-10 18:43:13 +0200
commit8b6ff428f2f482f94c533e2b4366cd7316f3244a (patch)
treebc4ef9468987703971cdb2ca3de6cfede833b655
parent7339de327612bf6cd02fb255b213d963ff00de25 (diff)
parent31acd22765a129bd482354f875f230c4a2b66e4f (diff)
downloadplayGraph-8b6ff428f2f482f94c533e2b4366cd7316f3244a.tar.gz
playGraph-8b6ff428f2f482f94c533e2b4366cd7316f3244a.tar.bz2
playGraph-8b6ff428f2f482f94c533e2b4366cd7316f3244a.zip
Merge branch 'master' of github.com:jodersky/playGraph
Conflicts: app/js/controllers.js
-rw-r--r--app/js/controllers.js3
-rw-r--r--app/js/services.js40
2 files changed, 37 insertions, 6 deletions
diff --git a/app/js/controllers.js b/app/js/controllers.js
index cb061da..1c6e8d0 100644
--- a/app/js/controllers.js
+++ b/app/js/controllers.js
@@ -3,13 +3,14 @@
/* Controllers */
var p = new Point(1,2);
+<<<<<<< HEAD
app.controller('pointCtrl', ['$scope', '$http', function($scope, $http){
var constList = [
{x: 10, y:20},
{x: 100, y:50},
{x: 100, y:200}
]
- pathService.computePlaylist(dummyPoints,);
+ pathService.computePlaylist(dummyPoints,constList, 60);
$scope.points=dummyPoints;
/*$http.get('data.json').then( function (response) {
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;