Ios DrawRect:如何在给定一组有序点的情况下填充多边形?

Ios DrawRect:如何在给定一组有序点的情况下填充多边形?,ios,objective-c,cocoa-touch,drawrect,Ios,Objective C,Cocoa Touch,Drawrect,目前,在我的drawRect:方法中,我正在我的点集中的每个点之间画一条线,每个点表示为CGPoint;但是,现在我想填充这些设定点区域内的区域。我不知道如何使用quartz api实现这一点,有办法吗 现在,积分是有序的。因此,可以识别哪个点代表多边形的第一个点,依此类推。将点添加到UIBezierPath,然后使用其填充方法。将点添加到UIBezierPath,然后使用其填充方法。这显示了您需要执行的操作: -(void)drawInContext:(CGContextRef)context

目前,在我的
drawRect:
方法中,我正在我的点集中的每个点之间画一条线,每个点表示为
CGPoint
;但是,现在我想填充这些设定点区域内的区域。我不知道如何使用quartz api实现这一点,有办法吗


现在,积分是有序的。因此,可以识别哪个点代表多边形的第一个点,依此类推。

将点添加到UIBezierPath,然后使用其填充方法。

将点添加到UIBezierPath,然后使用其填充方法。

这显示了您需要执行的操作:

-(void)drawInContext:(CGContextRef)context
{
    // Drawing with a white stroke color
    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
    // Drawing with a blue fill color
    CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
    // Draw them with a 2.0 stroke width so they are a bit more visible.
    CGContextSetLineWidth(context, 2.0);

    CGPoint center;

    // Add a star to the current path
    center = CGPointMake(90.0, 90.0);
    CGContextMoveToPoint(context, center.x, center.y + 60.0);
    for(int i = 1; i < 5; ++i)
    {
        CGFloat x = 60.0 * sinf(i * 4.0 * M_PI / 5.0);
        CGFloat y = 60.0 * cosf(i * 4.0 * M_PI / 5.0);
        CGContextAddLineToPoint(context, center.x + x, center.y + y);
    }
    // And close the subpath.
    CGContextClosePath(context);

    // Now add the hexagon to the current path
    center = CGPointMake(210.0, 90.0);
    CGContextMoveToPoint(context, center.x, center.y + 60.0);
    for(int i = 1; i < 6; ++i)
    {
        CGFloat x = 60.0 * sinf(i * 2.0 * M_PI / 6.0);
        CGFloat y = 60.0 * cosf(i * 2.0 * M_PI / 6.0);
        CGContextAddLineToPoint(context, center.x + x, center.y + y);
    }
    // And close the subpath.
    CGContextClosePath(context);

    // Now draw the star & hexagon with the current drawing mode.
    CGContextDrawPath(context, drawingMode);
}
-(void)drawInContext:(CGContextRef)上下文
{
//白色笔画
CGContextSetRGBStrokeColor(上下文,1.0,1.0,1.0,1.0);
//使用蓝色填充颜色绘制
CGContextSetRGBFillColor(上下文,0.0,0.0,1.0,1.0);
//以2.0笔划宽度绘制它们,使它们更为可见。
CGContextSetLineWidth(上下文,2.0);
点中心;
//将星形添加到当前路径
中心=CGPointMake(90.0,90.0);
CGContextMoveToPoint(上下文,center.x,center.y+60.0);
对于(int i=1;i<5;++i)
{
CGFloat x=60.0*sinf(i*4.0*M_PI/5.0);
CGFloat y=60.0*cosf(i*4.0*M_PI/5.0);
CGContextAddLineToPoint(上下文,center.x+x,center.y+y);
}
//然后关闭子路径。
CGContextClosePath(上下文);
//现在将六边形添加到当前路径
中心=CGPointMake(210.0,90.0);
CGContextMoveToPoint(上下文,center.x,center.y+60.0);
对于(int i=1;i<6;++i)
{
CGFloat x=60.0*sinf(i*2.0*M_PI/6.0);
CGFloat y=60.0*cosf(i*2.0*M_PI/6.0);
CGContextAddLineToPoint(上下文,center.x+x,center.y+y);
}
//然后关闭子路径。
CGContextClosePath(上下文);
//现在使用当前绘图模式绘制星形和六边形。
CGContextDrawPath(上下文,drawingMode);
}
注意,答案中提到了这一点。

这显示了您需要做什么:

-(void)drawInContext:(CGContextRef)context
{
    // Drawing with a white stroke color
    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
    // Drawing with a blue fill color
    CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
    // Draw them with a 2.0 stroke width so they are a bit more visible.
    CGContextSetLineWidth(context, 2.0);

    CGPoint center;

    // Add a star to the current path
    center = CGPointMake(90.0, 90.0);
    CGContextMoveToPoint(context, center.x, center.y + 60.0);
    for(int i = 1; i < 5; ++i)
    {
        CGFloat x = 60.0 * sinf(i * 4.0 * M_PI / 5.0);
        CGFloat y = 60.0 * cosf(i * 4.0 * M_PI / 5.0);
        CGContextAddLineToPoint(context, center.x + x, center.y + y);
    }
    // And close the subpath.
    CGContextClosePath(context);

    // Now add the hexagon to the current path
    center = CGPointMake(210.0, 90.0);
    CGContextMoveToPoint(context, center.x, center.y + 60.0);
    for(int i = 1; i < 6; ++i)
    {
        CGFloat x = 60.0 * sinf(i * 2.0 * M_PI / 6.0);
        CGFloat y = 60.0 * cosf(i * 2.0 * M_PI / 6.0);
        CGContextAddLineToPoint(context, center.x + x, center.y + y);
    }
    // And close the subpath.
    CGContextClosePath(context);

    // Now draw the star & hexagon with the current drawing mode.
    CGContextDrawPath(context, drawingMode);
}
-(void)drawInContext:(CGContextRef)上下文
{
//白色笔画
CGContextSetRGBStrokeColor(上下文,1.0,1.0,1.0,1.0);
//使用蓝色填充颜色绘制
CGContextSetRGBFillColor(上下文,0.0,0.0,1.0,1.0);
//以2.0笔划宽度绘制它们,使它们更为可见。
CGContextSetLineWidth(上下文,2.0);
点中心;
//将星形添加到当前路径
中心=CGPointMake(90.0,90.0);
CGContextMoveToPoint(上下文,center.x,center.y+60.0);
对于(int i=1;i<5;++i)
{
CGFloat x=60.0*sinf(i*4.0*M_PI/5.0);
CGFloat y=60.0*cosf(i*4.0*M_PI/5.0);
CGContextAddLineToPoint(上下文,center.x+x,center.y+y);
}
//然后关闭子路径。
CGContextClosePath(上下文);
//现在将六边形添加到当前路径
中心=CGPointMake(210.0,90.0);
CGContextMoveToPoint(上下文,center.x,center.y+60.0);
对于(int i=1;i<6;++i)
{
CGFloat x=60.0*sinf(i*2.0*M_PI/6.0);
CGFloat y=60.0*cosf(i*2.0*M_PI/6.0);
CGContextAddLineToPoint(上下文,center.x+x,center.y+y);
}
//然后关闭子路径。
CGContextClosePath(上下文);
//现在使用当前绘图模式绘制星形和六边形。
CGContextDrawPath(上下文,drawingMode);
}
注意,答案中提到了这一点