Paths中的几个重要元素

来源:互联网 发布:软件的特点 编辑:程序博客网 时间:2024/05/21 17:51

Paths中的几个重要元素

Paths中的几个重要元素

Points
voidCGContextMoveToPoint (
                          
CGContextRef c,
                          
CGFloat x,
                          
CGFloat y
                           );
指定一个点成为current point
Quartz
会跟踪current point一般执行完一个相关函数后,current point都会相应的改变.
Lines
相关的几个函数
voidCGContextAddLineToPoint (
                             
CGContextRef c,
                             
CGFloat x,
                             
CGFloat y
                              );
创建一条直线,从current point(x,y)
然后current point会变成(x,y)

voidCGContextAddLines (
                        CGContextRef c,
                       
const CGPoint points[],
                        size_t count
                        );
创建多条直线,比如points有两个点,那么会画两条直线current point(x1,y1),
然后是(x1,y1)(x2,y2)
然后current point会变成points中的最后一个点

Arcs
两种方法创建弧度 第一种
voidCGContextAddArc (
                      CGContextRef c,
                      CGFloat x,            
//圆心的x坐标
                      CGFloat y,   
//圆心的x坐标
                      CGFloat radius,  
//圆的半径
                      CGFloat startAngle,   
//开始弧度
                      CGFloat endAngle,  
//结束弧度
                     
int clockwise         //0表示顺时针,1表示逆时针
);
假如想创建一个完整的圆圈,那么开始弧度就是0结束弧度是 2pi因为圆周长是 2*pi*r.
最后,函数执行完后,current point就被重置为(x,y).
还有一点要注意的是,假如当前path已经存在一个subpath,那么这个函数执行的另外一个效果是
会有一条直线,从current point到弧的起点


第二种
voidCGContextAddArcToPoint (
                             CGContextRef c,
                             CGFloat x1, 
//端点1x坐标
                             CGFloat y1, 
//端点1y坐标
                             CGFloat x2, 
//端点2x坐标
                             CGFloat y2, 
//端点2y坐标
                             CGFloat radius
//半径
);
原理:首先画两条线,这两条线分别是current point to (x1,y1)(x1,y1) to (x2,y2).
这样就是出现一个以(x1,y1)为顶点的两条射线,
然后定义半径长度,这个半径是垂直于两条射线的,这样就能决定一个圆了,更好的理解看下图,不过个人认为下图所标的tangent point 1的位置是错误的。
最后,函数执行完后,current point就被重置为(x2,y2).
还有一点要注意的是,假如当前path已经存在一个subpath,那么这个函数执行的另外一个效果是
会有一条直线,从current point(x1,y1)

