How do I translate a point from one rect to a point in a scaled rect?

Posted by Bill on Stack Overflow See other posts from Stack Overflow or by Bill
Published on 2010-04-29T00:03:26Z Indexed on 2010/04/29 0:07 UTC
Read the original article Hit count: 466

Filed under:
|
|

I have an image (i) that's been scaled from its original size (rectLarge) to fit a smaller rectangle (rectSmall). Given a point p in rectSmall, how can I translate this to a point in rectLarge.

To make this concrete, suppose I have a 20x20 image that's been scaled to a 10x10 rect. The point (1, 1) in the smaller rect should be scaled up to a point in the larger rect (i.e. (2,2)).

I'm trying to achieve this with:

result.x = point.x * (destRect.size.width / srcRect.size.width );
result.y = point.y * (destRect.size.height / srcRect.size.height);

However, the points generated by this code are not correct - they do not map to the appropriate point in the original image. What am I doing wrong?

© Stack Overflow or respective owner

Related posts about graphics

Related posts about scaling