以下のコードを使用して、さまざまな辺の数のポリゴンを作成しています。CGContextを使用して円を描く
誰かが、円を囲むコードを追加したり、返された各ポリゴンの円に刻印する方法をアドバイスできますか?
-(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;
}
ハイテクはあなたの答えに感謝しかし、ポイントの計算方法のより詳細な説明を必要とする必要がある結果を与えるそれをsussed返された各ポリゴンは外接し、正しく刻印されます。 – superllanboy
可能なポリゴンの一般的な答えは?私が恐れていることは分かりません。数学のスタック交換について質問する必要があります。 CGPathGetBoundingBoxのどこかで、あなたのパスを囲む矩形を与え、その対角線を使って半径を与えることができますか? – jrturton
お返事ありがとうございました - あなたは私にアイデアの種を与えましたので、私は周りを回り、働くことができるかどうかを見てみましょう – superllanboy