Skip to content

Instantly share code, notes, and snippets.

@C4Code
Last active October 11, 2015 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save C4Code/3812753 to your computer and use it in GitHub Desktop.
Save C4Code/3812753 to your computer and use it in GitHub Desktop.
Simple, arbitrary, random, arc, diagrams.
//
// C4WorkSpace.m
// arcDiagrams
//
// Created by moi on 12-09-30.
// Copyright (c) 2012 moi. All rights reserved.
//
#import "C4WorkSpace.h"
@implementation C4WorkSpace {
C4Shape *arc1;
CGPoint pts[2];
BOOL canAddArc;
}
-(void)setup {
canAddArc = YES;
}
-(void)touchesBegan {
if(canAddArc) {
canAddArc = NO;
int i = [self.canvas.subviews count];
if(i == 0) pts[0] = CGPointMake((CGFloat)[C4Math randomInt:(int)self.canvas.width],self.canvas.center.y);
else pts[0] = pts[1];
pts[1] = CGPointMake((CGFloat)[C4Math randomInt:(int)self.canvas.width], self.canvas.center.y);
BOOL up = (i % 2 == 0) ? YES : NO;
C4Shape *arc = [self arcBetweenPoints:pts up:up];
arc.userInteractionEnabled = NO;
[self.canvas addShape:arc];
[self runMethod:@"animateArc:" withObject:arc afterDelay:0.05f];
}
}
-(C4Shape *)arcBetweenPoints:(CGPoint *)beginEndPoints up:(BOOL)up {
CGPoint p1 = beginEndPoints[0];
CGPoint p2 = beginEndPoints[1];
CGFloat radius = [C4Math absf:p2.x - p1.x]/2;
CGPoint center = self.canvas.center;
if(p2.x > p1.x) {
center.x = p1.x + radius;
} else {
center.x = p2.x+radius;
}
CGFloat startAngle;
if(up == YES) startAngle = PI;
else startAngle = 0;
CGFloat endAngle = startAngle + PI;
C4Shape *s = [C4Shape arcWithCenter:center radius:radius
startAngle:startAngle endAngle:endAngle clockwise:YES];
s.fillColor = [UIColor clearColor];
if(p2.x > p1.x) {
if(up) {
s.strokeEnd = 0.0f;
} else {
s.strokeStart = 1.0f;
}
} else {
if(up) {
s.strokeStart = 1.0f;
} else {
s.strokeEnd = 0.0f;
}
}
if(up) s.strokeColor = C4GREY;
return s;
}
-(void)animateArc:(C4Shape *)arc {
arc.animationDuration = 1.0f;
if(arc.strokeStart == 1.0f) arc.strokeStart = 0.0f;
else arc.strokeEnd = 1.0f;
[self runMethod:@"canAdd" afterDelay:arc.animationDuration];
}
-(void)canAdd {
canAddArc = YES;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment