CLOSE SEARCH

[Snippet] Draw Polygon using Quartz

– (void)drawPolygon:(CGContextRef)context
{

//star
CGPoint center = CGPointMake(80.0, 100.0);
CGContextMoveToPoint(context, center.x, center.y + 30.0);
for (int i = 1; i < 5; ++i)
{

CGFloat x = 30.0 * sinf(i * 4.0 * M_PI / 5.0);

CGFloat y = 30.0 * cosf(i * 4.0 * M_PI / 5.0);

CGContextAddLineToPoint(context, center.x + x, center.y + y);

}

CGContextClosePath(context);
CGContextFillPath(context);

//star + circle
center = CGPointMake(160.0, 100.0);
CGContextMoveToPoint(context, center.x, center.y + 30.0);
for (int i = 1; i < 5; i++)
{

CGFloat x = 30.0 * sinf(i * 4.0 * M_PI / 5.0);
CGFloat y = 30.0 * cosf(i * 4.0 * M_PI / 5.0);
CGContextAddLineToPoint(context, center.x + x, center.y + y);

}
CGContextAddEllipseInRect(context, CGRectMake(130, 70, 60, 60));
CGContextClosePath(context);
CGContextEOFillPath(context);

//pentagon
center = CGPointMake(240.0, 100.0);
CGContextMoveToPoint(context, center.x, center.y + 30.0);
for (int i = 1; i < 6; i++)
{

CGFloat x = 30.0 * sinf(i * 2.0 * M_PI / 6.0);
CGFloat y = 30.0 * cosf(i * 2.0 * M_PI / 6.0);
CGContextAddLineToPoint(context, center.x + x, center.y + y);

}
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);

}

 

Filed under: iOS