Maxim Veksler
Maxim Veksler

Reputation: 30232

CGAffineTransform Translation and Rotation produces unrounded transformation result

For given transformation calculation,

CGAffineTransform preferredTransform = CGAffineTransformMake(-1, 0, 0, -1, 1920, 1080);
CGAffineTransform transform1 = CGAffineTransformConcat(CGAffineTransformMakeTranslation(-preferredTransform.tx, -preferredTransform.ty), CGAffineTransformMakeRotation(M_PI));
NSLog(@"%@", NSStringFromCGAffineTransform(transform1));

The output would be [-1, 1.2246467991473532e-16, -1.2246467991473532e-16, -1, 1920.0000000000002, 1079.9999999999998] yet I would expect it to be [-1, 0, 0, -1, 1920, 1080]

Why the rounding errors? Should I apply the transformations differently to produce rounded results?

Upvotes: 1

Views: 382

Answers (2)

Alexander Kostiev
Alexander Kostiev

Reputation: 306

The specified rounding errors are due to 64-bit architecture. If you launch on 32-bit simulator, you shouldn't see those rounding errors.

Upvotes: 0

user3150031
user3150031

Reputation: 16

On a 32-bit iPad rounding is correct, However, on a 64-bit iPad the rounding errors occurs. Since rotation by M_PI is the same as scaling by -1, this can be easily fixed by changing it to:

CGAffineTransform preferredTransform = CGAffineTransformMake(-1, 0, 0, -1, 1920, 1080);
CGAffineTransform transform1 = CGAffineTransformConcat(CGAffineTransformMakeTranslation(-preferredTransform.tx, -preferredTransform.ty), **CGAffineTransformMakeScale(-1, -1)**);

NSLog(@"%@", NSStringFromCGAffineTransform(transform1));

Upvotes: 0

Related Questions