8

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.

Pegah
  • 672
  • 2
  • 13
  • 23
  • 4
    Remember that UIKit and CoreGraphics have different coordinate systems : http://www.informit.com/articles/article.aspx?p=2149190&seqNum=11 – Vinzzz Jul 16 '14 at 14:07
  • @Vinzzz Do u mean that in CGPathAddArc clockwise means unclockwise? Then why is it not mentioned in the documents? – Pegah Jul 16 '14 at 14:16
  • @Vinzzz But I think the 0,0 coordinate is top left here. Because with this assumption the position of targetStart1 apparently is correct. – Pegah Jul 16 '14 at 14:19

1 Answers1

8

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.

Tommy
  • 99,986
  • 12
  • 185
  • 204
  • 1
    Yes. In Core Graphics the origin is in the bottom left. In UIKit it is in the top left. So UIKit will display Core Graphics buffers upside down. Your origin being at the top left **proves that's your problem**. Flipping horizontally changes clockwise to anticlockwise and vice versa. – Tommy Jul 16 '14 at 14:39