Straight Rectangular Graphics

Using this formula, I got the 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 a rectangle around a corner?

+7
source share
1 answer

Here's how you do it:

 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); 
+27
source

All Articles