Home > Objective-C > A Couple CGAffineTransform Goodies
A Couple CGAffineTransform Goodies
Posted on Tuesday, February 15, 2011 by android apps market for tablests
Thanks to Core Animation, we iOS programmers tend to use affine transformations (by way of CGAffineTransform) a lot. By being able to combine multiple 2D transformations into a single matrix, we have the ability to do a lot of cool animation effects with only a few lines of code.
Take the following example, which is fairly typical:
Not bad, right? In just three lines of code, we're able to both scale and translate a view or layer. But in reality, there's actually quite a few operations going on behind these three lines of code. The CGAffineTransformScale() function calls CGAffineTransformConcat() to perform a matrix multiplication operation between two affine matrices. But, as you probably know, you can't multiply a 2x3 matrix by another 2x3 matrix. To multiply affine transformations, they have to be converted back to 3x3 vector matrices first.
On today's devices (even mobile devices), this all takes a trivial amount of processing power. But sometimes, when you're doing a lot of these transformations � say thousand or tens of thousands a second � it can be valuable to be able to avoid that conversion and matrix multiplication.
It just so happens that with certain commonly used CGAffineTransforms, you can cheat. Certain matrices can be joined together without performing matrix multiplication. For example, here are the matrices created by CGAffineTransformMakeScale() and CGAffineTransformMakeTranslation(), respectively:

Go ahead and multiply those two together. Plug in any number for tx, ty, sx, and sy and run the numbers. I'll wait. Okay, you don't have to. This is what you'll get:

