Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core graphics rotate rectangle

With this formula I got angle

double rotateAngle = atan2(y,x)

with this code I can draw a rectangle

CGRect rect = CGRectMake(x,y , width ,height);
CGContextAddRect(context, rect);
CGContextStrokePath(context);

How can I rotate the rectangle around the angle ?

like image 502
user1125890 Avatar asked Jan 07 '12 23:01

user1125890


1 Answers

Here's how you'd do that:

CGContextSaveGState(context);

CGFloat halfWidth = width / 2.0;
CGFloat halfHeight = height / 2.0;
CGPoint center = CGPointMake(x + halfWidth, y + halfHeight);

// Move to the center of the rectangle:
CGContextTranslateCTM(context, center.x, center.y);
// Rotate:
CGContextRotateCTM(context, rotateAngle);
// Draw the rectangle centered about the center:
CGRect rect = CGRectMake(-halfWidth, -halfHeight, width, height);
CGContextAddRect(context, rect);
CGContextStrokePath(context);

CGContextRestoreGState(context);
like image 95
Steve Avatar answered Nov 06 '22 13:11

Steve