Radical functions

Game Editor comments and discussion.

Radical functions

Postby willg101 » Fri May 05, 2006 1:18 am

Is there anyway to figure out the squre root of a number in GE?

For example, to find the distance from two points you can use the formula
[(x1-x2)^2 + (y1-y2)^2]. Of course, what's in brackets would be under a radical sign. Also, when I say x1-x2 I mean your first x value minus your second x value.
http://www.wish-games.com
Old betas now available!!
User avatar
willg101
 
Posts: 473
Joined: Thu Dec 16, 2004 12:08 am
Location: West Michigan
Score: 5 Give a positive score

Postby Fuzzy » Fri May 05, 2006 1:59 am

so you want to use the formula

d^2 = (x1-x2)^2 + (y1-y2)^2 and reduce it to d?

Do you have to calculate it on the fly, or can you build a lookup table? Are you using integers, or floating points?

When I was in school, we were just a few years ahead of the time when kids were allowed calculators, and I learned the basics with charts and tables. Ugh. Still, its an excellent way to do things when programming, because it really knocks the computation cycles down.

If you need a float as a return value, just build that table with integers and then use that Linear Interpolation function that I posted a while back. That should return a value that isnt exact, but closer than you can resolve with pixels, if thats the use.

Here it is again..

int Lint(int a, int z, double j) {return (int)(a*(1-j) + z*j);}

That returns an integer. here is a floating point version(didnt check for errors though)

double Fint(int a, int z, double j) {return (double)(a*(1-j) + z*j);}

note that it still takes two integers as inputs. As always, j should be from 0.0 to 1.0 and no other numbers, or strangeness results. change int to double to make it accept float numbers for a and z.

[edit]errr, what I mean is, take the closest lookup value rounding down, assign it to a, and the lookup value one table entry higher than that, assigned to z, and use those as inputs to the Lint() with a j value of 0.5.
[/edit]

Ahem. As for calculating square roots, try...

sqrt();

Its easily overlooked... i know. its in the function list, far right, just about halfway down the last column, right after sprintf.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Postby willg101 » Fri May 05, 2006 8:00 pm

Thank-you soo much, ThreeFinger Pete! As always, you've made my life a whole lot easier! :D
http://www.wish-games.com
Old betas now available!!
User avatar
willg101
 
Posts: 473
Joined: Thu Dec 16, 2004 12:08 am
Location: West Michigan
Score: 5 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest