user519274
user519274

Reputation: 1144

Quartz 2D drawing primitives Rotation

I'm new to Quartz 2D. I'm trying to draw a triangle then rotate. With my limited background using Quartz 2D I found from Apple/googling that i can use CGContextRotateCTM function . My problem is when i do that the whole text i draw after that is also rotated. I tried using CGContextSaveGstate and restoring it after i do the rotation but didnt work. I'm wondering if my approach is correct? Or there is a better way I can use to achieve that?

  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextSaveGState(context);
  for (key in data) 
  {
    // get point 
    Data *tmpdata =[data objectForKey:key]  ;
    point=[data point ] 
    //setup and draw the 
    CGContextBeginPath(context);
    CGContextSetRGBFillColor(context, [data fillcolor].r, 
        [data fillcolor].g, [tmpACdata fillcolor].b, 1);
    CGContextSetLineWidth(context, 2.0

    // Draw Triangle         
    CGContextMoveToPoint(context,point.x,point.y);
    CGContextAddLineToPoint(context, point.x+8, point.y+8);
    CGContextAddLineToPoint(context, point.x-8, point.y+8); 

    CGContextClosePath(context);
    CGContextDrawPath(context, kCGPathFill);

    CGContextRotateCTM(context, [data heading]* M_PI/180);

    CGContextClosePath(context);
    CGContextDrawPath(context, kCGPathFill);
    // Draw Text
    ...............
  }
  CGContextRestoreGState(context);

Upvotes: 2

Views: 1198

Answers (3)

user519274
user519274

Reputation: 1144

okay , Sorry for not replaying back fast enough was finishing couple of finals. I did exactly what you pointed out and i got it working . here is the code maybe it would help someone else

CGContextSaveGState(context);
CGContextBeginPath(context);        
CGContextTranslateCTM(context, point.x, point.y);
//CGContextScaleCTM(context, 1.0, -1.0); that didnt work         
CGContextRotateCTM(context, [data heading]* M_PI/180) ;
CGContextSetRGBFillColor(context, [data fillcolor].r, [data fillcolor].g, [data fillcolor].b, 1);

//Draw Triangle
CGContextMoveToPoint(context,0,0);
CGContextAddLineToPoint(context, 10, 10);
CGContextAddLineToPoint(context, 0, 6);
CGContextAddLineToPoint(context, -10,10);            
CGContextRotateCTM(context,(-1.0)* [tmpACdata heading]* M_PI/180);
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFill);'   
CGContextRestoreGState(context);

Thanks for your help it was great . Do you have any suggestion for a good practical Quartz 2D book ! Apple doc was a bit helpful but not that great to understand the concept ..etc

Upvotes: 3

Nico
Nico

Reputation: 3826

CGContextRotateCTM(context, [data heading]* M_PI/180);

CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFill);

CGContextRotateCTM(context, -[data heading]* M_PI/180);

// Draw Text

Upvotes: 0

Adrian
Adrian

Reputation: 51

Try this:

CGContextSaveGState(context);
CGContextRotateCTM(context, [data heading]* M_PI/180);
// draw triangle
CGContextRestoreGState(context);

and only then

// Draw Text

Upvotes: 2

Related Questions