Created
April 2, 2012 12:12
-
-
Save davidaurelio/2283073 to your computer and use it in GitHub Desktop.
Angle type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Angle(radians) { | |
this._radians = radians; | |
} | |
Angle.prototype = { | |
degrees: function(degrees) { | |
this._radians = degrees / 180 * Math.PI; | |
return this; | |
}, | |
radians: function(radians) { | |
this._radians = radians; | |
return this; | |
}, | |
asDegrees: function() { | |
return this._radians / Math.PI * 180; | |
}, | |
asRadians: function() { | |
return this._radians; | |
}, | |
add: function(angle) { | |
this._radians += angle; | |
return this; | |
}, | |
multiply: function(factor) { | |
this._radians *= factor; | |
return this; | |
}, | |
valueOf: function() { | |
return this._radians; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var a = new Angle().radians(Math.PI * 3 / 4), | |
b = new Angle().degrees(135); | |
var c = new Angle(a + b); | |
Math.sin(a); | |
Math.cos(b); | |
a.add(new Angle().degrees(30)); | |
b.add(Math.PI); | |
c.multiply(1.5); |
parse strings:
var a = new Angle().fromString('90deg');
or var b = AngleFromString('90deg');
a.add(b)
// PI
+1 new Angle().fromString('90deg');
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice
toDegrees and toRadians i would say