旋转多个形状并保持关系
我有几个形状想要旋转并在它们之间保持相同的距离。 最好的方法是什么? 我正在考虑计算包裹所选形状的矩形的中心,并与该点相比旋转形状
这是我的代码
while(iter.hasNext() ){
shape = (Shape)iter.next();
anchor = getCenter();
AffineTransform t = shape.getAffineTransform();
t.rotate(Math.toRadians(thetaDegrees), anchor.x, anchor.y);
shape.setAffineTransform(t);
}
谢谢
I have several shapes that I want to rotate and keep the same distance between them.
What's the best way to do that ?
I was thinking to calculate the center of the rectangle that wraps the selected shapes and rotate the shaped compared to that point
This is my code
while(iter.hasNext() ){
shape = (Shape)iter.next();
anchor = getCenter();
AffineTransform t = shape.getAffineTransform();
t.rotate(Math.toRadians(thetaDegrees), anchor.x, anchor.y);
shape.setAffineTransform(t);
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
原则上,旋转始终是一种保持距离的操作(数学术语中的等距操作,类似于平移,但与缩放和剪切操作不同)。因此,如果您围绕任何点旋转所有形状(以相同的角度),它总是会为您提供所需的属性。
In principle, rotation always is a distance-preserving operation (a isometry, in mathematical terms, like a translation, and unlike scaling and shearing operations). So, if you rotate all your shapes around any point (by the same angle), it always gives you the desired property.
我与之前的两个答案一致,旋转不应该改变形状的任何内容。
需要注意的是,您需要旋转一个形状,并且从原点旋转它。因此,形状中的每个顶点都是根据公共参考系进行测量的。
例如,如果有两个正方形 A 和 B,长度均为 10 个单位。一个错误是创建两个具有点 (0, 0) (0, 10) (10, 10) (10, 0) 的形状。例如,如果正方形 B 位于 A 上方,则形状 A 与之前一样,但形状 B 为 (0, 10) (10, 10) (20, 10), (10, 10))。现在你可以旋转 A 和 B,它们在旋转时将保持相对位置(如果我没记错的话,旋转通常以 (0,0) 为参考)
I coincide with the two previous answers, rotation should not change anything in a shape.
The caveat would be that you need to rotate ONE SHAPE, and you rotate it from the origin. So, each vertex in the shapes is measured against a common reference frame.
For example, if you have two squares A and B, both of 10 units of length. One mistake would be to create two Shapes with points (0, 0) (0, 10) (10, 10) (10, 0). If, for example, square B is over A, then you have Shape A as before, but Shape B is (0, 10) (10, 10) (20, 10), (10, 10)). Now you can rotate both A and B and they will keep their relative position when rotated (if I recall correctly, rotation is usually taking as reference (0,0))
只要您围绕同一点旋转每个对象(当然,以相同的角度),您就可以选择任何您想要的点作为中心。
常见的选择是边界框的并集中心、各个对象中心的平均值、中心的面积加权平均值等。您甚至可以围绕对象组之外的任意点旋转。这更多地取决于你想要实现什么而不是其他任何事情。
As long as you rotate every object around the same point (and by the same angle, of course), you can pick any point you want as the center.
Common choices are the center of the union of the bounding boxes, the average of the individual object centers, the area-weighted average of the centers, and so on. You can even rotate around an arbitrary point outside the group of objects. It depends more on what you want to achieve than anything else.
您需要保持对象的初始状态(即位置),应用保存的旋转位置,然后检查对象是否在边界内。如果它们在边界之外,那么您需要将它们以适当的距离向边界框的中心平移。
除非您有围绕其中心旋转的圆,否则旋转将始终修改边界框(增大或缩小它)。这就是为什么您需要始终保留原始参考并从中进行转换的原因。
You would need to keep an initial state of the objects (i.e. position), apply that saved position for rotation then check if the objects are within bounds. If they are outside the bounds then you need to translate them toward the center of the bounding box with the appropriate distance.
Unless you have circles that rotate around their center the rotation will always modify the bounding box (growing or shrinking it). That's why you would need to keep the original reference at all times and transform from that.