How to draw a circle starting from the top

I am working with some sample code that draws an arc using CGPaths. I looked around and found the documentation, but I just can’t imagine what is happening in terms of using MoveToPoint, AddLineToPoint, etc. I cannot "see" what the code is doing. I can just see the result.

For example, the code below draws an arc of 360 degrees, starting at position 3 o hours. For life, I can’t understand how to make him start from a 12-hour position with the actual rotation of the view - 90 degrees.

Can someone help me understand this code and how I would change it to reach the beginning from 12 o’clock, it is advisable to try to explain how it all works. Or maybe point me to a visual resource on the Internet?

- (void)drawPathWithArc:(CGFloat)arc { CGMutablePathRef thePath = CGPathCreateMutable(); CGPathMoveToPoint(thePath, NULL, 100.f, 100.f); CGPathAddLineToPoint(thePath, NULL, 200.f, 100.f); CGPathAddArc(thePath, NULL, 100.f, 100.f, 100.f, 0.f, (360* M_PI)/180, NO); CGPathCloseSubpath(thePath); shapeLayer_.path = thePath; CGPathRelease(thePath); } 
+7
source share
1 answer

Ways are really not that difficult to understand visually. Basically, all the paths are the line connecting the two points on the Cartesian plane that defines the iphone screen.

When you move the ToPoint, it sets the current waypoint to the specified point.

When you add LineToPoint, it draws a straight line from the current point to the specified point.

When you add CurveToPoint, it draws a curved line from the current point to the specified point based on the defined tangent and control points.

And so on. I would recommend reading the apple documentation on CGPaths to better understand what each function does.

http://developer.apple.com/library/mac/#documentation/graphicsimaging/Reference/CGPath/Reference/reference.html


As far as your question is about starting with 12 instead of 3, just read the documentation for the CGPathAddArc function.

What you need to do is change the current code:

 CGPathAddArc(thePath, NULL, 100.f, 100.f, 100.f, 0.f, (360* M_PI)/180, NO); 

to:

 CGPathAddArc(thePath, NULL, 100.f, 100.f, 100.f, -M_PI_2, M_PI_2*3, NO); 

All this makes the launch angle change to -90 degrees (all angles are measured in radians from the horizontal) and the final angle is up to 270 degrees.


Hope this helps. Greetings

Brenton.

+16
source

All Articles