C/C++ Function to Compute the Bilinear Interpolation


Bilinear Interpolation (BI) is a very useful mathematical approach that you can use to estimate any given value in a two dimensional grid.

Bilinear_interpolation C/C++ Function to Compute the Bilinear Interpolation  c / c++ code code library math programming languages

Bilinear_interpolation

If you don’t like Matlab (me neither), then the following C/C++ function may be useful to you. You can also easily use this code in other C-like programming languages e.g. Java with little modification. You can also use double type if the precision is of importance.

For more information on Bilinear Interpolation, you can visit the Wiki in details.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// https://helloacm.com
inline float 
BilinearInterpolation(float q11, float q12, float q21, float q22, float x1, float x2, float y1, float y2, float x, float y) 
{
    float x2x1, y2y1, x2x, y2y, yy1, xx1;
    x2x1 = x2 - x1;
    y2y1 = y2 - y1;
    x2x = x2 - x;
    y2y = y2 - y;
    yy1 = y - y1;
    xx1 = x - x1;
    return 1.0 / (x2x1 * y2y1) * (
        q11 * x2x * y2y +
        q21 * xx1 * y2y +
        q12 * x2x * yy1 +
        q22 * xx1 * yy1
    );
}
// https://helloacm.com
inline float 
BilinearInterpolation(float q11, float q12, float q21, float q22, float x1, float x2, float y1, float y2, float x, float y) 
{
	float x2x1, y2y1, x2x, y2y, yy1, xx1;
	x2x1 = x2 - x1;
	y2y1 = y2 - y1;
	x2x = x2 - x;
	y2y = y2 - y;
	yy1 = y - y1;
	xx1 = x - x1;
	return 1.0 / (x2x1 * y2y1) * (
		q11 * x2x * y2y +
		q21 * xx1 * y2y +
		q12 * x2x * yy1 +
		q22 * xx1 * yy1
	);
}

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
275 words
Last Post: C/C++ Coding Exercise - Find Minimum in Rotated Sorted Array - Leetcode Online Judge
Next Post: How To Block Google Domains in Adsense?

The Permanent URL is: C/C++ Function to Compute the Bilinear Interpolation

4 Comments

  1. Stix
  2. manoj
  3. Wasiulla sharief

Leave a Reply