iPhone绘图关于QuartZ中绘制Polygons案例是本文要介绍的内容,主要介绍了如何在QuartZ中绘制Polygons的内容,内容不多,主要是基于代码实现,一起来看这个有趣的案例。
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));
结果如下图:
2.绘制多边形(Polygon)
- // 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);
我们会根据drawingMode的五个常量讨论
- kCGPathFill, kCGPathEOFill, kCGPathStroke, kCGPathFillStroke, or kCGPathEOFillStroke.
(1)kCGPathFill如下图:
此fill 模式为缺省模式(非零缠绕数原则),大概规则为,在需要填充颜色的区域的一点向画区域外画一条线,g如果是从左向右穿过的,则加1,如果从右向左穿过,则减一,最后结果为0则不fill,大于0则填充,所以line的方向对fill的区域有影响。
还有一种为even-odd(奇偶原则),只计算line穿过path段的个数,为偶数时,不填充,奇数时填充,所以path的方向不会影响填充的结果。
(2) kCGPathEOFill模式
此填充模式为奇偶模式
(3)kCGPathStroke模式
(4)kCGPathFillStroke模式
(5)kCGPathEOFillStroke模式
小结:iPhone绘图关于QuartZ中绘制Polygons案例的内容介绍完了,希望本文对你有所帮助!如果想深入了解iphone绘图的更多内容,请参考: