Jump to content

JavaScript Math Functions


ShadowMage

Recommended Posts

Does anybody know of a good reference that I can use to find math calculations and such?I need to be able to convert degrees to radians and use sin, cosin, tangent, etc. plus a lot of other area formulas.Any ideas? Thanks.

Link to comment
Share on other sites

Surely you've seen this: http://www.w3schools.com/jsref/jsref_obj_math.asp ? Most of those methods operate on radians. The conversion formula is pretty simple; but here's a link if you don't know it: http://www.krellinst.org/uces/archive/reso...rig/node10.htmlI suppose someone out there might have a better Math object. Why not extend this one?

Link to comment
Share on other sites

Yes, I've looked at the W3Schools link. I just threw those sin, cosine functions in just because. Thanks for the link to the formula. I've been all the way through calculus in high school but I can't remember all those formulas. That was four years ago! :)I guess what I was hoping for was a link to some free scripts that I could use (and of course give credit to their writers) instead of writing my own and having to research all the formulas that I need. But I guess research it is. Google here I come! :)BTW,Frankly, I don't know how to extend an object. I've tried playing around with the prototype property of other objects before but I could never really figure out how it works. Perhaps you could enlighten me? :)

Link to comment
Share on other sites

Use prototype to "extend" the prototype of a string object or array object, etc. The Math object is like a singleton. So you can extend it just by adding properties. So, the Math object as defined has no property called .two , but I can give it one just like this:Math.two = 2;Same with a new method. An advantage there is that you can use self-reference in methods, since Math.PI is exactly what you think it is. A method can get that value by referring to this.PI and that will work.

Link to comment
Share on other sites

How would I define a method that accepts parameters? For example, I want to add the toDeg property to convert a number from radians to degrees but I need to be able to pass that number to it.Like this?Math.toDeg = function(radians) { ... }Or say I already wrote a function:function toDegrees(radians) {...}and I want to set the toDeg method equal to that function, would that be like this:Math.toDeg = toDegrees;or this:Math.toDeg = function(rad) { toDegrees(rad); }

Link to comment
Share on other sites

All 3 will work. The first is the most efficient, the third is the least.What I like to do for this stuff is open my FF Error Console and enter code in the code bar. You can tweak things pretty quickly, without the hassle of opening and closing a file for every change.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...