graphics - How do I translate a point from one rect to a point in a scaled rect? -
i have image (i) that's been scaled original size (rectlarge) fit smaller rectangle (rectsmall). given point p in rectsmall, how can translate point in rectlarge.
to make concrete, suppose have 20x20 image that's been scaled 10x10 rect. point (1, 1) in smaller rect should scaled point in larger rect (i.e. (2,2)).
i'm trying achieve with:
result.x = point.x * (destrect.size.width / srcrect.size.width ); result.y = point.y * (destrect.size.height / srcrect.size.height);
however, points generated code not correct - not map appropriate point in original image. doing wrong?
what language using?
if c, , rect.size.height
, ints, have major rounding problem. you're scaling point 10, 10
size 20x20
size 30x30
. calculations like:
result.x = 10 * (30 / 20);
this simplifies to:
result.x = 10 * (1);
the simplest way fix rid of parentheses:
result.x = point.x * destrect.size.width / srcrect.size.width; result.y = point.y * destrect.size.height / srcrect.size.height;
now calculation, 10 * 30 / 20
, simplifies 300 / 20
, 15
, want.
note, still have slight rounding problem, unless can subpixel locations, close you're gonna get.
Comments
Post a Comment