# Interpolation of four points (grid corners)

## Recommended Posts

Hi all,

Consider, if you will, this image:

http://i.imgur.com/xsZ6duv.png

Assuming that the four corners there have n value, I'm looking for a way to determine the best values for the red dots (or, actually, and point within the square).  I'm trying to determine light values at a finer grain than just finding the nearest corner and using it.

Is there a way to do this?  I tried simple linear interpolation combining both directions, but the results aren't satisfactory, so I think the way I thought up is fatally flawed.

Thanks for any help!

--JR

#### Share this post

##### Share on other sites

I usually do something like interpolate between the TL and TR values according to the X position to get a Top value, then interpolate between the BL and BR values according to the X position to get a Bottom value, then interpolate between my calculated Top value and my calculated Bottom value according to the Y position to get a final result.

Not sure if there's better options out there.

#### Share this post

##### Share on other sites

Do you happen to have a scrap of code I can look at?  Cuz I'm doing something similar to that, I think-- I'm getting a left right value, and then modulating it against an up-down value.  Like I said, the results are... much worse than I would have expected, so I'm clearly doing something wrong.

#### Share this post

##### Share on other sites

Have you tried looking into Bilinear interpolation?

#### Share this post

##### Share on other sites

Do you happen to have a scrap of code I can look at?  Cuz I'm doing something similar to that, I think-- I'm getting a left right value, and then modulating it against an up-down value.  Like I said, the results are... much worse than I would have expected, so I'm clearly doing something wrong.

Here's some code from my perlin noise code. It uses a cosine interpolation for extra smoothness in ground height, but a straight lerp should work OK too.

	// Separate the base and the fraction parts.
int iBaseX = (int)floorf(vInput.GetX());
float fFractionX = vInput.GetX() - (float)iBaseX;
int iBaseY = (int)floorf(vInput.GetY());
float fFractionY = vInput.GetY() - (float)iBaseY;

// Cosine interpolation
float fTL = GetDiscreteNoise2D(iBaseX, iBaseY, pPrimeSet);
float fTR = GetDiscreteNoise2D(iBaseX + 1, iBaseY, pPrimeSet);
float fBL = GetDiscreteNoise2D(iBaseX, iBaseY + 1, pPrimeSet);
float fBR = GetDiscreteNoise2D(iBaseX + 1, iBaseY + 1, pPrimeSet);
float fT = CosInterp(fTL, fTR, fFractionX);
float fB = CosInterp(fBL, fBR, fFractionX);
float fRet = CosInterp(fT, fB, fFractionY);
return fRet;



## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now

• ### Forum Statistics

• Total Topics
627735
• Total Posts
2978855

• 10
• 10
• 21
• 14
• 12