Skip to content

Instantly share code, notes, and snippets.

@Tantas
Last active August 29, 2015 14:05
Show Gist options
  • Save Tantas/4e49ce9cee896619f16e to your computer and use it in GitHub Desktop.
Save Tantas/4e49ce9cee896619f16e to your computer and use it in GitHub Desktop.
Creates a nPointed star with the odd point at the center-top.
#import <SpriteKit/SpriteKit.h>
@interface NPointedStar : SKShapeNode
/** Creates a nPointed star with the odd point at the center-top.
Must provide all parameters normally added to a SKShapeNode except for the path.
Example:
(Inside a SKScene class)
NPointedStar* fivePointStar = [[NPointedStar alloc] initWithRadius:20.0f nPoints:5 fatness:1.0f];
fivePointStar.position = CGPointMake(self.size.width/2, self.size.height/2);
fivePointStar.fillColor = [SKColor yellowColor];
[self addChild:fivePointStar];
*/
-(instancetype)initWithRadius:(float)radius nPoints:(NSUInteger)points fatness:(float)fatness;
@end
#import "NPointedStar.h"
@implementation NPointedStar
-(instancetype)initWithRadius:(float)radius nPoints:(NSUInteger)points fatness:(float)fatness
{
if (self = [super init]) {
CGMutablePathRef starPath = CGPathCreateMutable();
float alpha = 2 * M_PI / points/2;
for (NSUInteger i = 0; i < points*2+1; i++) {
float r = radius* (i%2+fatness)/2;
float omega = i*alpha;
if (i == 0) { // Change '-r' to 'r' if the odd-th number of point should point down
CGPathMoveToPoint(starPath, NULL, -r*sinf(omega), -r*cosf(omega));
} else {
CGPathAddLineToPoint(starPath, NULL, -r*sinf(omega), -r*cosf(omega));
}
}
self.path = starPath;
}
return self;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment