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; }
source share