Bug 31277 - CGAffineTransform incorrectly transforms the coordinates
Summary: CGAffineTransform incorrectly transforms the coordinates
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 8.10
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2015-06-22 05:45 UTC by Prashant Cholachagudda
Modified: 2015-08-03 07:47 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Prashant Cholachagudda 2015-06-22 05:45:27 UTC
Take the following piece of code:

            // Get identity transform
            CGAffineTransform transform1 = CGAffineTransform.MakeIdentity();
            CGAffineTransform transform2 = CGAffineTransform.MakeIdentity();

            // Apply translation of 100, 100 followed by rotation 45 degrees
            transform1.Rotate ((float)Math.PI/4);

            // Apply translation of 100, 100 followed by rotation 45 degrees
            transform2 = CGAffineTransform.Translate (transform2, 100, 100);
            transform2 = CGAffineTransform.Rotate (transform2, (float)Math.PI / 4);

            // They should be the same
            Console.WriteLine ("transform1 = " + transform1.ToString ());
            Console.WriteLine ("transform2 = " + transform2.ToString ());

The two resulting transforms should be the same, but the output is:

2015-06-19 17:03:55.203 CGAffineTransform[70700:14724843] transform1 = xx:0.71 yx:0.71 xy:-0.71 yy:0.71 x0:0.0 y0:141.42
2015-06-19 17:03:55.203 CGAffineTransform[70700:14724843] transform2 = xx:0.71 yx:0.71 xy:-0.71 yy:0.71 x0:100.0 y0:100.0

Transform 2 is correct, transform1 wrong !


Xamarin Studio
Version 5.8.3 (build 1)
Installation UUID: 78541bc2-394f-4fef-928b-161c24e56604
	Mono 3.12.1 ((detached/0849ec7)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 312010003

Version: (Business Edition)
Android SDK: /Users/markw/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		2.3    (API level 10)
		4.0.3  (API level 15)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

Xamarin Android Player
Version: Unknown version
Location: /Applications/Xamarin Android Player.app

Apple Developer Tools
Xcode 6.3.2 (7718)
Build 6D2105

Version: (Business Edition)
Hash: e6ebd18
Branch: master
Build date: 2015-05-21 21:55:09-0400

Not Installed

Build Information
Release ID: 508030001
Git revision: 6e8e725e0d689351901c2c70453bfa4ea25e293b
Build date: 2015-04-06 20:31:47-04
Xamarin addins: 051cd5f8c1b5dbfc87eaef80a74aec03f34c60a8

Operating System
Mac OS X 10.10.3
Darwin iMac.local 14.3.0 Darwin Kernel Version 14.3.0
    Mon Mar 23 11:59:05 PDT 2015
    root:xnu-2782.20.48~5/RELEASE_X86_64 x86_64
Comment 1 Rolf Bjarne Kvinge [MSFT] 2015-08-03 07:47:50 UTC
This is an implementation difference between these two methods (difference in matrix multiplication order).

// I = identify matrix
// R = rotation transformation matrix
// T = translation transformation matrix

> var I = CGAffineTransform.MakeIdentity ();
> var R = CGAffineTransform.MakeRotation ((float) Math.PI / 4);
> var T = CGAffineTransform.MakeTranslation (100, 100);

// I*T and T*I are identical:

> var ti = T * I;
> var it = I * T;
> Console.WriteLine ("TI == IT: {0} == {1}", ti, it);
>> "TI == IT: xx:1.0 yx:0.0 xy:0.0 yy:1.0 x0:100.0 y0:100.0 == xx:1.0 yx:0.0 xy:0.0 yy:1.0 x0:100.0 y0:100.0"

// However the operand order for rotation matters: 

> var r_it = R * it;
> var it_r = it * R;
> Console.WriteLine ("R * (I * T): {0}", r_it);
> Console.WriteLine ("(I * T) * R: {0}", it_r);
>> "R * (I * T): xx:0.71 yx:0.71 xy:-0.71 yy:0.71 x0:100.0 y0:100.0"
>> "(I * T) * R: xx:0.71 yx:0.71 xy:-0.71 yy:0.71 x0:0.0 y0:141.42

Back to the original question:

* The instance CGAffineTransform.Rotate (float) method does:

> self = self * R

* The static CGAffineTransform.Rotate (CGAffineTransform, float) does:

> return R * self;

which is why you're getting different results.

The reason behind this difference his historical, and can't be changed now (it would break all apps currently using these API).