Dear all.
I'm doing the computation rectangle's edge using Pithagore theory.
All 3 rectangle's edge are integer.
Is there any algorithm for fast computation that don't use sqrt() function?
Thanks.
Printable View
Dear all.
I'm doing the computation rectangle's edge using Pithagore theory.
All 3 rectangle's edge are integer.
Is there any algorithm for fast computation that don't use sqrt() function?
Thanks.
c is less than a+b but greater than max(a,b)
you can use some search algorithm in this range with test condition c*c == a*a +b*b
to find c
not guaranteed to be faster, but it uses only integer math
Post the function in your current code that uses it and we'll show you an optimised version.
just now I use this function:
c=(int) sqrt(a*a+b*b);
So don't calculate the sqrt, just use the c * c value - for all intents and purposes, it's the same thing, just bigger.
--
Mats
Pythagorean. Pythagoras invented it.
That's not what I meant. I meant, post the whole function that includes this line of code. Assuming of course that it isn't longer than a couple dozen lines...
It's extremely difficult to optimise 1 line of code, but if we can see the lines of code around it then it becomes quite easy.
Thank you very much, iMalc.
I'm doing the image rotation. I use this for point distance computation.
This computation is used many times in my program, so that is require a lot resource.
I wonder whether there is any other way of point distant computation that does not use pythagore?
That is my problem.
Thanks.
Your problem must be a common one in gaming forums. (as opposed to general purpose programming forums like this). I'd check the gaming forum, and also game.dev for more detailed answers.
You can find the hypotenuse of a triangle by using it's two sides and the angle between them, but I have no idea if that is any faster, at all. The game programmers will know what's quick.
Most games use the trick I explained, by not using sqrt unless absolutely necessary.
For image rotation, you could use the trick of Bresenham's circle algorithm: http://www.ecse.rpi.edu/Homepages/wr...bresenham.html
--
Mats