Wednesday, June 5, 2013

UIBezierPath bezierPathWithArcCenter example in Objective C (iOS).


UIBezierPath bezierPathWithArcCenter

Creates and returns a new UIBezierPath object initialized with an arc of a circle.

+ (UIBezierPath *)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise

Parameters of [UIBezierPath bezierPathWithArcCenter]
center
Specifies the center point of the circle (in the current coordinate system) used to define the arc.
radius
Specifies the radius of the circle used to define the arc.
startAngle
Specifies the starting angle of the arc (measured in radians).
endAngle
Specifies the end angle of the arc (measured in radians).
clockwise
The direction in which to draw the arc.

Return Value of [UIBezierPath bezierPathWithArcCenter]
A new path object with the specified arc.

Discussion of [UIBezierPath bezierPathWithArcCenter]
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.

Figure 1 Angles in the default coordinate system

After calling this method, the current point is set to the point on the arc at the end angle of the circle.
UIBezierPath bezierPathWithArcCenter example.
-(void) createPaths {
    largeCircle = [UIBezierPath bezierPathWithArcCenter:self.center radius:LARGE_RADIUS startAngle:0 endAngle:RADIANS(360) clockwise:YES];
    whiteCircle = [UIBezierPath bezierPathWithArcCenter:self.center radius:SMALL_RADIUS startAngle:0 endAngle:RADIANS(360) clockwise:YES];   

    orangeWedge = [UIBezierPath bezierPath];
    [orangeWedge moveToPoint:self.center];

    double startAngle = RADIANS(45);
    double endAngle = RADIANS(135);

    [orangeWedge addLineToPoint:CGPointMake(sin(startAngle) + self.center.x, cos(startAngle) + self.center.y)];
    [orangeWedge addArcWithCenter:self.center radius:LARGE_RADIUS startAngle:startAngle endAngle:endAngle clockwise:YES];
    [orangeWedge addLineToPoint:self.center];

    startAngle = RADIANS(60);
    endAngle = RADIANS(120);

    yellowWedge = [UIBezierPath bezierPath];
    [yellowWedge moveToPoint:self.center];

    [yellowWedge addLineToPoint:CGPointMake(sin(startAngle) + self.center.x, cos(startAngle) + self.center.y)];
    [yellowWedge addArcWithCenter:self.center radius:LARGE_RADIUS startAngle:startAngle endAngle:endAngle clockwise:YES];
    [yellowWedge addLineToPoint:self.center];

    startAngle = RADIANS(75);
    endAngle = RADIANS(105);

    greenWedge = [UIBezierPath bezierPath];
    [greenWedge moveToPoint:self.center];

    [greenWedge addLineToPoint:CGPointMake(sin(startAngle) + self.center.x, cos(startAngle) + self.center.y)];
    [greenWedge addArcWithCenter:self.center radius:LARGE_RADIUS startAngle:startAngle endAngle:endAngle clockwise:YES];
    [greenWedge addLineToPoint:self.center];
}

Example of [UIBezierPath bezierPathWithArcCenter].
 to reverse your path, you'd just:

UIBezierPath* aPath = [UIBezierPath bezierPathWithArcCenter:center radius:200 startAngle:0 endAngle:180 clockwise:YES];
self.myPath = [aPath bezierPathByReversingPath];

UIBezierPath bezierPathWithArcCenter example.
upperLeft = [UIBezierPath bezierPathWithArcCenter:CGPointMake(xCorner + margin, yCorner + margin)
                                                   radius:5.5
                                               startAngle:0
                                                 endAngle:DEGREES_TO_RADIANS(360)
                                                clockwise:YES];
        [[UIColor blackColor]setFill];
        [upperLeft fill];
        [upperLeft closePath];

upperRight = [UIBezierPath bezierPathWithArcCenter:CGPointMake(xCorner+ widths - margin, yCorner + margin)
                                                radius:5.5
                                            startAngle:0
                                              endAngle:DEGREES_TO_RADIANS(360)
                                             clockwise:YES];
    [[UIColor blackColor]setFill];
    [upperRight fill];
    [upperRight closePath];

End of UIBezierPath bezierPathWithArcCenter example article.