Last active
August 29, 2015 14:05
-
-
Save Tantas/4e49ce9cee896619f16e to your computer and use it in GitHub Desktop.
Creates a nPointed star with the odd point at the center-top.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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