Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw an elliptical arc with CoreGraphics?

Is it possible to draw an elliptical arc like SVG path, in CoreGraphics and how?

like image 987
Amr Elsehemy Avatar asked Jul 06 '12 16:07

Amr Elsehemy


People also ask

How do you draw an elliptical arc in latex?

@sandu Try \draw (3,0) ++ ({3*cos(80)},{1*sin(80)}) arc (80:180:3 and 1); for an arc with center at (3,0) , start angle of 80 , end angle of 180 , x radius of 3 and y radius of 1 . Compare with \draw[dashed,gray] (3,0) circle (3 and 1); to see the complete ellipse.


1 Answers

I ran into the same thing tonight. CG doesn't offer an easy way to draw a non-circular arc, but you can do it using CGPath and a suitable transformation matrix. Suppose you want an arc of an axis-aligned ellipse starting at left,top and with a size of width,height. Then you can do something like this:

CGFloat cx = left + width*0.5;
CGFloat cy = top + height*0.5;
CGFloat r = width*0.5;

CGMutablePathRef path = CGPathCreateMutable();
CGAffineTransform t = CGAffineTransformMakeTranslation(cx, cy);
t = CGAffineTransformConcat(CGAffineTransformMakeScale(1.0, height/width), t);
CGPathAddArc(path, &t, 0, 0, r, startAngle, endAngle, false);
CGContextAddPath(g->cg, path);

CGContextStrokePath(g);

CFRelease(path);

Note that if you want to draw a pie-shaped wedge, then just surround the "CGContextAddPath" call with a CGContextMoveToPoint(cx,cy) and a CGContextAddLineToPoint(cx,cy), and use CGContextFillPath instead of CGContextStrokePath. (Or if you want to fill and stroke at the same time, use CGContextDrawPath.)

like image 79
Joe Strout Avatar answered Sep 23 '22 23:09

Joe Strout