Rectangle width/height off-by-one?

Found a bug or something that needs fixing?
Post Reply
Edgar5
Posts: 63
Joined: Sun Apr 02, 2017 7:22 am

Rectangle width/height off-by-one?

Post by Edgar5 »

In file:
\Helper\Helper.h
at or near line #220, there are two functions:
int GetRectHeight(RECT *rc);
int GetRectWidth(RECT *rc);
implemented in file:
\Helper\Helper.cpp
at or near line #575 thus:
int GetRectHeight(RECT *rc)
{
return rc->bottom - rc->top;
}

int GetRectWidth(RECT *rc)
{
return rc->right - rc->left;
}
in the header I have changed this to:
int GetRectHeight(const RECT & pRectangle);
int GetRectWidth(const RECT & pRectangle);
because if you are returning the calculation it makes no sense to pass in a pointer to an actual rectangle structure; I have also added "const" to enforce preservation of the parameter's value. The implementation looks like this:
int GetRectHeight(const RECT & pRectangle) {
return pRectangle.bottom - pRectangle.top + 1;//was off-by-one
//return rc->bottom - rc->top;
}

int GetRectWidth(const RECT & pRectangle) {
return pRectangle.right - pRectangle.left + 1;//was off-by-one
}
Note that I think that there was an original off-by-one error. If you manually count the pixels in a window whose "left" is 2 and whose "right" is 5 you will see:
2 3 4 5
which is four pixels whereas the original math (5-2 = 3) is off-by-one.
Edgar5
Posts: 63
Joined: Sun Apr 02, 2017 7:22 am

Re: Rectangle width/height off-by-one?

Post by Edgar5 »

A global Search & Replace; all files (*.c;*.cpp;*.h); In the solution:
Replace:
GetRectHeight(&
With:
GetRectHeight(
and Replace:
GetRectWidth(&
With:
GetRectWidth(
resolves all the coding changes.
Post Reply