integer interpolation

This topic is 4910 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi Is it possible to fast lineary interpolate between two integers without using floating point computations, I mean only adding, substracting and multiplying of integers? I googled a little and found that there was an Application Note from Intel about bilerp using MMX but I can't find it on their site. Maybe you have some links about similar techniques (simple fractal linear interpolation algorithm) Thanks

Share on other sites
Apologies if I misinterpreted your request, but:
inline int interpolate(int a, int b, int percentage) {    return (a*percentage)/100+(b*(100-percentage)/100);}

would seem like a sensible solution - no floats in there. Of course it depends entirely on what you're using it for.

Share on other sites
Yes; for example by using fixed point numbers or Bresenham's algorithm.

EDIT: DavW's code is an example of fixed-point arithmetic, though it would make more sense to use binary, not decimal, fixpoints - that way you can replace the costly divides by bit shifts.

Share on other sites
Oops, you have a good point there:
Revised version (position is between 0 and 256):
inline int interpolate(int a, int b, int position) {    return (a * position) >> 8 + (b * (256-position)) >> 8;}

Share on other sites
I think this idea with binary fixpoint and shifting will do. There may be some problems with position range but nothing I can't handle.
thanks a lot!

Share on other sites
Beleive it or not, in the age of the pentium 4 and athlon64, divides aren't so costly that you need to be conscious about them.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 15
• 14
• 46
• 22
• Forum Statistics

• Total Topics
634054
• Total Posts
3015274
×