调整矩形的大小,来自不同的来源

发布于 2024-08-15 11:50:37 字数 321 浏览 9 评论 0原文

我确实有矩形,其中包含有关 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

败给现实 2024-08-22 11:50:38

定义,则

如果矩形由topx, topy, widht, height

缩放因子为

factor

原点坐标为

ox, oy

您可以缩放由给定原点按以下公式计算的矩形

topx   = ox + ( topx - ox ) * factor;
topy   = oy + ( topy - oy ) * factor;
width  = width  * factor;
height = height * factor;

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;
静赏你的温柔 2024-08-22 11:50:38

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

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

我建议比例 > 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}

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}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文