Game Development Reference
In-Depth Information
To accommodate this, we need to abandon using the index of the vector as our
x
value. Instead, we create a
struct
that contains data for both
x
and
y
. We then can
create a vector composed of that
struct.
struct sELEMENT
{
int x;
double y;
};
typedef std::vector< sELEMENT > ELEMENT_VECTOR;
ELEMENT_VECTOR mvElementVector;
Entering Data
Filling the vector with the equation results is not much different with this new
twist. Instead of simply pushing a
y
value onto the next element of the vector, we
now store both the
x
and
y
value in an
sELEMENT
and push the whole thing onto the
end of the vector.
void CLinearFunction::FillVector( int Low, int High )
{
sELEMENT thisElement;
for ( int x = Low; x <= High; x++ ) {
thisElement.x = x;
thisElement.y = ( -2 * x ) + 100;
mvElementVector.push_back( thisElement );
} // end for
}
If, as we stated above, we want to store the data for
x
values from 125 to 165, we
call
FillVector
with:
FillVector( 125, 165 );
By running this new version of
FillVector
, we fill
mvElementVector
with 41
entries. The
x
values range from 125 to 165, with the corresponding
y
values being
the result of our function.