【问题标题】:Drawing circles using CGContext使用 CGContext 绘制圆
【发布时间】:2012-03-17 07:50:11
【问题描述】:

使用下面的代码,我正在构建具有不同边数的多边形。

有人可以建议我如何添加代码来外接圆并在返回的每个多边形中内接一个圆吗?

-(void) drawRect:(CGRect)rect{
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextBeginPath (context); 
    CGContextSetLineWidth(context,5);

    NSArray * points = [PolygonUIView pointsForPolygonInRect:[self bounds] numberOfSides:polygon.numberOfSides];

    NSLog(@"%d", [points count]);
    NSLog(@"%d", polygon.numberOfSides);

    for(NSValue * point in points) {
        CGPoint val = [point CGPointValue];
        if([points indexOfObject:point]==0)
        {
            CGContextMoveToPoint (context, val.x, val.y);

        }
        else
        {
            CGContextAddLineToPoint (context, val.x, val.y); 
        }
    }

    CGContextClosePath(context);
    [[UIColor clearColor] setFill]; 
    [[UIColor blackColor] setStroke]; 
    CGContextDrawPath (context, kCGPathFillStroke);
    polygonLabel.text = polygon.name;
}

+ (NSArray *)pointsForPolygonInRect:(CGRect)rect numberOfSides:(int)numberOfSides { 
    CGPoint center = CGPointMake(rect.size.width / 2.0, rect.size.height / 2.0); 
    float radius = 0.90 * center.x; 
    NSLog(@"%f rad",radius);
    NSMutableArray *result = [NSMutableArray array]; 
    float angle = (2.0 * M_PI) / numberOfSides; 
    float exteriorAngle = M_PI - angle; 
    float rotationDelta = angle - (0.5 * exteriorAngle); 

    for (int currentAngle = 0; currentAngle < numberOfSides; currentAngle++) { 
        float newAngle = (angle * currentAngle) - rotationDelta; 
        float curX = cos(newAngle) * radius; 
        float curY = sin(newAngle) * radius; 
        [result addObject:[NSValue valueWithCGPoint:CGPointMake(center.x + curX, 
                                                                center.y + curY)]]; 
    } 

    return result;
}

【问题讨论】:

    标签: objective-c core-graphics


    【解决方案1】:

    多亏了别人的指点,终于搞定了 下面添加到原始代码的代码给了我我需要的结果 谢谢

    // circumscribe the polygons
    CGContextSetLineWidth(context, 2); // set the line width
    CGContextSetRGBStrokeColor(context, 20.0 /255, 101.0 / 255.0, 18.0 / 255.0, 1.0);
    
    CGPoint center = CGPointMake(rect.size.width / 2, rect.size.height / 2); // get the circle centre
    CGFloat radius = 0.9 * center.x; // little scaling needed
    CGFloat startAngle = -((float)M_PI / 2); // 90 degrees
    CGFloat endAngle = ((2 * (float)M_PI) + startAngle);
    CGContextAddArc(context, center.x, center.y, radius + 4, startAngle, endAngle, 0); // create an arc the +4 just adds some pixels because of the polygon line thickness
    CGContextStrokePath(context); // draw
    
    // inscribed circle
    float angle = M_PI - ((2 * M_PI) / polygon.numberOfSides); // need the polygon angles
    CGContextSetLineWidth(context, 2); // set the line width
    CGContextSetRGBStrokeColor(context, 20.0 / 255, 101.0 / 255.0, 211.0 / 255.0, 1.0);
    CGFloat innerradius = (0.9 * center.x) * sin(angle / 2); // calc the inner radius
    CGContextAddArc(context, center.x, center.y, innerradius - 3, startAngle, endAngle, 0); // create an arc the minus 3 subtracts some pixels because of the polygon line thickness
    CGContextStrokePath(context); // draw
    

    【讨论】:

      【解决方案2】:

      您使用CGContextAddEllipseInRect() 绘制圆圈,并传入一个正方形CGRect。计算矩形以适应您返回的每个多边形 inside 会更复杂一些,具体取决于您希望它适应的紧密程度。

      【讨论】:

      • 您好,感谢您的回答,但需要更多关于如何计算点的说明,以便正确地限制和内切返回的每个多边形
      • 任何可能的多边形的通用答案?恐怕我不知道,您可能需要在数学堆栈交换中询问。您可能会使用 CGPathGetBoundingBox 找到某个地方,它给出了一个包围您的路径的矩形,然后计算出它的对角线来给出您的半径?
      • 嗨,谢谢你的回答——你给了我一个想法的种子,所以我会摆弄一下,看看我能不能让它发挥作用
      • 我很想看看你的想法,这是一个很好的问题(如果你喜欢那种东西!)
      • 有什么方法可以为 CGContextAddEllipseInRect 提供描边,它应该是圆的轮廓线。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      相关资源
      最近更新 更多