Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to understand iOS core graphics coordinate system transformation, need assistance

I am trying to understand quartz 2d coordinate system, Currently I am reading Apple reference guide and old book "Quartz 2d graphics for mac os x developer".

I understand the concept of user-space and device-space concept that device-space can have different default coordinate system and device-space coordinates can't be modified, and we map user-space by modifying its coordinate system acceding to device-space to achieve desire result.


  • First part of the problem

Quartz 2d graphics for mac os x developer book says:

When transforming a coordinate system, you must have another coordinate system to compare it to. The transformation provides a relative mapping from one coordinate system to the other. When you draw in the transformed coordinate system, the transformation maps the graphic back to the first coordinate system. The appearance of that graphic in the fixed coordinate system is affected by the transformation.

i didn't get this point in bold.

And

Quartz 2D programming guide says:

Quartz accomplishes device independence with a separate coordinate system—user space—mapping it to the coordinate system of the output device—device space—using the current transformation matrix, or CTM. A matrix is a mathematical construct used to efficiently describe a set of related equations. The current transformation matrix is a particular type of matrix called an affine transform, which maps points from one coordinate space to another by applying translation, rotation, and scaling operations (calculations that move, rotate, and resize a coordinate system).

The current transformation matrix has a secondary purpose: It allows you to transform how objects are drawn. For example, to draw a box rotated by 45 degrees, you rotate the coordinate system of the page (the CTM) before you draw the box. Quartz draws to the output device using the rotated coordinate system.

Confusion is that "Quartz draws to the output device using the rotated coordinate system.", if I want to draw one object(image etc) rotated and other with out rotation then what happen? We have whole coordinates rotated every thing will be drawn is rotated?

I am trying different experiments but unable to wrap my head around this, I create an image my drawing two line replicating bottom left coordinate system in photoshop and then added to my project to see visibly how coordinates are behaving by calling CGContextRotateCTM(myContext, 45); in drawrect method, but it didn't do any thing to image which I included in xib file using interface builder placing image inside uiimage.

this code is from quartz 2D programming guide

CGContextRef myContext = UIGraphicsGetCurrentContext();
CGRect contextRect = self.bounds;

CGContextTranslateCTM(myContext, 0, contextRect.size.height);
CGContextRotateCTM(myContext, 45); //my modification
CGContextScaleCTM(myContext, 1, -1);

float w, h;
w = contextRect.size.width;
h = contextRect.size.height;

CGAffineTransform myTextTransform;
CGContextSelectFont (myContext, "Helvetica-Bold", h/10, kCGEncodingMacRoman);
CGContextSetCharacterSpacing (myContext, 10);
CGContextSetTextDrawingMode (myContext, kCGTextFillStroke);

CGContextSetRGBFillColor (myContext, 0, 1, 0, .5);
CGContextSetRGBStrokeColor (myContext, 0, 0, 1, 1);
myTextTransform =  CGAffineTransformMakeRotation(0);
CGContextSetTextMatrix (myContext, myTextTransform);
CGContextShowTextAtPoint (myContext, 0, 50, "Quartz 2D", 9);

but this code is modifying the text drawn on screen not the image I added?


  • Second Part of the problem

This is also from Quartz 2D programming guide:

... apply a transform that translates the origin to the upper-left corner of the PDF context and scales the y-coordinate by -1.

Using a scaling transform to negate the y-coordinate alters some conventions in Quartz drawing. For example, if you call CGContextDrawImage to draw an image into the context, the image is modified by the transform when it is drawn into the destination. ...

I am already doing this -1 thing but no effect on image and this -1 thing is still not clear to me.

I have read this document several times, tried searching on google but no useful tutorial and also no latest books are available only books written in 2004, 2005, 2006. Can any one help regarding this or can refer me useful resources to learn this in depth.

Desperately waiting for replies, really need help. Thank you.

like image 699
Pamy Avatar asked Feb 23 '12 06:02

Pamy


1 Answers

Read this guide and I hope all your problems will be clear

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaDrawingGuide/Transforms/Transforms.html

like image 157
supera Avatar answered Nov 27 '22 23:11

supera