Skip to content

Instantly share code, notes, and snippets.

@C4Examples
Created July 24, 2012 15:44
Show Gist options
  • Save C4Examples/3170776 to your computer and use it in GitHub Desktop.
Save C4Examples/3170776 to your computer and use it in GitHub Desktop.
Line, Triangles & Polygons
//
// C4WorkSpace.m
// Examples
//
// Created by Travis Kirton on 12-07-23.
//
#import "C4WorkSpace.h"
@implementation C4WorkSpace {
C4Shape *line, *triangle, *polygon, *random;
}
-(void)setup {
//create a 2 point array for the line
CGPoint linePoints[2];
linePoints[0] = CGPointMake(0, 0);
linePoints[1] = CGPointMake(100, 100);
line = [C4Shape line:linePoints];
//create a 3 point array for the triangle
CGPoint trianglePoints[3];
trianglePoints[0] = CGPointMake(0, 0);
trianglePoints[1] = CGPointMake(0, 100);
trianglePoints[2] = CGPointMake(100, 0);
triangle = [C4Shape triangle:trianglePoints];
//create a 4 point array for the polygon
CGPoint polyPoints[4];
polyPoints[0] = CGPointMake(0, 0);
polyPoints[1] = CGPointMake(0, 100);
polyPoints[2] = CGPointMake(100, 0);
polyPoints[3] = CGPointMake(100, 100);
polygon = [C4Shape polygon:polyPoints pointCount:4];
line.backgroundColor = [UIColor clearColor];
//create an array between 10 and 20 points for the random shape
NSInteger randomCount = [C4Math randomInt:10]+10;
CGPoint randomPoints[randomCount];
for(int i = 0; i < randomCount; i++) {
randomPoints[i] = CGPointMake([C4Math randomInt:100], [C4Math randomInt:100]);
}
random = [C4Shape polygon:randomPoints pointCount:10];
//Figure out the vertical whitespace (i.e. the space between shapes and the edges of the canvas)
CGFloat whiteSpace = self.canvas.height - 4* line.height;
whiteSpace = whiteSpace/5; // because there are 5 gaps
//create a point that will define the center of each shape, aligned to the middle of the canvas
CGPoint center;
center.x = self.canvas.center.x;
//set the y position for the line
center.y = whiteSpace + line.height/2;
line.center = center;
//set the y position for the triangle
center.y = center.y + whiteSpace + triangle.height;
triangle.center = center;
//set the y position for the polygon
center.y = center.y + whiteSpace + polygon.height;
polygon.center = center;
//set the y position for the random shape
center.y = center.y + whiteSpace + random.height;
random.center = center;
[self.canvas addShape:line];
[self.canvas addShape:triangle];
[self.canvas addShape:polygon];
[self.canvas addShape:random];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment