Without a bit more details, I assume that you are actually suffering from rounding errors ...
- When you scale (top, left) coordinate back to the original, you need to round off (top left).
- When you scale (bottom, right) coordinate back to the original, you need to round (right to right)
Take a simple example of a 12x12 grid as the original and a 4x4 grid as a scaled version.
- (1,1) :( 2,2) on the scaled version = (3,3) :( 8,8)
- 2x2 pixel = 25% of the scaled version area
- 6x6 pixels = 25% of the area of ββthe original version
If we simply multiply by the same scale factors, this would give (3.3): (6.6).
OriginalTop = INT (ScaledTop * YScalingFactor);
OriginalLeft = INT (ScaledLeft * XScalingFactor);
OriginalBottom = INT ((ScaledBottom + 1) * YScalingFactor) - 1;
OriginalRight = INT ((ScaledRight + 1) * XScalingFactor) - 1;
EDIT
The best way to explain what I'm trying to say is to draw a picutra. And I suck in ASCII Art. So here is another attempt with words.
The pixel is not a point. This is a small rectangle in its own right.
When you use a pixel to represent the upper left corner of a rectangle, you include the area from the upper left of the point's point.
When you use a pixel to represent the Lower Right Rectangle, you include the area completely at the point. The lower right point of the point.
Using the example (12x12) => (4x4), each scaled pixel represents a whole set of 3 Γ 3 pixels in the original. Speaking of the top left, you select the top left pixel of the 3x3 pixel group in the original. And when you talk about the lower right, you select the lower right part of the 3x3 pixel group in the original.
EDIT : using integers.
NewTop = (( OldTop ) * NewHeight / OldHeight); NewLeft = (( OldLeft ) * NewWidth / OldWidth ); NewBottom = ((OldBottom + 1) * NewHeight / OldHeight) - 1; NewRight = ((OldRight + 1) * NewWidth / OldWidth ) - 1;
The only consideration is that you are not overflowing your data type after multiplication. But with the images you will not, unless it is a damn image.