1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//
//  PolygonView.m
//
//  Created by Tommy Bergeron on 10-03-02.
//  Copyright 2010 Egzakt. All rights reserved.
//

#import "PolygonView.h"

@implementation PolygonView

- (void)drawRect:(CGRect)rect {
	NSLog(@"drawRect");
	
	NSLog(@"Crap: %@", [polygon description]);
}

+ (NSArray *)pointsForPolygonInRect:(CGRect)rect numberOfSides:(int)numberOfSides {
	CGPoint center = CGPointMake(rect.size.width / 2.0, rect.size.height / 2.0);
	if (numberOfSides % 2 != 0) {
		center.y = center.y * (1.0 + .1 * ((12.0 - (float)numberOfSides) / 12.0));
	}
	float radius = 0.8 * center.x;
	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;
}

@end