Skip to content

Instantly share code, notes, and snippets.

@sgoodwin
Created May 1, 2012 09:19
Show Gist options
  • Save sgoodwin/2566653 to your computer and use it in GitHub Desktop.
Save sgoodwin/2566653 to your computer and use it in GitHub Desktop.
//
// GOParametricView.m
// CircleTest
//
// Created by Samuel Goodwin on 5/1/12.
//
#import "GOParametricView.h"
#import <QuartzCore/QuartzCore.h>
const CGFloat dotWidth = 10.0f;
@implementation GOParametricView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setBackgroundColor:[UIColor whiteColor]];
[self setClearsContextBeforeDrawing:NO];
_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(setNeedsDisplay)];
[_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
_startDate = [NSDate date];
}
return self;
}
- (void)drawRect:(CGRect)rect{
// yay parametrics!
// y = sin(omega *t), omega = 2*pi*f
// x = cos(omega *t), omega = 2*pi*f
CGFloat magnitude = 50.0f;
CGFloat t = -1.0f*((CGFloat)[self.startDate timeIntervalSinceNow]);
CGFloat x = magnitude*cosf(2*M_PI*self.xFrequency*t);
CGFloat y = magnitude*sinf(2*M_PI*self.yFrequency*t);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, CGRectGetWidth(rect)/2.0f, CGRectGetHeight(rect)/2.0f);
CGContextSetLineWidth(context, 20.0f);
CGContextFillEllipseInRect(context, [self rectWithCenterPoint:CGPointMake(0.0f, 0.0f)]);
CGContextFillEllipseInRect(context, [self rectWithCenterPoint:CGPointMake(x, y)]);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment