Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a circle starting at the top

I'm working with some example code that draws an arc using CGPaths. I've had a look around and have found documentation but I just can't seem to visualize in my head whats going on in terms of using MoveToPoint, AddLineToPoint etc. I can't 'see' what the code is doing I can just see the result.

For example the code below draws an arc a full 360 degrees beginning from the 3 o clock position. For the life of me I can't figure out how to make it begin from the 12 o clock position with actually rotating the view - 90 degrees.

Could somebody help me figure out this code and how I would change it to achieve the beginning from 12 o'clock, preferably trying to explain how this whole paths things works. Or maybe point me to a visual resource online?

- (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);
}
like image 624
dubbeat Avatar asked Mar 03 '11 16:03

dubbeat


People also ask

How do you draw a easy circle?

To draw a circle, take a compass with a pencil attached and place the ends on a piece of paper. Then, keeping the end without the pencil stationery, rotate the compass 360 degrees so the pencil draws a perfect circle. If you don't have a compass, you can draw a circle using a piece of string instead.


1 Answers

Paths really are not that hard to understand visually. Basically all a path is is a line connecting two points on the cartesian plane that defines the iphone's screen.

When you moveToPoint it sets the current point of the path to the specified point.

When you addLineToPoint it draws a straight line from the current point to the specified point.

When you addCurveToPoint it draws a curved line from the current point to the specified point based on certain tangents and control points.

And so on. I would recommend reading apples documentation on CGPaths in order to better understand what each function is doing.

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


As far as your question goes to making this start at 12 instead of 3 simply read the documentation for the CGPathAddArc function.

What you need to do is change your 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 is doing is changing the starting angle to -90 degrees (all angles are measured in radians from the horizontal) and the ending angle to 270 degrees.


Hope this helps. Cheers,

Brenton.

like image 56
Brenton Morse Avatar answered Sep 19 '22 23:09

Brenton Morse