组合多个仿射变换的方法,就好像每个仿射变换都是在未变换的空间中指定的一样

发布于 2024-11-04 03:10:22 字数 2585 浏览 3 评论 0原文

我正在寻找一种以这种方式组合仿射变换的方法,以便其效果相当于使用每个变换连续操作一个形状。问题是,如果我简单地连接变换,那么每个连续变换的效果都会在现有变换的坐标空间中解释。

例如,考虑围绕原点 (-50,-50, 100,100) 的正方形。我想旋转它,然后将其向下平移 100px。如果我进行变换并旋转,然后平移,则平移将在旋转坐标中解释。相反,如果我变换形状本身以旋转它,然后再次变换该形状以平移它,则两种平移都会在“正常”未平移平面中解释,并且它给了我我想要的东西。

问题是,对于我正在做的事情,可能会发生许多变换,每个变换都需要在法线坐标平面中进行解释,但我不想存储一堆变换,也不能简单地继续操作形状,因为我需要随时能够从原始起始形状创建最终的变换形状。

示例:左侧正常串联,右侧连续变换

我知道对于这个简单的示例,如果我执行了在旋转之前翻译我会得到相同的结果,但这没有抓住要点。我正在处理任意一组连续的缩放、平移和旋转变换,因此简单地将它们按一定的顺序排列并不能解决问题。

我有一种预感,应该有一种方法可以连接变换,这样您可以在连接新变换之前对其进行修改,纠正现有变换,以便效果是新变换似乎已被应用,就好像它已被应用一样参考未变换的坐标平面。例如,如果在上例中翻译为 (70.7, 70.7) 而不是 (0,100),则结果将相等。我似乎无法弄清楚一般来说如何改变新的转换以使其正确运行的数学是什么。

