Resizing a rectangle , from different origins
I do have rectangle, which had information about topx, t开发者_开发问答opy, width and height.
I want to scale this rectangle based on an origin other than top-left. Is there already existing algo to do that ?
Currently I work on Eclipse GEF & SWT. In GEF, the all rectangle operations are assumed that top-left is where the drawing starts and they scale/resize from top-left. But I want to do scale/resize from center.
eg : my rectangle have info like {100,100,50,50}. If I do scaling of 1.5 in both x&y from top-left I'll get the resultant rectangle as {100,100,100,100} ( First two are x,y and rest are width,height).
Thanks J
if the rectangle is defined by
topx, topy, widht, height
the scaling factor is
factor
the origin coordinates are
ox, oy
you can scale the rectangle by the given origin with the following formula
topx = ox + ( topx - ox ) * factor;
topy = oy + ( topy - oy ) * factor;
width = width * factor;
height = height * factor;
My definition of scale is different from yours because if I scale by 1.5 from the top left my resulting rectangle would be {100, 100, 75, 75} -> the origin stays the same and the size of each side is multiplied by the scale.
Using these definitions, if (x, y) are the top left co-ordinates of the rectangle, scaling from the center and keeping the origin constant: {x, y, width, height} -> { x + width * (1 - scale)/2, y + height * (1 - scale)/2, width * scale, height * scale}
I suggest scale > 0 although the result is defined for zero and negative values.
Worked example: Scale {100, 100, 50, 50} by 1.5 from the center.
x: 100 -> 100 + 50 * (1 - 1.5)/2 = 100 + 50 * (-0.5)/2 = 100 - 50/4 = 87.5
y: 100 -> 100 + 50 * (1 - 1.5)/2 = 100 + 50 * (-0.5)/2 = 100 - 50/4 = 87.5
width: 50 -> 50 * 1.5 = 75
height: 50 -> 50 * 1.5 = 75
Result: {100, 100, 50, 50} -> {87.5, 87.5, 75, 75}
精彩评论