Curves
画曲线,一般是一条直线,然后定义几个控制点,使直线变弯曲。
三次曲线函数
voidCGContextAddCurveToPoint (
                               CGContextRef c,
                               CGFloat cp1x,
//控制点1 x坐标
                               CGFloat cp1y,
//控制点1 y坐标
                               CGFloat cp2x,
//控制点2 x坐标
                               CGFloat cp2y,
//控制点2 y坐标
                               CGFloat x, 
//直线的终点x坐标
                               CGFloat y 
//直线的终点y坐标
);
假如第二个控制点(cp2xcp2y)比(cp1x,cp1y更接近current point,那么会形成一个封闭的曲线

二次曲线函数
voidCGContextAddQuadCurveToPoint (
                                   CGContextRef c,
                                   CGFloat cpx, 
//控制点x坐标
                                   CGFloat cpy, 
//控制点y坐标
                                   CGFloat x, 
//直线的终点x坐标
                                   CGFloat y 
//直线的终点y坐标
);
执行完函数貌似current point不会变化,没有具体测试过
Ellipses
voidCGContextAddEllipseInRect (
                                CGContextRef context,
                                CGRect rect 
//一矩形
);
如果矩形是一个正方形,那么画出来就是一个圆
执行完函数貌似current point不会变化,没有具体测试过
Rectangles
voidCGContextAddRect (
                       CGContextRef c,
                       CGRect rect
                       );

一次性画出多个矩形
voidCGContextAddRects (
                        CGContextRef c,
                       
const CGRect rects[],
                        size_t count
                        );
需要注意的是,画矩形有一些特别,current point没有发生变化


Creating a Path
调用函数 CGContextBeginPath 开始创建路径,线调用函数CGContextMoveToPoint设置起点
然后开始画自己想画的路径,注意一下几点:
1.Lines, arcs, and curves,是从current point开始的
2.假如想封闭一条路径,那么调用函数CGContextClosePath把当前点和起点连接起来
3.当在画arcs的时候,Quartz会画一条线从current point starting point
4.画矩形的时候不会有第三条那这样的的一条直线
5.创建完路径后,必须调用painting 函数  fill or stroke the path,不然不会画上面东东在相应的设备上】
6.开始创建一个新的路径的时候,使用函数CGContextBeginPath

重复利用路径的相关函数和数据类型
CGPathCreateMutable
类似于CGContextBeginPath
CGPathMoveToPoint
类似于CGContextMoveToPoint
CGPathAddLineToPoint
类似于 CGContextAddLineToPoint
CGPathAddCurveToPoint
类似于 CGContextAddCurveToPoint
CGPathAddEllipseInRect
类似于 CGContextAddEllipseInRect
CGPathAddArc
类似于CGContextAddArc
CGPathAddRect
类似于CGContextAddRect
CGPathCloseSubpath
类似于CGContextClosePath
CGPathRef
CGMutablePathRef

CGContextAddPath函数把一个路径添加到graphics context
voidCGContextAddPath (
                       CGContextRef context,
                       CGPathRef path
                       );

Painting a Path
Stroking
:画出路径
Filling
:填充路径的封闭区域

影响Stroking的参数
Line width
voidCGContextSetLineWidth (
                            CGContextRef c,
                            CGFloat width
                            );
Line join
:线转弯的时候的样式,比如圆滑的方式
voidCGContextSetLineJoin (
                           CGContextRef c,
                           CGLineJoin join
                           );


Line cap:线的两端的样式,比如两端变的圆滑
voidCGContextSetLineCap (
                          CGContextRef c,
                          CGLineCap cap
                          );


Miter limit:当Line join的模式是Miter join的时候,这个参数会有影响
voidCGContextSetMiterLimit (
                             CGContextRef c,
                             CGFloat limit
                             );
Line dash pattern
:虚线相关
voidCGContextSetLineDash (
                           CGContextRef c,
                           CGFloat phase,
                          
const CGFloat lengths[],
                           size_t count
                           );

Stroke color space
voidCGContextSetStrokeColorSpace (
                                   CGContextRef c,
                                   CGColorSpaceRef colorspace
                                   );
Stroke color
voidCGContextSetStrokeColor (
                             
CGContextRef c,
                             
const CGFloat components[]
                              );
voidCGContextSetStrokeColorWithColor (
                                      
CGContextRef c,
                                      
CGColorRef color
                                       );
Stroke pattern
(和透明度相关)
voidCGContextSetStrokePattern (
                                CGContextRef c,
                                CGPatternRef pattern,
                               
const CGFloat components[]
                                );


Stroking
的相关函数
Strokes
当前path.
voidCGContextStrokePath (
                          CGContextRef c
                          );

Strokes
指定的 矩形.
voidCGContextStrokeRect (
                          CGContextRef c,
                          CGRect rect
                          );

Strokes
指定的 矩形, 使用指定的宽度.
voidCGContextStrokeRectWithWidth (
                                   CGContextRef c,
                                   CGRect rect,
                                   CGFloat width
                                   );

Strokes
指定的椭圆.
voidCGContextStrokeEllipseInRect (
                                   CGContextRef context,
                                   CGRect rect
                                   );

Strokes
一些直线.
voidCGContextStrokeLineSegments (
                                  CGContextRef c,
                                 
const CGPoint points[],
                                  size_t count
                                  );

决定是Stroking还是Filling
voidCGContextDrawPath (
                       
CGContextRef c,
                       
CGPathDrawingMode mode
                        );

Filling a Path
填充一个路径的时候,路径里面的子路径都是独立填充的。
假如是重叠的路径,决定一个点是否被填充,有两种规则
1,nonzero winding number rule:非零绕数规则,假如一个点被从左到右跨过,计数器+1,从右到左跨过,计数器-1,最后,如果结果是0,那么不填充,如果是非零,那么填充。
2,even-odd rule:奇偶规则,假如一个点被跨过,那么+1,最后是奇数,那么要被填充,偶数则不填充,和方向没有关系。
0 0
原创粉丝点击