AffineTransform instance combination requires a specific order in Java?

I have a collection of AffineTranform instances. Depending on the specific conditions, I need to concatenate any of them - the conditions are not important here. In doing so, I found that the concatenation order seems to have some significance. Looking at an example, I:

  • one original tranformation 'original' that scales and translates2. one unit matrix 'one'
  • one identical matrix 'two'
  • one transformation scale that scales
  • one tranformation 'translate' that translates

In this example, I create the following combinations: 1. one x scale x translation 2. two scales x translatex

Following the Java documentation, matrices should be multiplied when concatenated, but looking at the result of the sample 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 a problem with Java or am I having an error in my code?
Thanks for any hint.

+4
source share
3 answers

Order matters. Look at your second concatenation, you are actually scaling your translation, as it is used before.

+1
source

Yes matrix multiplication order is important. Check out simple examples in which the same matrices are multiplied in a different order:

Example 1

Example 2

+1
source

Linear algebra (mathematical model for matrices) is different from basic algebra. One of the simplest differences is that multiplication is not commutative. Thus, A x B will give a different result than B x A.

0
source

Source: https://habr.com/ru/post/1395217/


All Articles