旋转和缩放——如何同时进行这两项操作并获得正确的结果?

发布于 2024-07-15 23:22:59 字数 715 浏览 11 评论 0原文

我有一组 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 技术交流群。

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

发布评论

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

评论(3

风蛊 2024-07-22 23:22:59

我相信这些转换是像堆栈一样实现的 - 因此最后一个转换首先执行。 尝试颠倒旋转和缩放变换的顺序,您应该会得到您想要的结果。

newTransform.concatenate(AffineTransform.getTranslateInstance(x1, x2));
newTransform.concatenate(AffineTransform.getRotateInstance(Math.toRadians(rotationAngle), (double)iconW/2.0d, (double)iconH/2.0d));
newTransform.concatenate(AffineTransform.getScaleInstance((double)newW/(double)iconW, (double)newH/(double)iconH));

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.

newTransform.concatenate(AffineTransform.getTranslateInstance(x1, x2));
newTransform.concatenate(AffineTransform.getRotateInstance(Math.toRadians(rotationAngle), (double)iconW/2.0d, (double)iconH/2.0d));
newTransform.concatenate(AffineTransform.getScaleInstance((double)newW/(double)iconW, (double)newH/(double)iconH));
把人绕傻吧 2024-07-22 23:22:59

旋转始终围绕原点进行。 为了围绕某个点旋转,您必须平移点。

此页面解释了您尝试执行的操作背后的数学原理,以及说明为什么需要按特定顺序应用转换。

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.

谎言月老 2024-07-22 23:22:59

更改连接变换的顺序以控制它们在组合中应用的顺序。

Change the order in which you concatenate the transforms to control the order in which they are applied in the composite.

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