Paths中的几个重要元素

Paths中的几个重要元素

Paths中的几个重要元素

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

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

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


第二种
void CGContextAddArcToPoint (
                             CGContextRef c,
                             CGFloat x1, 
// 端点 1 x 坐标
                             CGFloat y1, 
// 端点 1 y 坐标
                             CGFloat x2, 
// 端点 2 x 坐标
                             CGFloat y2, 
// 端点 2 y 坐标
                             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
画曲线,一般是一条直线,然后定义几个控制点,使直线变弯曲。
三次曲线函数
void CGContextAddCurveToPoint (
                               CGContextRef c,
                               CGFloat cp1x,
// 控制点 1 x 坐标
                               CGFloat cp1y,
// 控制点 1 y 坐标
                               CGFloat cp2x,
// 控制点 2 x 坐标
                               CGFloat cp2y,
// 控制点 2 y 坐标
                               CGFloat x, 
// 直线的终点 x 坐标
                               CGFloat y 
// 直线的终点 y 坐标
);
假如第二个控制点( cp2x cp2y )比( cp1x,cp1y 更接近 current point ,那么会形成一个封闭的曲线

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

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


Creating a Path
调用函数 CGContextBeginPath 开始创建路径,线调用函数 CGContextMoveToPoint 设置起点
然后开始画自己想画的路径,注意一下几点 :
1.L ines, 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
void CGContextAddPath (
                       CGContextRef context,
                       CGPathRef path
                       );

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

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


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


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

Stroke color space
void CGContextSetStrokeColorSpace (
                                   CGContextRef c,
                                   CGColorSpaceRef colorspace
                                   );
Stroke color
void CGContextSetStrokeColor (
                             
CGContextRef c,
                             
const CGFloat components[]
                              );
void CGContextSetStrokeColorWithColor (
                                      
CGContextRef c,
                                      
CGColorRef color
                                       );
Stroke pattern
(和透明度相关)
void CGContextSetStrokePattern (
                                CGContextRef c,
                                CGPatternRef pattern,
                               
const CGFloat components[]
                                );


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

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

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

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

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

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

Filling a Path
填充一个路径的时候,路径里面的子路径都是独立填充的。
假如是重叠的路径,决定一个点是否被填充,有两种规则
1 ,nonzero winding number rule: 非零绕数规则,假如一个点被从左到右跨过,计数器 + 1 ,从右到左跨过,计数器 - 1 ,最后,如果结果是 0 ,那么不填充,如果是非零,那么填充。
2 ,even-odd rule: 奇偶规则,假如一个点被跨过,那么 + 1 ,最后是奇数,那么要被填充,偶数则不填充,和方向没有关系。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值