旋转和缩放——如何同时进行这两项操作并获得正确的结果?
我有一组 Java2D 调用,可以在图形上下文上绘制向量。 我希望图像大小加倍,然后旋转 90 度。
我使用以下代码来执行此操作:
Graphics2D g2 = // ... get graphics 2d somehow ...
AffineTransform oldTransform = g2.getTransform();
AffineTransform newTransform = (AffineTransform)oldTransform.clone();
newTransform.concatenate(AffineTransform.getTranslateInstance(x1, x2));
newTransform.concatenate(AffineTransform.getScaleInstance((double)newW/(double)iconW, (double)newH/(double)iconH));
newTransform.concatenate(AffineTransform.getRotateInstance(Math.toRadians(rotationAngle), (double)iconW/2.0d, (double)iconH/2.0d));
// ... do my drawing ...
这会旋转和缩放,但是,缩放比例并未按照我想要的方式应用。 就好像在缩放之前进行了旋转,从而使图像在错误的轴上变宽。
有一个更好的方法吗?
I've got a set of Java2D calls that draw vectors on a graphics context. I'd like for the image to be doubled in size and then rotated 90 degrees.
I'm using the following code to do this:
Graphics2D g2 = // ... get graphics 2d somehow ...
AffineTransform oldTransform = g2.getTransform();
AffineTransform newTransform = (AffineTransform)oldTransform.clone();
newTransform.concatenate(AffineTransform.getTranslateInstance(x1, x2));
newTransform.concatenate(AffineTransform.getScaleInstance((double)newW/(double)iconW, (double)newH/(double)iconH));
newTransform.concatenate(AffineTransform.getRotateInstance(Math.toRadians(rotationAngle), (double)iconW/2.0d, (double)iconH/2.0d));
// ... do my drawing ...
This rotates and scales, however, the scale isn't applied the way I would like. It is as if it is rotated before scaling, thus making the image wider on the wrong axis.
Is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信这些转换是像堆栈一样实现的 - 因此最后一个转换首先执行。 尝试颠倒旋转和缩放变换的顺序,您应该会得到您想要的结果。
I believe those transforms are implemented like a stack - so the last transform is performed first. Try reversing the order of the rotate and scale transformations and you should get what you are looking for.
旋转始终围绕原点进行。 为了围绕某个点旋转,您必须平移点。
此页面解释了您尝试执行的操作背后的数学原理,以及说明为什么需要按特定顺序应用转换。
Rotations are always performed about the origin. In order to rotate about a certain point you must translate the points.
This page explains the maths behind what you're trying to do and show why transformations need to be applied in a certain order.
更改连接变换的顺序以控制它们在组合中应用的顺序。
Change the order in which you concatenate the transforms to control the order in which they are applied in the composite.