# Quartz 2D编程指南之三：路径(Path)

Figure 3-1 Quartz supports path-based drawing

Figure 3-2 A path that contains two shapes, or subpaths

Figure 3-3 A clipping area constrains drawing

Figure 3-4 Multiple paths; each path contains a randomly generated circle

Figure 3-5 Defining an arc with two tangent lines and a radius

Figure 3-6 Multiple paths; each path contains a randomly generated curve

Figure 3-7 A cubic Bézier curve uses two control points

Figure 3-8 A quadratic Bézier curve uses one control point

Quartz的一些函数将路径的子路径看成是闭合的。这些函数显示地添加一条直线来闭合 子路径，如同调用了CGContextClosePath函数。

Figure 3-9 Multiple paths; each path contains a randomly generated ellipse

Figure 3-10 Multiple paths; each path contains a randomly generated rectangle

CGPathCreateMutable，取代CGContextBeginPath

CGPathMoveToPoint，取代CGContextMoveToPoint

CGPathCloseSubpath，取代CGContexClosePath

Table 3-1 Parameters that affect how Quartz strokes the current path

linewidth是线的总宽度，单位是用户空间单元。

linejoin属性指定如何绘制线段间的联接点。Quartz支持表3-2中描述的联接样式。

Table 3-2 Line join styles

linecap指定如何绘制直线的端点。Quartz支持表3-3所示的线帽类型。默认的是butt cap。

Table 3-3 Line cap styles

Linedash pattern(虚线模式)允许我们沿着描边绘制虚线。我们通过在CGContextSetLineDash结构体中指定虚线数组和虚线相位来控制虚线的大小及位置。

CGContextSetLineDash结构如下：

voidCGContextSetLineDash(

CGContextRefctx,

floatphase,

constfloatlengths[],

size_t count,

);

Figure 3-11 Examples of line dash patterns

Quartz提供了表3-4中的函数来描边当前路径。其中一些是描边矩形及椭圆的便捷函数。

Table 3-4 Functions that stroke paths

CGContextBeginPath(context);

for(k =0; k < count; k +=2)

{

}

CGContextStrokePath(context);

Figure 3-12 Concentric circles filled using different fill rules

Quartz提供了表3-5中的函数来填充当前路径。其中一些是填充矩形及椭圆的便捷函数。

Table 3-5 Functions that fill paths

result = (alpha * foreground) + (1 - alpha) *background

“颜色与颜色空间”章节里面详细讨论了颜色值的alpha组件，该组件用于指定颜色的透明度。在本章的例子中，我们可以假设颜色值是完全不透明的(alpha = 0)。对于不透明的颜色值，当我们用普通混合模式时，所有绘制于背景之上的绘图都会遮掩住背景。

Figure 3-13 The rectangles painted in the foreground

Figure 3-14 The rectangles painted in the background

Figure 3-15 Rectangles painted using normal blend mode

Figure 3-16 Rectangles painted using multiply blend mode

Figure 3-17 Rectangles painted using screen blend mode

Figure 3-18 Rectangles painted using overlay blend mode

Figure 3-19 Rectangles painted using darken blend mode

Figure 3-20 Rectangles painted using lighten blend mode

Figure 3-21 Rectangles painted using color dodge blend mode

Figure 3-22 Rectangles painted using color burn blend mode

Figure 3-23 Rectangles painted using soft light blend mode

Figure 3-24 Rectangles painted using hard light blend mode

Figure 3-25 Rectangles painted using difference blend mode

Figure 3-26 Rectangles painted using exclusion blend mode

Figure 3-27 Rectangles painted using hue blend mode

Figure 3-28 Rectangles painted using saturation blend mode

Figure 3-29 Rectangles painted using color blend mode

Figure 3-30 Rectangles painted using luminosity blend mode

Listing 3-1 Setting up a circular clip area

CGContextBeginPath(context);

CGContextAddArc(context, w/2, h/2, ((w>h) ? h : w)/2,0,2*PI,0);

CGContextClosePath(context);

CGContextClip(context);

Table 3-6 Functions that clip the graphics context