0
votes

I have a UIBezierPath drawn by the user on the touchscreen. Now I want to draw a circle around this path/drawing, and the path has to be in the middle of this circle. I tried to use:

UIBezierPath.bounds

But obviously this does not work :( So my next idea was to go through every point in the UIBezierPath and get the "lowest" point and the most "left" point. After that draw the circle based on those points.

My question is: Is there are more elegant and efficient way of drawing a circle around a custom UIBezierPath?

1
Show your code using bounds and describe what it does wrong.Wain
Ohh thats simple, when the bounds width is = 10 and the height = 1, the circle is flat, but not round. That happens for different shapes.jjuser19jj
So the issue is just that you need to always get a circle from the bounds?Wain

1 Answers

0
votes

Did it this way:

NSMutableArray *bezierPoints = [NSMutableArray array];
CGPathApply(drawPath.CGPath, (__bridge void *)(bezierPoints), MyCGPathApplierFunc);

float minX = 100000.f;
float minY = 100000.f;

for(NSValue* value in bezierPoints)
{
    CGPoint point = value.CGPointValue;

    if (point.x<minX)
        minX = point.x;
    if (point.y<minY)
        minY = point.y;
}
float midX = minX + (drawPath.bounds.size.width/2);
float midY = minY + (drawPath.bounds.size.height/2);

float radius = 0.f;
if (drawPath.bounds.size.width>drawPath.bounds.size.height)
{
    radius = drawPath.bounds.size.width * 0.65;

}
else
{
    radius = drawPath.bounds.size.height * 0.65;
}

CGMutablePathRef myPath = CGPathCreateMutable();
CGPathAddArc(myPath, NULL, midX,midY,radius , 0, M_PI*2, YES);



SKShapeNode* node = [[SKShapeNode alloc]init];
node.path = myPath;
node.fillColor = nil;
node.strokeColor = SKColor.redColor;
[self addChild:node];

void MyCGPathApplierFunc (void *info, const CGPathElement *element) {
NSMutableArray *bezierPoints = (__bridge NSMutableArray *)info;

CGPoint *points = element->points;
CGPathElementType type = element->type;

switch(type) {
    case kCGPathElementMoveToPoint: // contains 1 point
        [bezierPoints addObject:[NSValue valueWithCGPoint:points[0]]];
        break;

    case kCGPathElementAddLineToPoint: // contains 1 point
        [bezierPoints addObject:[NSValue valueWithCGPoint:points[0]]];
        break;

    case kCGPathElementAddQuadCurveToPoint: // contains 2 points
        [bezierPoints addObject:[NSValue valueWithCGPoint:points[0]]];
        [bezierPoints addObject:[NSValue valueWithCGPoint:points[1]]];
        break;

    case kCGPathElementAddCurveToPoint: // contains 3 points
        [bezierPoints addObject:[NSValue valueWithCGPoint:points[0]]];
        [bezierPoints addObject:[NSValue valueWithCGPoint:points[1]]];
        [bezierPoints addObject:[NSValue valueWithCGPoint:points[2]]];
        break;

    case kCGPathElementCloseSubpath: // contains no point
        break;
}
 }