Using the code below, I am constructing polygons with various number of sides.
Can somebody advise how I can add code to circumscribe a circle and also inscribe a circle in each polygon returned?
-(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 < 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;
}
A compass, more accurately known as a pair of compasses, is a technical drawing instrument that can be used for inscribing circles or arcs. As dividers, it can also be used as a tool to step out distances, in particular, on maps. Compasses can be used for mathematics, drafting, navigation and other purposes.
Using the circles and everything makes drawing much more simplified, actually. It basically organizes proportions for you and anatomy and such. It doesn't actually take all that much effort to actually do it, since your shapes don't have to be perfect.
finally sussed it out thanks to some pointers from others code below added to the original code gives me the result i need thanks
// circumscribe the polygons
CGContextSetLineWidth(context, 2); // set the line width
CGContextSetRGBStrokeColor(context, 20.0 /255, 101.0 / 255.0, 18.0 / 255.0, 1.0);
CGPoint center = CGPointMake(rect.size.width / 2, rect.size.height / 2); // get the circle centre
CGFloat radius = 0.9 * center.x; // little scaling needed
CGFloat startAngle = -((float)M_PI / 2); // 90 degrees
CGFloat endAngle = ((2 * (float)M_PI) + startAngle);
CGContextAddArc(context, center.x, center.y, radius + 4, startAngle, endAngle, 0); // create an arc the +4 just adds some pixels because of the polygon line thickness
CGContextStrokePath(context); // draw
// inscribed circle
float angle = M_PI - ((2 * M_PI) / polygon.numberOfSides); // need the polygon angles
CGContextSetLineWidth(context, 2); // set the line width
CGContextSetRGBStrokeColor(context, 20.0 / 255, 101.0 / 255.0, 211.0 / 255.0, 1.0);
CGFloat innerradius = (0.9 * center.x) * sin(angle / 2); // calc the inner radius
CGContextAddArc(context, center.x, center.y, innerradius - 3, startAngle, endAngle, 0); // create an arc the minus 3 subtracts some pixels because of the polygon line thickness
CGContextStrokePath(context); // draw
You draw circles using CGContextAddEllipseInRect()
, passing in a square CGRect. Calculating the rect to fit inside each polygon you return is a bit more involved, depending on how tightly you want it to fit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With