Search code examples
iosobjective-cuibezierpathcashapelayercgpoint

How to get center point of radius in UIBezierPath arc?


I have and objective c application. In a ViewController I have a UIView, and into this view I draw a circle with UIBezierPath with this code:

CAShapeLayer* clockWiseLayer = [[CAShapeLayer alloc] init];
        clockWiseLayer.path = [UIBezierPath bezierPathWithArcCenter:centerPoint
                                                              radius:radius
                                                          startAngle:startAngle
                                                            endAngle:endAngle
                                                           clockwise:YES].CGPath;

Then I want know what is the center of point into portion or the center of radius to the center of a part's arc like this:

enter image description here

How can I get this point? If I know the end, start of angle, the radius size what is the value of the point and how can I calculate this?


Solution

  • All of this is just some basic trigonometry.

    First get the angle of the center of the arc:

    normalEnd = endAngle < startAngle ? endAngle + 2 * pi : endAngle
    centerAngle = startAngle + (normalEnd - startAngle) / 2
    

    Then calculate the x and y coordinate of the center of the arc relative to the center point:

    arcCenterX = centerPoint.x + cos(centerAngle) * radius
    arcCenterY = centerPoint.y + sin(centerAngle) * radius
    

    Now you have the end points of a line segment and you want the center of that line segment:

    desiredPointX = (centerPoint.x + arcCenterX) / 2
    desiredPointY = (centerPoint.y + arcCenterY) / 2