如何缩放许多形状并保持相同的比例
我有两个形状,我想进行缩放,我使用以下代码
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);
}
形状被缩放,但它们之间的距离变小
我想用这两个形状制作一个复合形状,然后缩放它。 还有其他方法可以保持比例吗?谢谢
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
I thought making a composite shape from the two shapes and then scale it.
Is there another way to keep the proportions? thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
就像我在评论中所说的那样,因为您围绕各自的中心缩放形状,所以这些中心显然不会移动。如果您希望所有内容统一缩放,请不要平移任何内容:
计算这些变换的最佳方法是拿起铅笔和纸,绘制坐标系并遵循纸上的变换。
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:
The best way to figure these transformations out is to grab pencil and paper, draw a coordinate system and follow the transformations on paper.
您拥有的代码会围绕其中心缩放每个形状。这意味着下面的线向下移动,上面的线向上移动。这就是为什么他们似乎一起移动。
为了避免这种情况发生,您必须围绕下边界(对于上部形状)和上边界(对于下部形状)上的点缩放形状。使“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.
如果你想要真正的缩放,这意味着缩放形状和它们之间的空白。如果您遵循幼稚的路线(缩放每个形状,然后缩放从一个形状到每个其他形状的距离),结果将不正确。
我知道的一种方法是将两个形状中心之间的距离视为具有固定方向的“吸引”力的矢量。然后衡量这些力的大小并找到使系统再次达到平衡的点。数学应该就在那里,某个地方。我很久以前就看到了这一切,就像是在另一种生活中一样。
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.
谢谢大家的回答,我找到了适合我的解决方案
Thank you everybody for the answers, I found a solution that works for me