Is it possible to draw an elliptical arc like SVG path, in CoreGraphics and how?
@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.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With