Good Math with Prototypes and Functions

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?

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 :slight_smile:

hehe, yup that worked. Thought I tried exactly that, guess I mussed it. Thanks! Any other math shortcuts you can give me?

nope, not that i know sorry :slight_smile:

Originally posted by jingman
any other math shortcuts you can give me?

are you looking for something specific? :slight_smile:
http://proto.layer51.com/l.aspx?p=7