Monday, April 28, 2008

Bit-101's Three Useful Methods

Keith Peters posted on his blog last week three useful methods.

I've added a couple of extra methods that I'm finding I use more often than not and might as well shove into this class. Shove is an inelegant word, but I'm an inelegant guy.

//thanks, Keith.

public static function distanceCalculation(x1:Number, y1:Number, x2:Number, y2:Number):Number
{
var dx:Number = x1 - x2;
var dy:Number = y1 - y2;
return Math.sqrt(dx * dx + dy * dy);
}

//I mixed something up here. Fixed the randomRange value return. Thanks again, Keith.

public static function randomRange(start:Number, end:Number):Number
{
return Math.floor(start + Math.random() * (end - start));
}
It's fair to note that I didn't create these methods. They were found at various times in various locations. I just keep misplacing them and looking for them again. Good to know that it's last time I'll do it.

It's nice to be spurred on to centralizing this stuff. Thanks, Keith!

Labels: ,

5 Comments:

At Mon Apr 28, 11:57:00 AM PDT, Blogger Keith Peters said...

I think this might be a bit more optimized for distance:

var dx:Number = x1 - x2;
var dy:Number = y1 - y2;
return Math.sqrt(dx * dx + dy * dy);

A couple less calculations.

Also I think this is wrong:

return Math.floor(start + Math.random() * end - start);

More like this maybe:

return Math.floor(start + Math.random() * (end - start));

 
At Wed Jun 18, 08:20:00 AM PDT, Blogger Elizabeth said...

I think there's a semi-colon missing in there somewhere.

 
At Wed Jul 16, 05:00:00 PM PDT, Blogger anthony_pace said...

OMG the guy is trying to help you out and you pick it apart; as well, you guys didn't even think to reduce the amount of variables in use before posting your supposedly optimized versions.

function distance(x1:Number, y1:Number, x2:Number, y2:Number):Number {

return(Math.sqrt(((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2))));

}

 
At Thu Jul 17, 09:13:00 PM PDT, Blogger hugh said...

lol. In all fairness, one's Keith himself and the other's my fiancee.

 
At Sun Jul 20, 10:46:00 PM PDT, Blogger anthony_pace said...

Oh man...lol... I am an idiot; I should have read the names.

Let this be a lesson to all; read before posting, or look as stupid as I do.

 

Post a Comment

<< Home