感谢您的阅读 - 希望这是有道理的。以下是创建屏幕截图的示例的来源:(

public class TransformExample extends JPanel {

@Override
protected void paintComponent(Graphics _g) {
    super.paintComponent(_g);
    Graphics2D g = (Graphics2D) _g;
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g.translate(150, 100); // translate so we can see method 1 clearly
    paintConcatenate(g);
    g.translate(200, 0); // translate again so we can see method 2 to the right of method 1
    paintSuccessive(g);
}

private void paintConcatenate(Graphics2D g) {
    AffineTransform tx = new AffineTransform();
    Shape shape = new Rectangle(-50, -50, 100, 100);

    // Draw the 3 steps, altering the transform each time
    draw(g, shape, tx, Color.GRAY);
    tx.rotate(Math.PI / 4);
    draw(g, shape, tx, Color.GREEN);
    tx.translate(70.7, 70.7);
    draw(g, shape, tx, Color.PINK);
}

private void paintSuccessive(Graphics2D g) {
    Shape shape = new Rectangle(-50, -50, 100, 100);

    // Draw the 3 steps, altering the shape each time with a new transform
    draw(g, shape, null, Color.GRAY);
    shape = AffineTransform.getRotateInstance(Math.PI / 4).createTransformedShape(shape);
    draw(g, shape, null, Color.GREEN);
    shape = AffineTransform.getTranslateInstance(0, 100).createTransformedShape(shape);
    draw(g, shape, null, Color.PINK);
}

private void draw(Graphics2D g, Shape shape, AffineTransform tx, Color color) {
    if (tx != null) {
        shape = tx.createTransformedShape(shape);
    }
    g.setColor(color);
    g.fill(shape);
}

public static void main(String[] args) {
    JFrame f = new JFrame("Transform Example");
    f.setSize(500, 350);
    f.setContentPane(new TransformExample());
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

}

我正在使用 Java2D,尽管我认为语言或 2d 库与这里无关。)

I'm looking for a way to combine affine transforms in such a way so that the effect is equivalent to using each transform to manipulate a shape in succession. The problem is that if I simply concatenate the transforms, then each successive transform's effect is interpreted in the existing transform's co-ordinate space.

For example, consider a square around the origin (-50,-50, 100,100). I want to rotate it, and then translate it down 100px. If I take a transform and rotate and then translate, the translation gets interpreted in the rotated coordinates. Instead, if I transform the shape itself to rotate it, and then transform that shape again to translate it, both translations are interpreted in the "normal" un-translated plane, and it gives me what I want.

The problem is that for what I'm doing many transforms may take place, each of which needs to be interpreted in the normal coordinate plane, but I don't want to store a stack of transforms, nor can I simply keep manipulating a shape, because I need to at any time be able to create the final transformed shape from the original starting shape.

Example: normal concatenation on the left, successive transforms on the right

I'm aware that for this simple example if I did the translate before the rotate I'd get the same result, but that's missing the point. I'm dealing with an arbitrary set of successive scale, translate, and rotate transforms, so simply putting them in a certain order doesn't cut it.

I have an inkling that there should be a way to concatenate transforms in such a way that you modify the new transform before you concatenate it, correcting for the existing transform so that the effect is that the new transform appears to have been applied as if it were referencing the un-transformed coordinate plane. For example, if you translate by (70.7, 70.7) in the above example instead of (0,100), the result becomes equivalent. I just can't seem to figure out what the math is to figure out in general how to alter the new transform so it works out correctly.

Thanks for reading - hope this made sense. Heres the source of the example that created the screenshot:

public class TransformExample extends JPanel {

@Override
protected void paintComponent(Graphics _g) {
    super.paintComponent(_g);
    Graphics2D g = (Graphics2D) _g;
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g.translate(150, 100); // translate so we can see method 1 clearly
    paintConcatenate(g);
    g.translate(200, 0); // translate again so we can see method 2 to the right of method 1
    paintSuccessive(g);
}

private void paintConcatenate(Graphics2D g) {
    AffineTransform tx = new AffineTransform();
    Shape shape = new Rectangle(-50, -50, 100, 100);

    // Draw the 3 steps, altering the transform each time
    draw(g, shape, tx, Color.GRAY);
    tx.rotate(Math.PI / 4);
    draw(g, shape, tx, Color.GREEN);
    tx.translate(70.7, 70.7);
    draw(g, shape, tx, Color.PINK);
}

private void paintSuccessive(Graphics2D g) {
    Shape shape = new Rectangle(-50, -50, 100, 100);

    // Draw the 3 steps, altering the shape each time with a new transform
    draw(g, shape, null, Color.GRAY);
    shape = AffineTransform.getRotateInstance(Math.PI / 4).createTransformedShape(shape);
    draw(g, shape, null, Color.GREEN);
    shape = AffineTransform.getTranslateInstance(0, 100).createTransformedShape(shape);
    draw(g, shape, null, Color.PINK);
}

private void draw(Graphics2D g, Shape shape, AffineTransform tx, Color color) {
    if (tx != null) {
        shape = tx.createTransformedShape(shape);
    }
    g.setColor(color);
    g.fill(shape);
}

public static void main(String[] args) {
    JFrame f = new JFrame("Transform Example");
    f.setSize(500, 350);
    f.setContentPane(new TransformExample());
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

}

(I'm working with Java2D, although I don't think the language or 2d library is all that pertinent here.)

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

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

发布评论

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

评论(2

独夜无伴 2024-11-11 03:10:22

我建议您跟踪一些绝对值,然后尽可能少地进行转换。

例如,存储平移矩阵和绕原点的旋转角度。

int translate[2];
int rotate;

现在,假设您要绕其中心旋转,然后将对象平移到某处,然后在其中心下再次旋转它。
因为在仿射变换中,旋转矩阵不可交换,所以如果您应用旋转、平移、旋转,您将得到错误的结果。

但您可以简单地将第一次和第三次旋转的旋转角度相加,然后应用一次旋转,然后应用平移。

希望能说清楚。

I suggest you to keep track of some absolute values and then do less transformations as you can.

For example, store the translation matrix and the rotation angle around the origin.

int translate[2];
int rotate;

Now, suppose that you want to rotate around its center and then translate the object somewhere, and then rotate it again under its center.
Because with affine transformations, rotation matrix aren't commutative, so if you apply a rotation,translation, rotation you'll get an wrong result.

But you can simply sum the rotation angle of the first and third rotation, and apply a single rotation and then the translation.

Hope to be clear.

临风闻羌笛 2024-11-11 03:10:22

当您旋转对象时,通常会围绕特定点旋转。看起来你只是围绕 (0,0) 旋转,这通常不是你想要的。

要绕特定点 (x,y) 旋转,

  • 请将该点平移到 0 (-x, -y),
  • 然后旋转,
  • 然后平移回 (x, y)。

    公共静态AffineTransform getRotateInstance(双θ,
    双锚x,
    双锚点)

when you rotate an object, you normally rotate around a specific point. It looks like you are just rotating around (0,0) which is usually not what you want.

To rotate around a specific point (x,y),

  • translate the point to 0 (-x, -y),
  • then rotate,
  • then translate back (x, y).

    public static AffineTransform getRotateInstance(double theta,
    double anchorx,
    double anchory)

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