Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need examples for how to use CGAffineTransform in CGPaths

I don't understand how to apply "CGAffineTransform" parameter that appears in practically every CGPath method, e.g.:

void CGPathAddRect (
   CGMutablePathRef path,
   const CGAffineTransform *m,
   CGRect rect
);

Let's say I want to rotate a rect path, how would I write out this function? Where to I get the transform matrix?

like image 495
anna Avatar asked Nov 05 '10 20:11

anna


1 Answers

You use use CGAffineTransformMakeRotation to create a CGAffineTransform that rotates the rectangle about the point (0, 0).

 CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI / 4);  // π/4 = 45°
 CGPathAddRect(path, &rotation, CGRectMake(0, 0, 80, 40));

If you need it to rotate about any other points (x, y), you need to compose 2 translations to move (x, y) to (0, 0) and back.

like image 62
kennytm Avatar answered Oct 08 '22 19:10

kennytm