如何缩放许多形状并保持相同的比例

发布于 2024-10-21 18:35:45 字数 631 浏览 1 评论 0原文

我有两个形状,我想进行缩放,我使用以下代码

Iterator iter = objects.iterator();


    Shape shape;
    while(iter.hasNext()){
        shape = (Shape)iter.next();
        AffineTransform t = shape.getAffineTransform(); 
        int x = shape.getCenter().x;
        int y = shape.getCenter().y;            
        t.translate(-x, -y);
        t.scale(sx,sy);
        t.translate(x, y);
        shape.setAffineTransform(t);
    }

形状被缩放,但它们之间的距离变小 缩放前 缩放后the zomming

我想用这两个形状制作一个复合形状,然后缩放它。 还有其他方法可以保持比例吗?谢谢

I have two shapes and I want to do a zoom, I use the following code

Iterator iter = objects.iterator();


    Shape shape;
    while(iter.hasNext()){
        shape = (Shape)iter.next();
        AffineTransform t = shape.getAffineTransform(); 
        int x = shape.getCenter().x;
        int y = shape.getCenter().y;            
        t.translate(-x, -y);
        t.scale(sx,sy);
        t.translate(x, y);
        shape.setAffineTransform(t);
    }

The shapes are zoomed but the distance between them became smalled
Befor the zooming after the zomming

I thought making a composite shape from the two shapes and then scale it.
Is there another way to keep the proportions? thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

隔纱相望 2024-10-28 18:35:45

就像我在评论中所说的那样,因为您围绕各自的中心缩放形状,所以这些中心显然不会移动。如果您希望所有内容统一缩放,请不要平移任何内容:

  1. 找到原始视口的中心点。这是你必须缩小的点。
  2. 将所有内容放大 (sx,sy)。
  3. 通过 ( origcentrex*(1-sx), origcentrey*(1-sy)) 平移所有内容,将中心点移回到视口的中心。

计算这些变换的最佳方法是拿起铅笔和纸,绘制坐标系并遵循纸上的变换。

Like I said in my comment, because you scale the shapes around their respective centers, the centers will obviously not move around. If you want everything to be scaled uniformly, don't translate anything:

  1. Find the centre point of the original viewport. This is the point you have to zoom out from.
  2. Scale everything up by (sx,sy).
  3. Translate everything by ( origcentrex*(1-sx), origcentrey*(1-sy)) to move the centre point back to the centre of the viewport.

The best way to figure these transformations out is to grab pencil and paper, draw a coordinate system and follow the transformations on paper.

要走就滚别墨迹 2024-10-28 18:35:45

您拥有的代码会围绕其中心缩放每个形状。这意味着下面的线向下移动,上面的线向上移动。这就是为什么他们似乎一起移动。

为了避免这种情况发生,您必须围绕下边界(对于上部形状)和上边界(对于下部形状)上的点缩放形状。使“y”变量成为适当的边界。

作为替代(和等效)方法,在缩放每个形状后将其平移适当的量。

The code you have zooms each shape about its centre. This means the lower lines move down, and the upper lines move up. That's why they seem to move together.

In order to not have this happen you have to zoom the shapes about a point on the lower boundary (for the upper shape) and the upper boundary (for the lower shape). Make the "y" variable the appropriate boundary.

As an alternative (and equivalent) approach translate each shape by an appropriate amount after you have zoomed it.

月亮是我掰弯的 2024-10-28 18:35:45

如果你想要真正的缩放,这意味着缩放形状和它们之间的空白。如果您遵循幼稚的路线(缩放每个形状,然后缩放从一个形状到每个其他形状的距离),结果将不正确。

我知道的一种方法是将两个形状中心之间的距离视为具有固定方向的“吸引”力的矢量。然后衡量这些力的大小并找到使系统再次达到平衡的点。数学应该就在那里,某个地方。我很久以前就看到了这一切,就像是在另一种生活中一样。

If you want to have a true zoom, that means scaling the shapes and the white space between them. If you follow the naive route (scale each shape, and then scale the distance from a shape to every other shape) the result will not be right.

One method I'm aware of is to treat the distance between the center of two shapes as a vector of "attracting" force that have fixed direction. Then scale the magnitude of those forces and find the points that will put he system in balance again. The maths should be out there, someplace. I saw that so long ago that it seems like it was in another life.

浅听莫相离 2024-10-28 18:35:45

谢谢大家的回答,我找到了适合我的解决方案

    while(iter.hasNext()){
        shape = (Shape)iter.next();                     
        AffineTransform t = new AffineTransform();
        t.scale(sx,sy); 
        t.concatenate(shape.getAffineTransform());
        shape.setAffineTransform(t); 
    }
}

Thank you everybody for the answers, I found a solution that works for me

    while(iter.hasNext()){
        shape = (Shape)iter.next();                     
        AffineTransform t = new AffineTransform();
        t.scale(sx,sy); 
        t.concatenate(shape.getAffineTransform());
        shape.setAffineTransform(t); 
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文