Skip to content

Instantly share code, notes, and snippets.

@davidaurelio
Created April 2, 2012 12:12
Show Gist options
  • Save davidaurelio/2283073 to your computer and use it in GitHub Desktop.
Save davidaurelio/2283073 to your computer and use it in GitHub Desktop.
Angle type
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;
}
};
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);
@wolframkriesing
Copy link

nice
toDegrees and toRadians i would say

@basecode
Copy link

basecode commented Apr 2, 2012

parse strings:
var a = new Angle().fromString('90deg'); or var b = AngleFromString('90deg');

a.add(b) // PI

@wolframkriesing
Copy link

+1 new Angle().fromString('90deg');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment