Reputation:
the function already tells you what it does, but anyway, it wraps angle and makes sure it's between 0-360. it's Javascript:
Number.prototype.wrapAngle = function(){
var d = this;
while(d > 360){ d -= 360;};
while(d < 0){ d += 360;};
return d.toString();
}
I need 'wrapAngle' to be fastest possible!
Upvotes: 0
Views: 60
Reputation: 187222
Why not use modulo?
-721 % 360; // -1
390 % 360; // 30
90 % 360; // 90
So:
Number.prototype.wrapAngle = function(){
var d = this % 360;
if (d < 0) { // ensure positive number.
d += 360;
}
// return a number, not a string.
// This is a math based transformation of a number, so it should be a number.
return d;
}
Since modulus can return a negative number, but that number is always closer to zero than -360
you can simply increment it once if it's negative.
Performance comparison: http://jsperf.com/wrapangle
Upvotes: 3
Reputation: 782107
Use the modulus operator.
Number.prototype.wrapAngle = function(){
return (this % 360).toString();
}
Upvotes: 1