Draw a star and determine its area: cocos2D

Sunday morning and no plans. I was reading a blog post from one of my favorite programmer emanuele feronato. I have been following him from my early flash programming days. I decided to do a port one of his as3 tutorial to cocos2d iPhone, and help me kill time. This port  draws a random star shape from lines and calculate its area.

Port may not be perfect so I would love to see yours. The original as3 code.

star_area

// Import the interfaces

#import "HelloWorldLayer.h"
#import "AppDelegate.h"

#pragma mark - HelloWorldLayer

// HelloWorldLayer implementation

@implementation HelloWorldLayer

// Helper class method that creates a Scene with the HelloWorldLayer as the only child.

+(CCScene *) scene

{

// 'scene' is an autorelease object.

CCScene *scene = [CCScene node];

// 'layer' is an autorelease object.

HelloWorldLayer *layer = [HelloWorldLayer node];

// add layer as a child to scene

[scene addChild: layer];

// return the scene

return scene;

}

// on "init" you need to initialize your instance

-(id) init

{

// always call "super" init

// Apple recommends to re-assign "self" with the "super's" return value

if( (self=[super init]) ) {

NSInteger spikes = (arc4random() % 10) + 3;

NSInteger outer = (arc4random() % 200) + 40;

NSInteger inner = (arc4random() % outer);

CGFloat offset = roundf(arc4random() % 360);

[self createStar:spikes :CGPointMake(160, 120) :outer :inner :72];

NSString *infoString = [NSString stringWithFormat:@"Spikes: %d\nOuter arm: %d\nInner arm: %d\nOffset angle: %.02f\nArea: %.02f", spikes, outer, inner, offset, [self getArea]];

infoText = [CCLabelTTF labelWithString:infoString fontName:@"Helvetica" fontSize:18];

[infoText setPosition:CGPointMake(infoText.contentSize.width/2, [[CCDirector sharedDirector] winSize].height - infoText.contentSize.height/2)];

[self addChild:infoText z:2];

}

return self;

}

// on "dealloc" you need to release all your retained objects

- (void) dealloc

{

[vertices release];

vertices = nil;

[super dealloc];

}

- (void) draw

{

glLineWidth(3.0f);

// set line color.

ccDrawColor4F(255, 1.0, 0.76, 255);

for (int i = 0; i < vertices.count - 1; i++) {

NSValue *val = [vertices objectAtIndex:i];

NSValue *val2 = [vertices objectAtIndex:i+1];

CGPoint from = [val CGPointValue];

CGPoint to = [val2 CGPointValue];

ccDrawLine(from, to);

}

NSValue *val = [vertices objectAtIndex:0];

NSValue *val2 = [vertices objectAtIndex:vertices.count - 1];

CGPoint from = [val CGPointValue];

CGPoint to = [val2 CGPointValue];

ccDrawLine(to, from);

}

-(void)createStar : (NSInteger)arms : (CGPoint)center : (NSInteger)outer : (NSInteger)inner : (CGFloat)offsetAngle

{

offsetAngle *= 0.0174532925;

if (vertices == nil) {

vertices = [[NSMutableArray alloc] init];

}

else

{

[vertices removeAllObjects];

}

CGFloat r;

CGFloat angle = M_PI / arms;

for (int i = 0; i < 2 * arms; i++) {

if ((i % 2) == 0) {

r = outer;

}

else {

r = inner;

}

CGPoint point = CGPointMake(center.x + cosf(i * angle - angle / 2 + offsetAngle) * r, center.y + sinf(i * angle - angle/ 2+ offsetAngle) * r);

[vertices addObject:[NSValue valueWithCGPoint:point]];

}

}

-(CGFloat) getArea

{

NSInteger n = vertices.count;

CGFloat surface = 0.0f;

for (int i = 0; i < n; i++) {

int j = (i+1) % n;

NSValue *val = [vertices objectAtIndex:i];

NSValue *val2 = [vertices objectAtIndex:j];

CGPoint pVal = [val CGPointValue];

CGPoint pVal2 = [val2 CGPointValue];

surface += pVal.x * pVal2.y;

surface -= pVal.y * pVal2.x;

}

surface = surface/2;

return surface;

}
@end

Be the first to leave a comment. Don’t be shy.

Join the Discussion

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>