Note that there are some explanatory texts on larger screens.

plurals
  1. PODrawing circles using CGContext
    primarykey
    data
    text
    <p>Using the code below, I am constructing polygons with various number of sides.</p> <p>Can somebody advise how I can add code to circumscribe a circle and also inscribe a circle in each polygon returned?</p> <pre><code>-(void) drawRect:(CGRect)rect{ CGContextRef context = UIGraphicsGetCurrentContext(); CGContextBeginPath (context); CGContextSetLineWidth(context,5); NSArray * points = [PolygonUIView pointsForPolygonInRect:[self bounds] numberOfSides:polygon.numberOfSides]; NSLog(@"%d", [points count]); NSLog(@"%d", polygon.numberOfSides); for(NSValue * point in points) { CGPoint val = [point CGPointValue]; if([points indexOfObject:point]==0) { CGContextMoveToPoint (context, val.x, val.y); } else { CGContextAddLineToPoint (context, val.x, val.y); } } CGContextClosePath(context); [[UIColor clearColor] setFill]; [[UIColor blackColor] setStroke]; CGContextDrawPath (context, kCGPathFillStroke); polygonLabel.text = polygon.name; } + (NSArray *)pointsForPolygonInRect:(CGRect)rect numberOfSides:(int)numberOfSides { CGPoint center = CGPointMake(rect.size.width / 2.0, rect.size.height / 2.0); float radius = 0.90 * center.x; NSLog(@"%f rad",radius); NSMutableArray *result = [NSMutableArray array]; float angle = (2.0 * M_PI) / numberOfSides; float exteriorAngle = M_PI - angle; float rotationDelta = angle - (0.5 * exteriorAngle); for (int currentAngle = 0; currentAngle &lt; numberOfSides; currentAngle++) { float newAngle = (angle * currentAngle) - rotationDelta; float curX = cos(newAngle) * radius; float curY = sin(newAngle) * radius; [result addObject:[NSValue valueWithCGPoint:CGPointMake(center.x + curX, center.y + curY)]]; } return result; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload