I need help optimizing the function

Basically, I want to create an angle (0 - 360 degrees) that is not within the given range of a number of other angles. I already made this function to check two angles:

function check(angle1, angle2, range) { var diff = angle1 - angle2; if(Math.abs(diff % 360) <= range || (360-Math.abs(diff % 360)) <= range) { return true; } else { return false; } } 

Simple enough, but I need to check an arbitrary angle at all other angles, continue if it passes, create a new corner and double-check if it failed, and find out when it is impossible to go through any new corner.

I think this will work:

 var others = [array of objects]; ... for(var i = 0; i < 360; i++) { var pass = true; for(var n = 0; n < others.length; n++) { if(check(i, others[n].angle, 5)) { pass = false; break; } } if(pass) return i; } return false; 

However, this is a lot of cycles, and I would prefer a random angle rather than an increase. Is there a faster and better way to do this? Thanks.

Edit: decided to do something similar, got the idea from @TheBronx.

 var angles = []; var range = 5; function alterAngle(a, n) { var angle = a + n; if(angle < 0) angle = 360 + angle; if(angle > 360) angle = angle - 360; return angle; } // in the function var angle = Math.floor(Math.random() * 360); if(angles.indexOf(angle) == -1) { for(var i = -range; i <= range; i++) angles.push(alterAngle(angle, i)); } 
+4
source share
3 answers

Idea. Imagine that your corners are cards in a deck. When you create a random corner, you remove that corner from the deck, as well as the angles within your range. When you need to create a new angle, instead of generating a random value between 0..360, you just need to โ€œselect a mapโ€. This will always work if you donโ€™t have any more cards available.

Is the problem that you have a lot of cards? Do you have enough time to initialize the "cards" at startup?

Just an idea ... I donโ€™t know if this is good or not, but it seems promising.

+3
source

This

 if(Math.abs(diff % 360) <= range || (360-Math.abs(diff % 360)) <= range) { return true; } else { return false; } 

you can do it this way it will reduce operations

 return Math.abs(diff % 360) <= range || (360-Math.abs(diff % 360)) <= range; 
+1
source

Based on my previous question: Fisher Yates shuffles coffee - script

 var counter, i, permutations, shuffle, x, _i; // Fischer Yates shuffle algorithm shuffle = function(arr, required) { var i, index, randInt, _i, _ref, _ref1, _ref2; if (required == null) { required = arr.length; } randInt = function(n) { return Math.floor(n * Math.random()); }; if (required > arr.length) { required = arr.length; } if (required <= 1) { return arr[randInt(arr.length)]; } for (i = _i = _ref = arr.length - 1, _ref1 = arr.length - required; _ref <= _ref1 ? _i <= _ref1 : _i >= _ref1; i = _ref <= _ref1 ? ++_i : --_i) { index = randInt(i + 1); _ref2 = [arr[i], arr[index]], arr[index] = _ref2[0], arr[i] = _ref2[1]; } return arr.slice(arr.length - required); }; // generate array of all possible angles var angles = [] for(i=0;i<360;i++){ angles.push(i) } // shuffle as many as you need (20 in this example) var shuffled = shuffle(angles,20) // check the result console.log(shuffled) // simply deal off values from this shuffled array as needed 
0
source

All Articles