1

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

4

2 回答 2

1

如果矩形定义为

topx, topy, widht, height

比例因子是

factor

原点坐标是

ox, oy

您可以使用以下公式按给定原点缩放矩形

topx   = ox + ( topx - ox ) * factor;
topy   = oy + ( topy - oy ) * factor;
width  = width  * factor;
height = height * factor;
于 2016-08-11T15:00:55.533 回答
0

我对比例的定义与你的不同,因为如果我从左上角缩放 1.5,我得到的矩形将是 {100, 100, 75, 75} -> 原点保持不变,每边的大小乘以比例.

使用这些定义,如果 (x, y) 是矩形的左上角坐标,从中心缩放并保持原点不变: {x, y, width, height} -> { x + width * (1 - scale)/2, y + height * (1 - scale)/2, width * scale, height * scale}

我建议 scale > 0 尽管结果是针对零值和负值定义的。


工作示例:将 {100, 100, 50, 50} 从中心缩放 1.5。

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

结果:{100, 100, 50, 50} -> {87.5, 87.5, 75, 75}

于 2009-12-22T11:37:42.850 回答