Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

draw an arc clockwise in iOS results in unclockwise arc

Tags:

ios

cgpath

I use the following code to draw an arc in the lower half of a circle.

According to documentation in Apple:

This method creates an open subpath. The created arc lies on the perimeter of the specified circle. When drawn in the default coordinate system, the start and end angles are based on the unit circle shown in Figure 1. For example, specifying a start angle of 0 radians, an end angle of π radians, and setting the clockwise parameter to YES draws the bottom half of the circle. However, specifying the same start and end angles but setting the clockwise parameter set to NO draws the top half of the circle.

enter image description here I wrote

    std::complex<double> start1(   std::polar(190.0,0.0) );


    CGPoint targetStart1 = CGPointMake(start1.real() + 342.0, start1.imag() + 220.);

    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, targetStart1.x, targetStart1.y);
    CGPathAddArc(path, NULL, 342.0, 220.0, 190, 0.0, PI_180,   YES );
    CGContextAddPath(context, path);
    CGContextSetLineWidth( context, 15.0  );

    // set the color for the stroked circle
    CGContextSetStrokeColorWithColor( context, [UIColor greenColor].CGColor);

    CGContextStrokePath(context);
    CGPathRelease(path);

However, the arc is drawn in the top half of the circle (see the green ring). I am wondering if I have accidentally somewhere in the code flipped the coordination system, but I don't know for what command I should search.

enter image description here

Thanks for ur help.

like image 375
Pegah Avatar asked Jul 16 '14 14:07

Pegah


1 Answers

As per Vinzzz's comment: for some opaque reason, Apple flipped the window manager's coordinate system between OS X and iOS. OS X uses graph paper layout with the origin in the lower left and positive y proceeding upwards. UIKit uses English reading order with the origin in the upper left and positive y proceeding downwards.

Core Graphics retains the OS X approach; UIKit simply presents its output upside down. Throw some Core Text in if you really want to convince yourself.

The standard solution is to translate and scale the current transform matrix as the first two steps in any drawRect:, effectively moving the origin back to the lower left for that view and flipping the y coordinate axis.

like image 99
Tommy Avatar answered Oct 30 '22 13:10

Tommy