Game Development Reference
In-Depth Information
The GasTank class declares two other methods. The first is the getErrorFunction method
that returns a value of the error function. The method uses a table lookup procedure to compute
the error function value. The table lookup method is considerably faster than if the error function
were computed according to Equation (12.19). If the argument to the error function is greater
than 2.0, the error function value is set to 1.0. Otherwise, the error function value is found by
interpolating between known values of the error function. The method is declared to be private ,
so it can only be used by the GasTank class. If we wanted to reuse this method for other applications,
it could have been written as a public static method instead.
// This method computes and returns the value of
// the error function using a table lookup method.
private double getErrorFunction(double s) {
double erf[] = {0.0, 0.1125, 0.2227, 0.3286, 0.4284,
0.5205, 0.6039, 0.6778, 0.7421, 0.7969,
0.8427, 0.8802, 0.9103, 0.9340, 0.9523,
0.9661, 0.9764, 0.9838, 0.9891, 0.9928,
0.9953};
int j;
double value;
// If the argument is greater than 2.0, set the
// error function value to 1. Otherwise, find the
// value using the data in the erf[] array.
if ( s >= 2.0 ) {
value = 1.0;
}
else {
j = (int)(s*10.0);
value = erf[j] + (s*10.0 - j)*(erf[j+1] - erf[j]);
}
return value;
}
The final method declared in the class is named getTemperature , and it computes and
returns the temperature in the gas tank at any time and any location through the thickness
of the tank based on Equation (12.25).
// This method computes the value of the temperature
// for any given x and time value.
public double getTemperature(double x, double time) {
double temperature;
double grp;
double erf;
Search WWH ::




Custom Search