Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset to identity the "current transformation matrix" with some CGContext function?

I'm doing a series of translations and rotations on the CTM and at some point I need to reset it to identity before going further with transformations.

I can't find any proper way to do it (obviously, there should have been a function named CGContextSetCTM or so) and since efficiency is the key, I don't want to use CGContextSaveGState/CGContextRestoreGState...

like image 727
Ariel Malka Avatar asked Jan 22 '09 15:01

Ariel Malka


3 Answers

Get current transformation matrix via CGContextGetCTM, invert it with CGAffineTransformInvert and multiply the current matrix by the inverted one (that's important!) with CGContextConcatCTM. CTM is now identity.

like image 144
EightyEight Avatar answered Oct 23 '22 13:10

EightyEight


The save/restore operations are probably a single memory copy of a memory region comparable to the size of the identity matrix (twice or thrice the size). It might only happen for the save operation. Consider that this is probably not much slower than a nop FUNCTION call. Each graphic operation is in the scale of several multiplication operation and I'm guessing this happens more than once in your code for each save/restore cycle. The time of one graphic operation is probably larger than a single save/restore cycle.

like image 40
Qwmiwn Avatar answered Oct 23 '22 13:10

Qwmiwn


Note that inverting the current CTM with CGAffineTransformInvert does not work if your current CTM is singular.

The obvious case is if previously CGContextConcatCTM was performed with matrix CGAffineTransformMake(0, 0, 0, 0, 0, 0).

like image 27
jhavatar Avatar answered Oct 23 '22 13:10

jhavatar