调整矩形的大小,来自不同的来源
我确实有矩形,其中包含有关 topx、topy、宽度和高度的信息。
我想根据左上角以外的原点缩放这个矩形。是否已经有现有的算法可以做到这一点?
目前我从事 Eclipse GEF 和 Eclipse 工作。 SWT。在 GEF 中,所有矩形操作都假定左上角是绘图开始的位置,并且它们从左上角开始缩放/调整大小。但我想从中心进行缩放/调整大小。
例如:我的矩形有类似 {100,100,50,50} 的信息。如果我从左上角开始对 x 和 y 进行 1.5 缩放,我将得到结果矩形:{100,100,100,100}(前两个是 x,y,其余是宽度,高度)。
谢谢 J
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
定义,则
如果矩形由
topx, topy, widht, height
缩放因子为
factor
原点坐标为
ox, oy
您可以缩放由给定原点按以下公式计算的矩形
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
我对比例的定义与你的不同,因为如果我从左上角缩放 1.5,我得到的矩形将是 {100, 100, 75, 75} ->原点保持不变,每条边的大小乘以比例。
使用这些定义,如果 (x, y) 是矩形的左上角坐标,从中心缩放并保持原点不变:
{x,y,宽度,高度}-> { x + 宽度 * (1 - 比例)/2, y + 高度 * (1 - 比例)/2, 宽度 * 比例, 高度 * 比例}
我建议比例 > 0,尽管结果被定义为零和负值。
示例:从中心开始将 {100, 100, 50, 50} 缩放 1.5。
结果:{100, 100, 50, 50} -> {87.5, 87.5, 75, 75}
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.
Result: {100, 100, 50, 50} -> {87.5, 87.5, 75, 75}