# Pithagore-fast computation

• 01-14-2008
invinciblevn
Pithagore-fast computation
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.
• 01-14-2008
vart
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
• 01-14-2008
iMalc
Post the function in your current code that uses it and we'll show you an optimised version.
• 01-14-2008
invinciblevn
just now I use this function:
c=(int) sqrt(a*a+b*b);
• 01-14-2008
vart
Quote:

Originally Posted by invinciblevn
just now I use this function:
c=(int) sqrt(a*a+b*b);

that can be 1 less than the actual value
• 01-14-2008
invinciblevn
Quote:

Originally Posted by vart
that can be 1 less than the actual value

I'm doing compute distant in an image which use pixel as unit.
sqrt() require a lot resource.
• 01-14-2008
matsp
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
• 01-14-2008
Dino
Pythagorean. Pythagoras invented it.
• 01-14-2008
iMalc
Quote:

Originally Posted by invinciblevn
just now I use this function:
c=(int) sqrt(a*a+b*b);

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.
• 01-15-2008
invinciblevn
Quote:

Originally Posted by iMalc
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.
• 01-15-2008
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.
• 01-15-2008
matsp
Quote:

Originally Posted by Adak
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