Skip to content

Instantly share code, notes, and snippets.

@robinhayward
Created November 30, 2012 15:13
Show Gist options
  • Save robinhayward/4176327 to your computer and use it in GitHub Desktop.
Save robinhayward/4176327 to your computer and use it in GitHub Desktop.
Sports Formations
#import "FASFormationFactory.h"
@implementation FASFormationFactory
+ (void)test
{
FASFormationFactory *factory = [[FASFormationFactory alloc] init];
NSArray *map = [factory formationForContainer:CGSizeMake(320.0f, 400.0f) lines:[NSArray arrayWithObjects:@"1", @"4", @"4", @"2", nil] itemSize:CGSizeMake(67, 100)];
}
- (NSArray *)formationForContainer:(CGSize)container lines:(NSArray *)lines itemSize:(CGSize)itemSize
{
NSMutableArray *points = [NSMutableArray arrayWithCapacity:0];
NSInteger numberOfLines = [lines count];
NSArray *linePositions = [self midPointsForDistance:container.height itemWidth:itemSize.height itemCount:numberOfLines];
for (NSInteger i = 0; i < numberOfLines; i++)
{
NSString *line = [lines objectAtIndex:i];
NSInteger lineSize = [line integerValue];
NSInteger y = [[linePositions objectAtIndex:i] floatValue];
NSArray *linePoints = [self midPointsForDistance:container.width itemWidth:itemSize.width itemCount:lineSize];
for (NSNumber *x in linePoints)
{
CGPoint point = CGPointMake([x floatValue], y);
[points addObject:[NSValue valueWithCGPoint:point]];
}
}
return points;
}
- (NSArray *)midPointsForDistance:(CGFloat)distance itemWidth:(CGFloat)width itemCount:(NSInteger)count
{
NSMutableArray *points = [NSMutableArray arrayWithCapacity:0];
CGFloat space = (distance - (count * width)) / (count + 1);
for (int i = 0; i < count; i++) {
CGFloat point = space + ((width / 2));
if (i > 0) {
point += (width * i);
point += (space * i);
}
[points addObject:[NSNumber numberWithFloat:point]];
}
return points;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment