Thank you for your help. I managed to come up with something this afternoon which looks to be working fairly well. I used the "Inverse Distance to a Power" method as opposed to straight ahead linear interpolation.
The w[m] array is the "weighted" array which weights the contribution of each known point.
The code isn't really robust at all, but if you feed it the 3 line input file I showed a couple posts up, and tell it to use a grid increment of 1, it works perfectly. I had to use this new fangled (for me, anyways) dynamic memory allocation "new" and "delete" commands, which I'd never used before.
Code:
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <malloc.h>
#include <math.h>
using namespace std;
int main( void )
{
ifstream in_stream;
ofstream out_stream;
char filename[50];
char filename_out[50];
int i=0, num_points, m;
float X[10000], Y[10000], Z[10000];
//float* grid;
float X_min=0, Y_min=0, X_max=0, Y_max=0;
float X_points, Y_points;
float gridspace;
float sum_w, w[3];
out_stream.setf(ios::fixed);
out_stream.precision(3);
cout << "Assuming input file contains scattered data in the form:\n";
cout << "X[a] Y[b] Z[a,a]\n";
cout << "X[c] Y[d] Z[c,d]\n";
cout << "X[e] Y[f] Z[e,f]...\n";
cout << "\nOutput file is completed grid using 'Inverse Distance to a Power' to estimate Z values";
cout << "\nEnter filename to convert: ";
cin >> filename;
cout << "\nEnter grid spacing: ";
cin >> gridspace;
strcat(filename_out, filename);
strcat(filename_out, "_grid");
out_stream.open(filename_out);
in_stream.open(filename);
// Read in 1st line
in_stream >> X[0];
in_stream >> Y[0];
in_stream >> Z[0];
// Initialize min and max values
X_min = X[0];
X_max = X[0];
Y_min = Y[0];
Y_max = Y[0];
// Read scattered data file
while (!in_stream.eof()) {
i++;
// Read in data
in_stream >> X[i];
in_stream >> Y[i];
in_stream >> Z[i];
// Set min and max values
if (X[i] < X_min) X_min = X[i];
if (X[i] > X_max) X_max = X[i];
if (Y[i] < Y_min) Y_min = Y[i];
if (Y[i] > Y_max) Y_max = Y[i]; }
num_points = i;
in_stream.close();
X_points = (X_max - X_min) / gridspace + 1;
Y_points = (Y_max - Y_min) / gridspace + 1;
cout << "\nX_points: " << X_points;
cout << "\nY_points: " << Y_points << "\n\n";
// Dynamically allocate space for the grid
float** grid;
grid = new float*[X_points];
for(int j = 0 ; j < X_points ; ++j)
grid[j] = new float[Y_points];
// Initialize the grid
int x, y, n=0;
for(x = 0 ; x < X_points ; ++x)
for(y = 0 ; y < Y_points ; ++y) grid[x][y]=0;
// Assign values within the grid
for(x = 0 ; x < X_points ; ++x) {
for(y = 0 ; y < Y_points ; ++y) {
if ( (x==X[n]) & (y==Y[n]) ) { grid[x][y] = Z[n]; n++; } // If its a point we already have, don't recalculate it
//else grid[x][y] = 8;
else {
for(m = 0; m <= num_points; ++m) { // Look through all the scattered points
w[m] = pow( pow( (x - X[m]), 2) + pow( (y - Y[m]), 2), -0.5 ); // Find linear distance to each point
w[m] = pow( w[m], 2 );
grid[x][y] += w[m] * Z[m];
sum_w += w[m]; }
grid[x][y] /= sum_w; // Calculate the grid value
sum_w = 0;}
cout << x << "\t" << y << "\t" << grid[x][y] << "\n"; }}
// Free up the memory for the grid
for(j = 0 ; j < X_points ; ++j)
delete[] grid[j];
delete[] grid;
}