The UIBezierPath class lets you define a path consisting of straight and curved line segments and render that path in your custom views. You use this class initially to specify just the geometry for your path. Paths can define simple shapes such as rectangles, ovals, and arcs or they can define complex polygons that incorporate a mixture of straight and curved line segments. After defining the shape, you can use additional methods of this class to render the path in the current drawing context.UIBezierPath類允許您定義一個路徑組成的直線和曲線段和渲染路徑在您的自定義視圖。你使用這個類最初指定的幾何路徑。路徑可以定義簡單的形狀如矩形、橢圓、弧或他們可以定義復雜的多邊形,將直線和曲線段的混合物。定義形狀之后,您可以使用這個類的其他方法來呈現在當前繪圖上下文路徑
- 使用貝賽爾曲線創建簡單圖形
- (void)drawRect:(CGRect)rect{
//創建UIBezierPath
UIBezierPath *path = [UIBezierPath bezierPath];
//繪制三角形
[path moveToPoint:CGPointMake(100, 10)];
[path addLineToPoint:CGPointMake(130, 40)];
[path addLineToPoint:CGPointMake(70, 40)];
[[UIColor redColor] setFill];
[path fill];//填充模式
[path closePath];
//繪制剪切區域
[path moveToPoint:CGPointMake(100, 80)];
[path addLineToPoint:CGPointMake(105, 100)];
[path addLineToPoint:CGPointMake(95, 100)];
[path closePath];
[path appendPath:[UIBezierPath bezierPathWithRect:rect]];
path.usesEvenOddFillRule = NO;
[path addClip];
//繪制軸
[path removeAllPoints];//畫另一條線,不與之前的線連接
[path moveToPoint:CGPointMake(100, 40)];
[path addLineToPoint:CGPointMake(100, 100)];
//設置周寬度
[path setLineWidth:5];
[path stroke];
}
三角形及箭頭
- 繪制箭頭
- (void) drawRect:(CGRect)rect{
//1.獲取圖形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2. 繪制大三角
CGContextMoveToPoint(ctx, 100, 0);
CGContextAddLineToPoint(ctx, 140, 40);
CGContextAddLineToPoint(ctx, 60, 40);
CGContextSetRGBFillColor(ctx, 1, 0, 0, 1.0);
CGContextFillPath(ctx);
//關閉路徑
CGPDFContextClose(cox);
#pragma >>>>下方的剪切部分
//3 小三角
CGContextMoveToPoint(ctx, 100, 80);
CGContextAddLineToPoint(ctx, 110, 100);
CGContextAddLineToPoint(ctx, 90, 100);
CGContextClosePath(ctx);
//添加剪切區域
CGContextAddRect(ctx, rect);
//用奇偶規則剪切
CGContextEOClip(ctx);
//4 繪制軸
CGContextMoveToPoint(ctx, 100, 40);
CGContextAddLineToPoint(ctx, 100, 100);
CGContextSetLineWidth(ctx, 20);
CGContextStrokePath(ctx);
}
- 漸變色
- (void) drawRect:(CGRect)rect{
//1.獲取圖形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2. 繪制大三角
CGContextMoveToPoint(ctx, 100, 0);
CGContextAddLineToPoint(ctx, 140, 40);
CGContextAddLineToPoint(ctx, 60, 40);
CGContextSetRGBFillColor(ctx, 1, 0, 0, 1.0);
CGContextFillPath(ctx);
//關閉路徑
CGPDFContextClose(ctx);
#pragma >>>>下方的剪切部分
//3 小三角
CGContextMoveToPoint(ctx, 100, 80);
CGContextAddLineToPoint(ctx, 110, 100);
CGContextAddLineToPoint(ctx, 90, 100);
CGContextClosePath(ctx);
//添加剪切區域
CGContextAddRect(ctx, rect);
//用奇偶規則剪切
CGContextEOClip(ctx);
//4 繪制軸
CGContextMoveToPoint(ctx, 100, 40);
CGContextAddLineToPoint(ctx, 100, 100);
CGContextSetLineWidth(ctx, 20);
// CGContextStrokePath(ctx);//關閉,下方漸變才生效
//確定漸變區域
//stroke ->fill
CGContextReplacePathWithStrokedPath(ctx);
//保存設置狀態
CGContextSaveGState(ctx);
CGContextClip(ctx);
//繪制漸變顏色
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
const CGFloat components[] = {
0.2,0.6,0.8,0.6,//開始的顏色
0.5,0.9,0.3,0.8,//中間的顏色1
0.7,0.5,0.2,0.7,//中間的顏色2
0.4,0.7,0.7,0.4,//結束的顏色
};
//設置漸變區域
const CGFloat locations[] = {0,0.32,0.56,1};
/**
space 顏色空間對象
components 顏色的數組,存儲的事rgb顏色
location 顏色所在位置
count :顏色的個數 = locations的個數
*/
CGGradientRef gradientref = CGGradientCreateWithColorComponents(space, components, locations, 4);
//繪制漸變
//顏色0對應的開始點,顏色數組中的結束點位置
CGContextDrawLinearGradient(ctx, gradientref,CGPointMake(90, 100),CGPointMake(110, 100),kCGGradientDrawsBeforeStartLocation);
//釋放顏色空間,漸變的對象
CGColorSpaceRelease(space);
CGGradientRelease(gradientref);
//恢復以前的區域
CGContextRestoreGState(ctx);

- 矩陣變幻 CGContextTranslateCTM
//1.獲取圖形上下文 Translate平移 Rotate旋轉 cale縮放
CGContextRef ctx=UIGraphicsGetCurrentContext();
//5.矩陣變換,
//a.坐標軸的變換
CGRect bounds=self.bounds;
//平移坐標軸
CGContextTranslateCTM(ctx, 0, bounds.size.height);
//翻轉Y的坐標軸
CGContextScaleCTM(ctx, 1, -1);
//移動位置,沿著x軸移動100
CGContextTranslateCTM(ctx, 100, 0);
//旋轉
CGContextRotateCTM(ctx, radians(90));
//縮放
CGContextScaleCTM(ctx, 0.5, 0.5);

# 非零繞數
在圖形學中判斷一個點是否在多邊形內,若多邊形不是自相交的,那么可以簡單的判斷這個點在多邊形內部還是外部;若多邊形是自相交的,那么就需要根據非零環繞數規則和奇-偶規則判斷。
在圖形學中判斷一個點是否在多邊形內,若多邊形不是自相交的,那么可以簡單的判的,那么就需要根據非零環繞數規則和奇-偶規則判斷。判斷多邊形是否是自相交的:多邊形在平面內除頂點外還有其他公共點內-外測試
不自交的多邊形:多邊形僅在頂點處連接,而在平面內沒有其他公共點,此時可以自相交的多邊形:多邊形在平面內除頂點外還有其他公共點,此時劃分內-外部分(1)奇-偶規則(Odd-even Rule):奇數表示在多邊形內,偶數表示在多邊形外從任意位置p作一條射線,若與該射線相交的多邊形邊的數目為奇數,則p是多邊(2)非零環繞數規則(Nonzero Winding Number Rule):若環繞數為0表示在多首先使多邊形的邊變為矢量。將環繞數初始化為零。再從任意位置p作一條射線。
邊計數,每當多邊形的邊從右到左穿過射線時,環繞數加1,從左到右時,環繞數減1則p為內部點,否則,p是外部點。
參考[1]中例子如下,判斷點p是否在多邊形內,從點p向外做一條射線(可以任意方向),多邊形的邊從線時環數加1,最后環數不為0,即表示在多邊形內部。
當然,非零繞數規則和奇偶規則會判斷出現矛盾的情況,如下圖所示,左側表示用 奇充。右側圖用非零繞環規則判斷出繞數為2,非0表示在多邊形內部,所以填充。
斷

需形
當
左
斷這個點在多邊形內部還是外部;若多邊形是自相交
直接劃分內-外部分。要采用以下的方法。
內部點,否則是外部點。邊形內,非零表示在多邊形外
從p點沿射線方向移動時,對在每個方向上穿過射線的。處理完多邊形的所有相關邊之后,若環繞數為非零,
到右經過射線時環數減1,多邊形的邊從右往左經過射
偶規則判斷繞環數為2 ,表示在多邊形外,所以沒有填
以需
形多
當1

左
奇
另外一個例子,如下
非零環繞數規則和奇-偶規則(Non-Zero Winding Number Rule&&Odd-even Ru1.CGContextClip 使用非零環繞數規則來判斷當前路徑和裁剪路徑的交集。2.CGContextEOClip 使用奇偶環繞數規則來判斷當前路徑和裁剪路徑的交集。

u