Java 中 AffineTransform 实例的串联是否需要特定的顺序?
我有 AffineTranform 实例的集合。根据具体条件,我需要连接其中任何一个 - 条件在这里并不重要。当这样做的时候,我发现串联的顺序似乎有些重要。看一下这个例子,我有:
- 一个原始的转换“原始”,它可以缩放和平移2。一个单位矩阵“一”
- 一个单位矩阵“二”
- 一个转换“缩放”
- 一个转换“翻译” 翻译
在示例中,我创建了以下组合: 1. 1 x 缩放 x 平移 2. 两个 x 平移缩放
按照 Java 文档,矩阵在连接时应该相乘,但是查看示例代码的输出会显示不同的结果。
Java 版本:Java 6 SE Update 30
示例:
package affinetransformationtest;
import java.awt.geom.AffineTransform;
public class AffineTransformationTest {
public static void main(String[] args) {
AffineTransform original = new AffineTransform(10, 0.0, 0.0, 100, 2, 3);
System.out.println("original: " + original);
System.out.println("");
AffineTransform scale = AffineTransform.getScaleInstance(10, 100);
AffineTransform translate= AffineTransform.getTranslateInstance(2, 3);
AffineTransform one = new AffineTransform();
System.out.println("one: " + one);
one.concatenate(scale);
System.out.println("one + scale: " + one);
one.concatenate(translate);
System.out.println("one + scale + translate: " + one);
System.out.println("Is one equal to original: " + original.equals(one)); //is false
System.out.println("");
AffineTransform two = new AffineTransform();
System.out.println("two: " + two);
two.concatenate(translate);
System.out.println("two + translate: " + two);
two.concatenate(scale);
System.out.println("two + translate + scale: " + two);
System.out.println("Is two equal to original: " + original.equals(two)); //is true
System.out.println("");
}
}
输出:
original: AffineTransform[[10.0, 0.0, 2.0], [0.0, 100.0, 3.0]]
one: AffineTransform[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
one + scale: AffineTransform[[10.0, 0.0, 0.0], [0.0, 100.0, 0.0]]
one + scale + translate: AffineTransform[[10.0, 0.0, 20.0], [0.0, 100.0, 300.0]]
Is one equal to original: false
two: AffineTransform[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
two + translate: AffineTransform[[1.0, 0.0, 2.0], [0.0, 1.0, 3.0]]
two + translate + scale: AffineTransform[[10.0, 0.0, 2.0], [0.0, 100.0, 3.0]]
Is two equal to original: true
Java 是否存在问题,或者我的代码是否有错误?
感谢您的任何提示。
I have a collection of AffineTranform instances. Depending on specific conditions I need to concatenate any of these - the conditions are not important here. When doing so, I discovered that the order of concatenation seems to be of some importance. Looking at the example, I have:
- one original tranformation 'original' which scales and translates2. one identity matrix 'one'
- one identity matrix 'two'
- one tranformation 'scale' which scales
- one tranformation 'translate' which translates
In the example I create the following combinations:
1. one x scale x translate
2. two x translatex scale
Following the Java documentation the matrices should be multiplied when concatenated, but looking at the output of the example code shows different results.
Java version: Java 6 SE Update 30
Example:
package affinetransformationtest;
import java.awt.geom.AffineTransform;
public class AffineTransformationTest {
public static void main(String[] args) {
AffineTransform original = new AffineTransform(10, 0.0, 0.0, 100, 2, 3);
System.out.println("original: " + original);
System.out.println("");
AffineTransform scale = AffineTransform.getScaleInstance(10, 100);
AffineTransform translate= AffineTransform.getTranslateInstance(2, 3);
AffineTransform one = new AffineTransform();
System.out.println("one: " + one);
one.concatenate(scale);
System.out.println("one + scale: " + one);
one.concatenate(translate);
System.out.println("one + scale + translate: " + one);
System.out.println("Is one equal to original: " + original.equals(one)); //is false
System.out.println("");
AffineTransform two = new AffineTransform();
System.out.println("two: " + two);
two.concatenate(translate);
System.out.println("two + translate: " + two);
two.concatenate(scale);
System.out.println("two + translate + scale: " + two);
System.out.println("Is two equal to original: " + original.equals(two)); //is true
System.out.println("");
}
}
Output:
original: AffineTransform[[10.0, 0.0, 2.0], [0.0, 100.0, 3.0]]
one: AffineTransform[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
one + scale: AffineTransform[[10.0, 0.0, 0.0], [0.0, 100.0, 0.0]]
one + scale + translate: AffineTransform[[10.0, 0.0, 20.0], [0.0, 100.0, 300.0]]
Is one equal to original: false
two: AffineTransform[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
two + translate: AffineTransform[[1.0, 0.0, 2.0], [0.0, 1.0, 3.0]]
two + translate + scale: AffineTransform[[10.0, 0.0, 2.0], [0.0, 100.0, 3.0]]
Is two equal to original: true
Is there an issue with Java or do I have a mistake in my code?
Thanks for any hint.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
顺序确实很重要。
看看你的第二个串联,你实际上也在缩放你的翻译,因为它之前已经应用过。
Order does matter.
Look at your second concatenation, you are actually scaling your translation as well since it is applied before.
是的,矩阵乘法的顺序很很重要。查看那些简单的示例,其中相同的矩阵以不同的顺序相乘:
示例1
示例2
Yes the order of matrix multiplication is important. Check out those simple Examples in which the same matrices are multiplied in different order:
Example1
Example2
线性代数(矩阵的数学模型)与基本代数不同。最简单的区别之一是乘法不可交换。因此 A x B 会得到与 B x A 不同的结果。
Linear algebra (the mathematical model for matrices) is different from basic algebra. One of the simplest differences is that multiplication is not commutative. So A x B will give a different result than B x A.