Pegah
Pegah

Reputation: 682

In iOS, arcs are malformed for certain start angles

I use the following code to draw an arc

    double radius = 358.40001058578491;
    startAngle = 0.13541347644783652;
    double center_x= 684;
    double center_y = 440;
    std::complex<double> start1(   std::polar(radius,startAngle) );

    CGPoint targetStart1 = CGPointMake(start1.real() + center_x, start1.imag() +center_y);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, targetStart1.x, targetStart1.y);
    CGPathAddArc(path, NULL, center_x, center_y, radius, startAngle, 0.785,   0 );
    CGContextAddPath(context, path);
    CGContextSetLineWidth( context,  30 );

    CGContextSetStrokeColorWithColor( context, targetColor.CGColor);

    CGContextStrokePath(context);

    CGPathRelease(path);

If u check it in retina, it looks like this:

enter image description here

My arc is the green arc. I have shown the place that the start angle is with a orange line. As I have shown in the red rectangle, there is an extra thing drawn in the very beginning of the arc. This happens not for all start angles, but only for certain start angles.

Do you have any idea why it happens?

Thanks.

Upvotes: 2

Views: 101

Answers (1)

Rob
Rob

Reputation: 437592

In your original question, you specified a literal starting point that was not quite right and, as a result, Core Graphics will draw a line from that point to the start of the arc. And because that starting point was just a few pixels away from the actual start of the arc, it results in that curious rendering you illustrate in your question.

In your revised question, you're calculating the starting point, but I might suggest calculating it programmatically like so:

CGFloat centerX    = 684.0;
CGFloat centerY    = 440.0;
CGFloat radius     = 360.0;
CGFloat startAngle = 0.135;
CGFloat endAngle   = 0.785;

CGFloat startingX = centerX + radius * cosf(startAngle);
CGFloat startingY = centerY + radius * sinf(startAngle);
CGContextMoveToPoint(context, startingX, startingY);

CGContextAddArc(context, centerX, centerY, radius, startAngle, endAngle, 0);

CGContextSetLineWidth(context, 30);
CGContextSetStrokeColorWithColor(context, targetColor.CGColor);
CGContextStrokePath(context);

When I calculated it this way, there was no rounding errors that resulted in the artifact illustrated in your original question.


Note, if you're not drawing anything before the arc, you can just omit the CGContextMoveToPoint call altogether. You only need that "move to point" call if you've drawn something before the arc and don't want the path connecting from that CGContextGetPathCurrentPoint to the start of the arc.

Upvotes: 3

Related Questions