PDA

View Full Version : Good Math with Prototypes and Functions



jingman
May 19th, 2003, 07:27 PM
So, right now when I want to find the distance between two points, I go:

dist = Math.sqrt((this_x-that_x)*(this_x-that_x)+(this_y-that_y)*(this_y-that_y))

BLECH! I don't want to do that every time. So I want to make a nice little function that I can apply to an object. Problem is, I don't know how to do this. Is it possible to end up with something as easy as:

Math.dist(thisclipname, thatclipname) ?

How do you guys set up functions that do bulky math and return nice clean numbers?

RvGaTe
May 19th, 2003, 07:41 PM
yeah, thats possible, ill give it a try



Math.dist = function(thismc,thatmc){
result = Math.sqrt((thismc._x-thatmc._x)*(thismc._x-thatmc._x)+(thismc._y-thatmc._y)*(thismc._y-thatmc._y))
return result;
}

now you can call it like:

Math.dist(thisclipname,thatclipname);


edit: tested, and edited some errors, now it works fine :)

jingman
May 19th, 2003, 08:30 PM
hehe, yup that worked. Thought I tried exactly that, guess I mussed it. Thanks! Any other math shortcuts you can give me?

RvGaTe
May 20th, 2003, 05:13 AM
nope, not that i know sorry :)

kode
May 20th, 2003, 05:36 AM
Originally posted by jingman
any other math shortcuts you can give me?
are you looking for something specific? :)
http://proto.layer51.com/l.aspx?p=7