Well, I'm done bashing my head with this one because I've literally gone over it 10 times if not more and I can't seem to find anything wrong with it, so firstly here's the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int Numbers::CheckPositionClash(int currentposition) //Doesn't function properly
{
if (currentposition > 0)
for (int i = currentposition - 1; i >= 0; i--)
{
if ((position[currentposition].X >= position[i].X) && (position[currentposition].X <= (position[i].X + bmpNumber[0]->w)))
if ((position[currentposition].Y >= position[i].Y) && (position[currentposition].Y <= (position[i].Y + bmpNumber[0]->h)))
return 1; //found a clash
return 0; // no clash
}
elsereturn 0; // no clash
}
This is the bit (the function) where I think the problem lays in. Now a little background of the program, and the function, well it is basically a "collision" detection routine to check if two images overlap, now I can't seem to find the error, but there is definitely one, since when I output the result I often get images overlapping. Ok this is it, so if anybody has any idea, lay it on me.
If I've explained it in a weird or an incomprehensible way, let me know and I will be glad to provide additional info.
You're a) only testing a single point (the top left one) of the object and b) you're not testing for any positions after the index "currentposition". Also, use the term index when you mean a "position" in an array.
A possible design problem I see: why are you having an array of positions? Why is the position not part of an object?
Are all your objects the same size? If not, then you're using the wrong dimensions in most cases (i.e. bmpNumber[0]->w).
And you should return bool when a function can only return true or false.
1 2 3 4 5 6 7 8 9 10 11 12
bool Numbers::CheckPositionClash(uint positionIndex)//we don't have negative indexes and we return a boolean value
{
for (uint i=0;i<positions.size();i++) //assumes positions to be a container (also, note the plural)
{
if (i==positionIndex)continue; //skip position to be checked for collision
const Position& p1=positions[positionIndex]; //use references to create aliases
const Position& p2=positions[i]; //ditto
constint w=bmpNumber[0]->w,h=bmpNumber[0]->h; //removes clutter from the following line that contains the actual logic
if (!(p1.X>=p2.X+w || p1.Y>=p2.Y+h || p1.X+w<p2.X || p1.Y+h<p2.Y))returntrue; //untested, but should be correct
}
returnfalse;
}