Quartz2D 绘五角星

五角星.png
#pragma mark 画五角星
- (void) _drawStarWithContext:(CGContextRef)ctx
{
// 1. 基本数据
// 圆心 与 半径
CGPoint centerPoint = CGPointMake(170., 170.);
CGFloat radius = 90.;

// 2. 绘制路径
UIBezierPath *path = [UIBezierPath bezierPath];

// 两个点的夹角弧度
CGFloat angle = 2 * (2 * M_PI / 5.);

// 第一个点
CGPoint point1 = CGPointMake(centerPoint.x, centerPoint.y - radius);
[path moveToPoint:point1];

// 其他点
for (int i = 1; i <= 5; i++) {
    /** 
     1.0 确定了 point1 后,要确定连线的下一个点,由于 point1 与坐标 x 轴夹角为90度,所以与下一个点的夹角要加上90度。
     1.1 五角星是跨越一个点连线的,point1 与 point3 的夹角为 2 * 72度。
     1.2 计算把角度转换成弧度。
     1.3 结合圆弧上任意一点的坐标计算公式:
         '如果是圆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 结合转换公式:
         sin(π / 2 + θ)= cosθ
         cos(π / 2 + θ )= -sinθ
     简化后:
     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)];
}

// 上下文的状态
CGContextSetLineWidth(ctx, 1.);
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineJoin(ctx, kCGLineJoinRound);
[[UIColor whiteColor] set];

// 3. 把绘制的内容保存到上下文当中。
CGContextAddPath(ctx, path.CGPath);

// 4. 把上下文的内容显示到View上
//CGContextStrokePath(ctx);
CGContextFillPath(ctx);

// 辅助圆
CGContextBeginPath(ctx);
[[UIColor whiteColor] set];
CGContextAddArc(ctx, centerPoint.x, centerPoint.y, radius, 0, 2. * M_PI, 1);
CGContextStrokePath(ctx);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 《校园写生》-1 美术老师说像梵高?好吧 分明是爱因斯坦雕塑好吧?嘿嘿
    那里面阅读 176评论 0 2
  • 周末愉快!今天只发图,不说话~ 又是平胸! 唉,胸不平 何以平天下么? 祝我的朋友们周末愉快!
    那里面阅读 99评论 0 2
  • 《北极星》这本绘本是一位好朋友拿给我看的。它的画风非常的小清新,延续了作者彼得•雷诺兹一贯善用的水彩风格,没有任何...
    新桌子和旧板凳阅读 5,866评论 0 2
  • 人生其实就是一场修行,修的是一颗心,修的是一颗不污不垢、淡看浮华的平淡、平和、宁静之心。 学会平淡,便会淡看荣辱得...
    那里面阅读 249评论 0 6
  • 很多工作很多年的人,当告诉别人说自己以前有做船务的时候,大多的人表示不知道是干什么的,还得在那边向人解释好久。为此...
    猴哥如是说阅读 4,327评论 2 0