Skip to content

Instantly share code, notes, and snippets.

@Tantas
Created July 31, 2014 01:43
Show Gist options
  • Save Tantas/26f4282e2618b585fc6a to your computer and use it in GitHub Desktop.
Save Tantas/26f4282e2618b585fc6a to your computer and use it in GitHub Desktop.
SKShapeNode+Area
#import <SpriteKit/SpriteKit.h>
@interface SKShapeNode (Area)
-(float) area;
@end
#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