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.
This is a discussion on Pithagore-fast computation within the C Programming forums, part of the General Programming Boards category; Dear all. I'm doing the computation rectangle's edge using Pithagore theory. All 3 rectangle's edge are integer. Is there any ...
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
The first 90% of a project takes 90% of the time,
the last 10% takes the other 90% of the time.
Post the function in your current code that uses it and we'll show you an optimised version.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
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.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.