美文网首页
绘制多边形并填充

绘制多边形并填充

作者: 00d1ed2b53ae | 来源:发表于2018-11-02 16:34 被阅读6次

    1.绘制矩形的一般方法
    // Drawing with a white stroke color
    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
    // And 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);

    // Add Rect to the current path, then stroke it
    CGContextAddRect(context, CGRectMake(30.0, 30.0, 60.0, 60.0));
    CGContextStrokePath(context);

    // Stroke Rect convenience that is equivalent to above
    CGContextStrokeRect(context, CGRectMake(30.0, 120.0, 60.0, 60.0));

    // Stroke rect convenience equivalent to the above, plus a call to CGContextSetLineWidth().
    CGContextStrokeRectWithWidth(context, CGRectMake(30.0, 210.0, 60.0, 60.0), 10.0);
    // Demonstate the stroke is on both sides of the path.
    CGContextSaveGState(context);
    //red
    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
    CGContextStrokeRectWithWidth(context, CGRectMake(30.0, 210.0, 60.0, 60.0), 2.0);
    CGContextRestoreGState(context);

    CGRect rects[] =
    {
    CGRectMake(120.0, 30.0, 60.0, 60.0),
    CGRectMake(120.0, 120.0, 60.0, 60.0),
    CGRectMake(120.0, 210.0, 60.0, 60.0),
    };
    // Bulk call to add rects to the current path.
    CGContextAddRects(context, rects, sizeof(rects)/sizeof(rects[0]));
    CGContextStrokePath(context);

    // Create filled rectangles via two different paths.
    // Add/Fill path
    CGContextAddRect(context, CGRectMake(210.0, 30.0, 60.0, 60.0));
    CGContextFillPath(context);
    // Fill convienience.
    CGContextFillRect(context, CGRectMake(210.0, 120.0, 60.0, 60.0));

    注释:
    CGContextAddRect(context, CGRectMake(30.0, 30.0, 60.0, 60.0));
    CGContextStrokePath(context);
    此两句绘制的是左上角的矩形,当CGContextStrokePath调用之后,current path会被清空。

    CGContextStrokeRect(context, CGRectMake(30.0, 120.0, 60.0, 60.0));
    上面的一条语句等价于上面的两条。

    语句CGContextStrokeRectWithWidth(context, CGRectMake(30.0, 210.0, 60.0, 60.0), 10.0)等价与上面的语句在加上CGContextSetLineWidth(10.0)

    下面的三条语句通过两种方法来fill矩形区域。
    CGContextAddRect(context, CGRectMake(210.0, 30.0, 60.0, 60.0));
    CGContextFillPath(context);
    // Fill convienience.
    CGContextFillRect(context, CGRectMake(210.0, 120.0, 60.0, 60.0));

    结果如下图:

    QuartZ例子(2)---绘制Polygons

    2.绘制多边形(Polygon)

    // Drawing with a white stroke color

    CGContextSetRGBStrokeCol<wbr>or(context, 1.0, 1.0, 1.0, 1.0);

    // Drawing with a blue fill color

    CGContextSetRGBFillColor<wbr>(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);

    我们会根据drawingMode的五个常量讨论

    [kCGPathFill](http://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGPath/Reference/reference.html#//apple_ref/c/econst/kCGPathFill), <wbr>[kCGPathEOFill](http://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGPath/Reference/reference.html#//apple_ref/c/econst/kCGPathEOFill), <wbr>[kCGPathStroke](http://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGPath/Reference/reference.html#//apple_ref/c/econst/kCGPathStroke), <wbr>[kCGPathFillStroke](http://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGPath/Reference/reference.html#//apple_ref/c/econst/kCGPathFillStroke), or <wbr>[kCGPathEOFillStroke](http://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGPath/Reference/reference.html#//apple_ref/c/econst/kCGPathEOFillStroke).

    (1)kCGPathFill如下图:

    QuartZ例子(2)---绘制Polygons
    此fill 模式为缺省模式(非零缠绕数原则),大概规则为,在需要填充颜色的区域的一点向画区域外画一条线,g如果是从左向右穿过的,则加1,如果从右向左穿过,则减一,最后结果为0则不fill,大于0则填充,所以line的方向对fill的区域有影响。

    [图片上传中...(image-335809-1541147667811-5)]

    还有一种为even-odd(奇偶原则),只计算line穿过path段的个数,为偶数时,不填充,奇数时填充,所以path的方向不会影响填充的结果。

    (2) kCGPathEOFill模式

    QuartZ例子(2)---绘制Polygons

    此填充模式为奇偶模式

    (3)kCGPathStroke模式

    QuartZ例子(2)---绘制Polygons

    (4)kCGPathFillStroke模式

    QuartZ例子(2)---绘制Polygons

    (5)kCGPathEOFillStroke模式

    QuartZ例子(2)---绘制Polygons

    相关文章

      网友评论

          本文标题:绘制多边形并填充

          本文链接:https://www.haomeiwen.com/subject/wxycxqtx.html