【iOS】iPhone绘图关于QuartZ中绘制Polygons

来源:互联网 发布:二叉树层次遍历java 编辑:程序博客网 时间:2024/04/28 07:42

iPhone绘图关于QuartZ绘制Polygons案例是本文要介绍的内容,主要介绍了如何在QuartZ绘制Polygons的内容,内容不多,主要是基于代码实现,一起来看这个有趣的案例。

1.绘制矩形的一般方法

  1. //Drawingwithawhitestrokecolor
  2. CGContextSetRGBStrokeColor(context,1.0,1.0,1.0,1.0);
  3. //Anddrawingwithabluefillcolor
  4. CGContextSetRGBFillColor(context,0.0,0.0,1.0,1.0);
  5. //Drawthemwitha2.0strokewidthsotheyareabitmorevisible.
  6. CGContextSetLineWidth(context,2.0);
  7. //AddRecttothecurrentpath,thenstrokeit
  8. CGContextAddRect(context,CGRectMake(30.0,30.0,60.0,60.0));
  9. CGContextStrokePath(context);
  10. //StrokeRectconveniencethatisequivalenttoabove
  11. CGContextStrokeRect(context,CGRectMake(30.0,120.0,60.0,60.0));
  12. //Strokerectconvenienceequivalenttotheabove,plusacalltoCGContextSetLineWidth().
  13. CGContextStrokeRectWithWidth(context,CGRectMake(30.0,210.0,60.0,60.0),10.0);
  14. //Demonstatethestrokeisonbothsidesofthepath.
  15. CGContextSaveGState(context);
  16. //red
  17. CGContextSetRGBStrokeColor(context,1.0,0.0,0.0,1.0);
  18. CGContextStrokeRectWithWidth(context,CGRectMake(30.0,210.0,60.0,60.0),2.0);
  19. CGContextRestoreGState(context);
  20. CGRectrects[]=
  21. {
  22. CGRectMake(120.0,30.0,60.0,60.0),
  23. CGRectMake(120.0,120.0,60.0,60.0),
  24. CGRectMake(120.0,210.0,60.0,60.0),
  25. };
  26. //Bulkcalltoaddrectstothecurrentpath.
  27. CGContextAddRects(context,rects,sizeof(rects)/sizeof(rects[0]));
  28. CGContextStrokePath(context);
  29. //Createfilledrectanglesviatwodifferentpaths.
  30. //Add/Fillpath
  31. CGContextAddRect(context,CGRectMake(210.0,30.0,60.0,60.0));
  32. CGContextFillPath(context);
  33. //Fillconvienience.
  34. CGContextFillRect(context,CGRectMake(210.0,120.0,60.0,60.0));

注释:

  1. CGContextAddRect(context,CGRectMake(30.0,30.0,60.0,60.0));
  2. CGContextStrokePath(context);

此两句绘制的是左上角的矩形,当CGContextStrokePath调用之后,current path会被清空。

  1. CGContextStrokeRect(context,CGRectMake(30.0,120.0,60.0,60.0));

上面的一条语句等价于上面的两条。

语句

  1. CGContextStrokeRectWithWidth(context,CGRectMake(30.0,210.0,60.0,60.0),10.0)

等价与上面的语句在加上CGContextSetLineWidth(10.0)

下面的三条语句通过两种方法来fill矩形区域。

  1. CGContextAddRect(context,CGRectMake(210.0,30.0,60.0,60.0));
  2. CGContextFillPath(context);
  3. //Fillconvienience.
  4. CGContextFillRect(context,CGRectMake(210.0,120.0,60.0,60.0));

结果如下图:

2.绘制多边形(Polygon)

  1. //Drawingwithawhitestrokecolor
  2. CGContextSetRGBStrokeColor(context,1.0,1.0,1.0,1.0);
  3. //Drawingwithabluefillcolor
  4. CGContextSetRGBFillColor(context,0.0,0.0,1.0,1.0);
  5. //Drawthemwitha2.0strokewidthsotheyareabitmorevisible.
  6. CGContextSetLineWidth(context,2.0);
  7. CGPointcenter;
  8. //Addastartothecurrentpath
  9. center=CGPointMake(90.0,90.0);
  10. CGContextMoveToPoint(context,center.x,center.y+60.0);
  11. for(inti=1;i<5;++i)
  12. {
  13. CGFloatx=60.0*sinf(i*4.0*M_PI/5.0);
  14. CGFloaty=60.0*cosf(i*4.0*M_PI/5.0);
  15. CGContextAddLineToPoint(context,center.x+x,center.y+y);
  16. }
  17. //Andclosethesubpath.
  18. CGContextClosePath(context);
  19. //Nowaddthehexagontothecurrentpath
  20. center=CGPointMake(210.0,90.0);
  21. CGContextMoveToPoint(context,center.x,center.y+60.0);
  22. for(inti=1;i<6;++i)
  23. {
  24. CGFloatx=60.0*sinf(i*2.0*M_PI/6.0);
  25. CGFloaty=60.0*cosf(i*2.0*M_PI/6.0);
  26. CGContextAddLineToPoint(context,center.x+x,center.y+y);
  27. }
  28. //Andclosethesubpath.
  29. CGContextClosePath(context);
  30. //Nowdrawthestar&hexagonwiththecurrentdrawingmode.
  31. CGContextDrawPath(context,drawingMode);

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

  1. kCGPathFill,kCGPathEOFill,kCGPathStroke,kCGPathFillStroke,orkCGPathEOFillStroke.

(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案例的内容介绍完了,希望本文对你有所帮助!


转载自点击打开链接

0 0
原创粉丝点击