So, if that's the result we're going to get, why bother going through the matrix multiplication in the first place? Why not just populate the matrix with both the scale and translate values right from the get-go? Well, we can. We can also do the same thing with translate and rotate.
This is all there is to it:
That's it. It only saves you two lines of code:
But, your stack allocation is considerably smaller (one CGAffineTransform instead of two CGAffineTransforms and an intermediate 3x3 array. It also saves you eighteen floating point multiplications and nine floating point additions. 99.9% of the time, that number of operations is going to have no noticeable affect on your application - it's a trivial amount of both memory and FLOPS under most normal situations.
But� if you're doing a lot per second, they can add up and it's nice to know there's a way that you can save yourself a little overhead in some situations.
Take the following example, which is fairly typical:
CGAffineTransform transform = CGAffineTransformMakeTranslation(0, -translation);
transform = CGAffineTransformScale(transform, scaleFactor, scaleFactor);
view.transform = transform;Not bad, right? In just three lines of code, we're able to both scale and translate a view or layer. But in reality, there's actually quite a few operations going on behind these three lines of code. The CGAffineTransformScale() function calls CGAffineTransformConcat() to perform a matrix multiplication operation between two affine matrices. But, as you probably know, you can't multiply a 2x3 matrix by another 2x3 matrix. To multiply affine transformations, they have to be converted back to 3x3 vector matrices first.
On today's devices (even mobile devices), this all takes a trivial amount of processing power. But sometimes, when you're doing a lot of these transformations � say thousand or tens of thousands a second � it can be valuable to be able to avoid that conversion and matrix multiplication.
It just so happens that with certain commonly used CGAffineTransforms, you can cheat. Certain matrices can be joined together without performing matrix multiplication. For example, here are the matrices created by CGAffineTransformMakeScale() and CGAffineTransformMakeTranslation(), respectively:

Go ahead and multiply those two together. Plug in any number for tx, ty, sx, and sy and run the numbers. I'll wait. Okay, you don't have to. This is what you'll get:

So, if that's the result we're going to get, why bother going through the matrix multiplication in the first place? Why not just populate the matrix with both the scale and translate values right from the get-go? Well, we can. We can also do the same thing with translate and rotate.
This is all there is to it:
static inline CGAffineTransform CGAffineTransformMakeRotateTranslate(CGFloat angle, CGFloat dx, CGFloat dy)
{
return CGAffineTransformMake(cosf(angle), sinf(angle), -sinf(angle), cosf(angle), dx, dy);
}
static inline CGAffineTransform CGAffineTransformMakeScaleTranslate(CGFloat sx, CGFloat sy, CGFloat dx, CGFloat dy)
{
return CGAffineTransformMake(sx, 0.f, 0.f, sy, dx, dy);
}That's it. It only saves you two lines of code:
view.transform = CGAffineTransformMakeScaleTranslate(scaleFactor, scaleFactor, 0, -translation);But, your stack allocation is considerably smaller (one CGAffineTransform instead of two CGAffineTransforms and an intermediate 3x3 array. It also saves you eighteen floating point multiplications and nine floating point additions. 99.9% of the time, that number of operations is going to have no noticeable affect on your application - it's a trivial amount of both memory and FLOPS under most normal situations.
But� if you're doing a lot per second, they can add up and it's nice to know there's a way that you can save yourself a little overhead in some situations.
Category Article CGAffineTransform, iPhone SDK, Objective-C
Powered by Blogger.
Blog Archive
-
▼
2011
(4034)
-
▼
February
(643)
- Rumor - White iPad 2nd generation
- Consumer Reports Digital Edition for iPad
- Viewing Feature Android Smartphone "Samsung I910 ...
- February Reflections
- Best Evers: Laura Kelso, Writer
- Motorola Xoom overclocked to 1.5GHz, eats Quadrant...
- FORMWARE PR 2.0 DEMOED ON THHE NOKIA N8
- Gmail accidentally resetting accounts, years of co...
- SONY ERICSSON XPERIA ARC PINK - PICTURES
- ANDROID APP : MEGAVIDEO
- WORMS HD DEMOED ON THE NOKIA N8
- Dev Team Working on iTunes Alternative for Jailbre...
- Dev Team Working on iTunes Alternative for Jailbre...
- Ultrasn0w 1.3 May Also Support iPhone 4 Baseband 0...
- Ultrasn0w 1.3 May Also Support iPhone 4 Baseband 0...
- iPad 2 may also comes in white?
- iPad 2 may also comes in white?
- Apple working on Android-like gesture-based lock s...
- Apple working on Android-like gesture-based lock s...
- Download Cheat engine 5.5 Free Update
- Cheats for The Sims 3 PC
- Cheat Engine CE 5.6.1 Terbaru Download Free
- Update Security dan Definition for Microsoft Offic...
- Info Specification " Sony Ericsson Experia Pro: Th...
- LG Optimus Me P350 To Fight with Samsung Galaxy Mi...
- Sunday Salon: Week In Review #9
- Update on iPhone 4 Unlock on Baseband 3.10.01 and ...
- Update on iPhone 4 Unlock on Baseband 3.10.01 and ...
- Nexus S 2.3.3 update adjusts screen's color temper...
- HTC Incredible S, Desire HD, Desire Z and original...
- WINDOWS PHONE CAMPAIGN OVERVIEW
- WORMS HD AVAILABLE FOR SYMBIAN^3 PHONES
- Critical Dodge 2011 Ninja Saga Februari Cheat Update
- 28 29 Februari Cheat PB Point Blank Blooderz
- 2829 Februari Ninja Saga Engine Cheat 2011
- FullForce Forces iPhone Apps into Full View no mor...
- ManualCorrect Puts iPad Auto Correct in Check
- Wunderlist HD - Cloud-sync task manager for iPad
- How To Manage Subscriptions on iPad
- Sprint HTC Arrive with Windows Phone 7, first cop...
- Visualized: the HTC keyboard slider family
- Windows Phone's new UI and Xbox games
- one-cubic-millimeter computer, want to stick it in...
- New Apple MacBook Pro Reviewed
- Small Persons With Wings (MG)
- Library Loot: Fifth Trip in February
- North and South (Revisited)
- Sprint HTC Arrive with Windows Phone 7 copy and pa...
- ANGRY BIRDS COMING SOON TO WINDOWS PHONE 7
- Windows Phone's new UI and Xbox games are most exc...
- HTC Merge first hands-on!
- GOOGLE SKY ON WINDOWS PHONE 7
- Ixonos showcasing extensive Meego and Android expe...
- 27 Februari 2011 Hack v1.2 Update Point Blank Cheat
- Avira Premium 10 License Key Download Free
- Leaked Panel Lends Credence to Larger Screen and T...
- Leaked Panel Lends Credence to Larger Screen and T...
- Full Version Crack Adobe Photoshop CS5 12.0 Download
- Play PS3 Games on iPhone, iPod Touch and iPad with...
- Play PS3 Games on iPhone, iPod Touch and iPad with...
- Adobe Photoshop CS4 11.0 Full Version Download Cr...
- DeskTop Author Software Untuk Membuat Aplikasi EBook
- Samsung Galaxy Mini S5570 bundled with simpati
- Photoshop CS3 10.0 Download Full Version Crack Keygen
- 10 PC Gaming Terbaik 2011
- Nexian Tap Taouchscreen Promo Price at Hypermart, ...
- Cheat PB Storm Injector V.2.0 Download Released Fe...
- Cheat 26 Februari gc-evolution-v-11-full-hack 2011
- Download Maphack Gaspooll v1.10 Cheat Point Blank ...
- Windows XP iPod touch 2G Test 1 [Video]
- Windows XP iPod touch 2G Test 1 [Video]
- Sources: iPad 2 lands in thinner body with spec bu...
- NOKIA N8 FIRMWARE PR2 PREVIEW
- Official Android 2.3.3 updates for Nexus One and N...
- HTC Thunderbolt delayed until March 4th, says a Be...
- WINDOWS PHONE 7 -WHAT IF?
- REVIEW : MOTOROLA XOOM ANDROID TABLET
- NoVoiceMail Nixes Your iPhone�s Voicemail Button
- NoVoiceMail Nixes Your iPhone�s Voicemail Button
- Apple Set To Present FaceTime HD, Thunder Bolt I/O?
- Apple Set To Present FaceTime HD, Thunder Bolt I/O?
- Geohot Interviewed Again Over Sony Lawsuit
- Geohot Interviewed Again Over Sony Lawsuit
- Last Minute Engineering Issues Led Apple To Pull R...
- Last Minute Engineering Issues Led Apple To Pull R...
- �Undeletable Apps� Keeps Your iPhone Apps Secure
- �Undeletable Apps� Keeps Your iPhone Apps Secure
- Download Resetter Printer Canon iPTool MPTool i320...
- You Can�t Spell Hydrate Without Rate
- Samsung Galaxy S2 16GB Opened for Pre-Order in Swi...
- Download RiNocomp v.3.8 Cara Penggunaan Cheat Poin...
- Cheat 26 Februari FTF_Interaction 3 [Wallhack + FI...
- Komunitas Point Blank Facebook
- ACDSee FotoSlate 4 Photo Print Studio 4.0.146 Full...
- Download Internet Explorer 9 RC/Beta Offline Windo...
- iPad 2 Announced March 2
- How to Add Contact to Group on iPad
- Maximum Mail Attachment Size Limit on iPad
- NOOSY iPad HDMI adapter
- iBooks Text to Speech Feature on iPad
-
▼
February
(643)