0

I just need some help with a simple problem.. I'm trying to learn C++ as i want to get into game programming, I have previous programming experience, but not in C++.. My question comes a little like this. I'm making a dummy console application where it will take the numbers of your current XYZ and compare it to a box of coords.. [x1,z1,y1 / x2,y2,z2] and check if you're within this area.. now i can do that on its own.. my question is, How would you write up something that would allow you to reuse this function but have a different output, Eg if its within X box then do X, if its in Y box then do Y .. This is what i currently have..

    void withinRange(int x1, int y1, int z1, int x2, int y2, int z2)
{

    if (cCoordx > x1 && cCoordy > y1 && cCoordz > z1 && cCoordx < x2 && cCoordy < y2 && cCoordz < z2)
    {

    }

}

I'm not really sure how to expand on this to make it able to be a reusable function..

1
  • 3
    Use a bool as return value and do the stuff what it is supposed to do for each box outside that function. Commented Apr 28, 2015 at 5:33

1 Answer 1

1

You asked:

How would you write up something that would allow you to reuse this function but have a different output, Eg if its within X box then do X, if its in Y box then do Y

  1. Write different functions that check whether the value is in X box or Y box.
  2. Implement withinRange using the other functions.
  3. Use the returned values of these functions to make decisions in the calling function(s).
bool withinXRange(int x1, int x2)
{
   return (cCoordx > x1  && cCoordx < x2);
}

bool withinYRange(int y1, int y2)
{
   return (cCoordy > y1  && cCoordy < y2);
}

bool withinZRange(int z1, int z2)
{
   return (cCoordz > z1  && cCoordz < z2);
}

bool withinRange(int x1, int y1, int z1, int x2, int y2, int z2)
{
   return ( withinXRange(x1, x2) &&
            withinYRange(y1, y2) &&
            withinZRange(z1, z2) );
}

In the calling function:

int x1 = <some value>;
int x2 = <some value>;

if ( withinXRange(x1, x2) )
{
    // Do something
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.