square root & math

Started by cornjob, Fri 15/08/2003 09:12:22

Previous topic - Next topic

cornjob

So anyway, I'm trying to calculate the distance between 2 points on the screen, to see if a point is within a certain radius of another point... and...

Well, I was never really a math person. The best idea I could come up with is using the pythagorean theorem... But is there a square root operation in AGS? If not, how can I make one? Can I somehow use the functions in Erica's math plugin? (Sorry, like I said, my math is very limited.) Or is there a better way to do this? I'm guessing there is.

OK, thanks. (my first tech question in years)

Gilbert

#1
No, and since AGS doesn't support float data type, you won't get much of it, you may try Erica's math plugin though, that there may be someway round it.

If you just want to have just a approximate integer portion of sqrt, there is a foolish (but wiorking) solution:

function sqrt(int inputnum){
int i = 0;
while ((i*i)<inputnum) {i++;}
if ((i*i)==inputnum){return i;}
else {return (i - 1);}
}

You may tweak it so it return the closest integer, etc.

Fuzzpilz

You don't need the root, just use the square of the radius.

Gilbert

Yep in your case fuzz's solution should work too.

cornjob

OK, thanks guys... I'll try this out.

Ghormak

True what Fuzz says. Say, if you want to see if (x1,y1) and (x2,y2) are within 8 pixels from each other, you'd simply check if ( (x2 - x1)^2 + (y2-y1)^2 ) is less than 64 (8^2). No need for the square root if you don't need to actually display the distance. I'm sure you knew this already, but I thought I'd clear it up if anyone else was wondering.

(correct me if I made any mistakes, my math is a bit rusty nowadays)
Achtung Franz! The comic

Gilbert

Heh all right, but Ghor, better post the codes as:
... check if ( (x2 - x1) * (x2 - x1) + (y2-y1) * (y2-y1) ) is less than ...

Or people who actually tried it by copying the codes will get an error, as there's no raise to power operator in AGS ;)

Ghormak

Ah, that's what I get for not working with AGS for months. Thanks Gil. :)

* Ghormak gets to work on Moose Wars 2... maybe
Achtung Franz! The comic

SMF spam blocked by CleanTalk