Skip to content

Instantly share code, notes, and snippets.

@C4Tutorials
Last active December 16, 2015 06:19
Show Gist options
  • Save C4Tutorials/5391050 to your computer and use it in GitHub Desktop.
Save C4Tutorials/5391050 to your computer and use it in GitHub Desktop.
Spiral made using a custom CGPath
//
// C4WorkSpace.m
// Complex Shapes Tutorial
//
// Created by Travis Kirton.
//
#import "C4WorkSpace.h"
@implementation C4WorkSpace
-(void)setup {
CGMutablePathRef spiralPath = CGPathCreateMutable();
CGPathMoveToPoint(spiralPath, nil, 0, 0);
//Un-comment each block and recompile, using only one block at a time
for(int i = 1; i < 64; i++) {
CGFloat radius = 4 * i;
CGFloat angle = TWO_PI/16 * i;
CGPoint nextPoint = CGPointMake(radius*[C4Math sin:angle], radius*[C4Math cos:angle]);
// CGPathAddLineToPoint(spiralPath, nil, nextPoint.x, nextPoint.y);
// CGPathAddRelativeArc(spiralPath, nil, nextPoint.x, nextPoint.y, radius, 0, TWO_PI/16);
// CGPathAddRelativeArc(spiralPath, nil, nextPoint.x, nextPoint.y, radius, angle, TWO_PI/16);
// CGPathAddRelativeArc(spiralPath, nil, 0,0, radius, angle, TWO_PI/16);
//a smooth spiral
CGPoint controlPoint = CGPointMake(radius*[C4Math sin:angle - TWO_PI/32],
radius*[C4Math cos:angle - TWO_PI/32]);
CGPathAddQuadCurveToPoint(spiralPath, nil, controlPoint.x, controlPoint.y, nextPoint.x, nextPoint.y);
// radius *= 1.5f;
// CGPoint controlPoint = CGPointMake(radius*[C4Math sin:angle - TWO_PI/32],
// radius*[C4Math cos:angle - TWO_PI/32]);
// CGPathAddQuadCurveToPoint(spiralPath, nil, controlPoint.x, controlPoint.y, nextPoint.x, nextPoint.y);
}
CGRect frame = CGPathGetBoundingBox(spiralPath);
C4Shape *spiral = [C4Shape rect:frame];
const CGAffineTransform translate = CGAffineTransformMakeTranslation(-frame.origin.x, -frame.origin.y);
spiral.path = CGPathCreateCopyByTransformingPath(spiralPath, &translate);
spiral.center = self.canvas.center;
[self.canvas addShape:spiral];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment