五角星.png
#pragma mark 畫五角星
- (void) _drawStarWithContext:(CGContextRef)ctx
{
// 1. 基本數(shù)據(jù)
// 圓心 與 半徑
CGPoint centerPoint = CGPointMake(170., 170.);
CGFloat radius = 90.;
// 2. 繪制路徑
UIBezierPath *path = [UIBezierPath bezierPath];
// 兩個(gè)點(diǎn)的夾角弧度
CGFloat angle = 2 * (2 * M_PI / 5.);
// 第一個(gè)點(diǎn)
CGPoint point1 = CGPointMake(centerPoint.x, centerPoint.y - radius);
[path moveToPoint:point1];
// 其他點(diǎn)
for (int i = 1; i <= 5; i++) {
/**
1.0 確定了 point1 后东羹,要確定連線的下一個(gè)點(diǎn)端礼,由于 point1 與坐標(biāo) x 軸夾角為90度萤厅,所以與下一個(gè)點(diǎn)的夾角要加上90度目代。
1.1 五角星是跨越一個(gè)點(diǎn)連線的,point1 與 point3 的夾角為 2 * 72度点弯。
1.2 計(jì)算把角度轉(zhuǎn)換成弧度扇调。
1.3 結(jié)合圓弧上任意一點(diǎn)的坐標(biāo)計(jì)算公式:
'如果是圓O:
x^2 + y^2 = 1
{x = cosθ
{y = sinθ
'如果是圓C(a,b)
(x - a)^2 + (y - b)^2 = r^2
{x = a + r * cosθ
{y = b + r * sinθ
得出:
CGFloat x = centerPoint.x + radius * cos((90. + i * 72. * 2) * M_PI / 180.);
CGFloat y = centerPoint.y - radius * sin((90. + i * 72. * 2) * M_PI / 180.);
1.4 結(jié)合轉(zhuǎn)換公式:
sin(π / 2 + θ)= cosθ
cos(π / 2 + θ )= -sinθ
簡(jiǎn)化后:
CGFloat angle = 2 * (2 * M_PI / 5.);
CGFloat x = centerPoint.x - sinf(i * angle) * radius;
CGFloat y = centerPoint.y - cosf(i * angle) * radius;
*/
CGFloat x = centerPoint.x - sinf(i * angle) * radius;
CGFloat y = centerPoint.y - cosf(i * angle) * radius;
// 連線
[path addLineToPoint:CGPointMake(x, y)];
}
// 上下文的狀態(tài)
CGContextSetLineWidth(ctx, 1.);
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineJoin(ctx, kCGLineJoinRound);
[[UIColor whiteColor] set];
// 3. 把繪制的內(nèi)容保存到上下文當(dāng)中。
CGContextAddPath(ctx, path.CGPath);
// 4. 把上下文的內(nèi)容顯示到View上
//CGContextStrokePath(ctx);
CGContextFillPath(ctx);
// 輔助圓
CGContextBeginPath(ctx);
[[UIColor whiteColor] set];
CGContextAddArc(ctx, centerPoint.x, centerPoint.y, radius, 0, 2. * M_PI, 1);
CGContextStrokePath(ctx);
}