tags:

views:

138

answers:

1

Hi, I do have rectangle , which had information about topx ,topy , 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

A: 

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}

richj
I don't think it will yield correct results. Because assume my rectangle is {100,100,50,50} . So if i scale 1.5 at origin , it should be {87.5,87.5,75,75} . Isn't it ?
Jijoy
Correct. That's what the formula gives you. I've added a worked example to demonstrate this. The center of both rectangles is at 125, 125.
richj
yeah , my bad . Your answer is correct. Thanks
Jijoy