Game Development Reference
In-Depth Information
For the 41 potential values of vector index i , the corresponding values of the
elements x and y would be:
i
x
y
0
125
-150
1
126
-152
2
127
-154
3
128
-156
…
…
…
38
163
-226
39
164
-228
40
165
-230
Extracting a Value
Now that the index of the vector no longer corresponds to the x value, recovering
the data that we need is a slightly more involved process. There are three primary
ways of handling this.
Brute Force
The first option we could take is the brute force method. By iterating through the
entire vector, we could check all the x values until we find the one we want and re-
turn the corresponding y value. This is a perfectly viable solution—especially for
small data sets. It looks much like what we did with the guess type function earlier.
double CLinearFunction::GetY_BruteForce(int x)
{
for ( int i = 0; i < mvElementVector.size(); i++ ) {
if ( mvElementVector[i].x = x ) {
return mvElementVector[i].y;
} // end if
} // end for
// Code should never get here!
assert( 0 && “Index not found� );
return 0.0;
}
Search WWH ::




Custom Search