Created
July 31, 2014 01:43
-
-
Save Tantas/26f4282e2618b585fc6a to your computer and use it in GitHub Desktop.
SKShapeNode+Area
This file contains 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 SKShapeNode (Area) | |
-(float) area; | |
@end |
This file contains 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 "SKShapeNode+Area.h" | |
static void extractPointApplier(void* info, const CGPathElement* element) { | |
[((__bridge NSMutableArray*) info) addObject:[NSValue valueWithCGPoint:*element->points]]; | |
} | |
@implementation SKShapeNode (Area) | |
-(float) area { | |
NSMutableArray* pathPoints = [NSMutableArray array]; | |
CGPathApply(self.path, (__bridge void *)(pathPoints), extractPointApplier); | |
float area = 0; | |
NSUInteger j = [pathPoints count]-1; | |
for (NSUInteger i = 0; i < [pathPoints count]; i++) { | |
CGPoint pointJ = [[pathPoints objectAtIndex:j] CGPointValue]; | |
CGPoint pointI = [[pathPoints objectAtIndex:i] CGPointValue]; | |
area += (pointJ.x+pointI.x) * (pointJ.y-pointI.y); | |
j = i; | |
} | |
return abs(area/2); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment