CoreGraphics 図形を描画する 線, 曲線

CGXxxxで図形を描画するためのAPIをいくつかをピックアップ。

円(Circle)

CGContextAddEllipseInRect( CGContextRef, CGRect);

線(Line)その1

CGContextAddLines(CGContextRef,GPoint配列,ポイント数);

線(Line)その2

CGContextMoveToPoint(CGContextRef, 横位置 , 縦位置);
CGContextAddLineToPoint(context, 横位置, 縦位置);
CGContextStrokePath(context);

例)

CGContextSetRGBStrokeColor(context, 0.0, 0.0, 1.0, 1.0);
CGContextMoveToPoint(context, 30, 30);
CGContextAddLineToPoint(context, 150, 350);
CGContextStrokePath(context);

もう少し複雑な線(Curve Line)

CGContextMoveToPoint(CGContextRef, 横位置 , 縦位置 );
CGContextAddCurveToPoint( CGContextRef, 横位置c1 , 縦位置c1 ,
横位置c2 , 縦位置c2 , 横位置 , 縦位置 );

ベジエ曲線

CurveToPointとはまた違った, ベジエ曲線を書くためのUIBezierPathというのがあります。

サンプルその1 線など

- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context,
1.0, 1.0, 0.0, 1.0);
CGContextSetRGBStrokeColor(context,
1.0, 0.0, 1.0, 1.0);
CGContextSetLineWidth(context, 10.0);

CGRect r1 = CGRectMake(20.0 , 20.0, 100.0, 100.0);
CGContextAddEllipseInRect(context,r1);
CGContextFillPath(context);

CGContextMoveToPoint(context, 50, 100);
CGContextAddLineToPoint(context, 150, 100);
CGContextAddLineToPoint(context, 150, 150);
CGContextAddLineToPoint(context, 50, 150);
CGContextAddLineToPoint(context, 50, 200);
CGContextAddLineToPoint(context, 150, 200);
CGContextStrokePath(context);

CGContextSetRGBStrokeColor(context,
0.0, 1.0, 0.0, 1.0);
CGContextMoveToPoint(context,50, 250);
CGContextAddCurveToPoint(context,
100, 250, 100, 200, 150, 250);
CGContextAddCurveToPoint(context,
200, 350, 50, 250, 50, 350);
CGContextStrokePath(context);
}

drawline