Skip to content

Instantly share code, notes, and snippets.

@projectxcappe
Created August 30, 2011 00:40
Show Gist options
  • Save projectxcappe/1179803 to your computer and use it in GitHub Desktop.
Save projectxcappe/1179803 to your computer and use it in GitHub Desktop.
#import "CPCustomView.h"
#include "math.h"
#define LENGTH 250
#define HEIGHT 250
@implementation CPCustomView
@synthesize bgColor = _bgColor;
@synthesize MVO_D_Points = _MVO_D_Points;
@synthesize TCP_U_Points = _TCP_U_Points;
@synthesize TCP_D_Points = _TCP_D_Points;
@synthesize MVO_U_Points = _MVO_U_Points;
- (void)setBgColor:(UIColor *)bgColor{
[_bgColor release];
_bgColor = [bgColor retain];
[self setNeedsDisplay];
}
- (void)awakeFromNib{
self.bgColor = [UIColor orangeColor];
}
- (void)drawRect:(CGRect)rect
{
[self.bgColor setFill];
[self graphPoints:_MVO_D_Points];
// [self graphPoints:_MVO_U_Points];
// [self graphPoints:_TCP_D_Points];
// [self graphPoints:_TCP_U_Points];
}
-(void) graphPoints:(NSArray *)nsArray{
NSLog(@"GraphPoints%@ ", nsArray);
CGContextRef context = UIGraphicsGetCurrentContext();
NSUInteger count = [nsArray count];
CGFloat prevValX;
CGFloat prevValY;
CGFloat deltaX = LENGTH / (count - 1);
// CGFloat deltaY = HEIGHT / (count - 1) ;
//allocate data points for graph
for(int i=1; i<count; i++){
NSValue *currentPointValue = [nsArray objectAtIndex:i];
CGPoint currentPoint = [currentPointValue CGPointValue];
CGPoint previousPoint = [[nsArray objectAtIndex:i-1] CGPointValue];
prevValX = previousPoint.x;
prevValY = previousPoint.y;
//Scale the graph
// CGFloat xVal = count + (deltaX * i);
// CGFloat yVal = currentPoint.y;
CGFloat xVal = rand() % 250;
CGFloat yVal = rand() % 250;
//Draw Points
if (nsArray == _MVO_U_Points) {
NSLog(@"MVO D");
}else if (nsArray == _MVO_U_Points){
// NSLog(@"MVO U");
}else if (nsArray == _TCP_D_Points){
// NSLog(@"TCP D");
}else { //_TCP_U_Point
// NSLog(@"TCP U");
}
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextFillEllipseInRect(context, CGRectMake(xVal, yVal, 5, 5));
CGFloat white[4] = {1.0f, 1.0f, 1.0f, 1.0f};
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextBeginPath(context);
CGContextMoveToPoint(context, prevValX, prevValY);
CGContextAddLineToPoint(context, xVal, yVal);
CGContextStrokePath(context);
}
//----- AXIS
CGFloat white[4] = {1.0f, 1.0f, 1.0f, 1.0f};
CGContextSetStrokeColor(context, white);
CGContextBeginPath(context);
//Vert Line
CGContextMoveToPoint(context, 25, 25);
CGContextAddLineToPoint(context, 25, 275);
//Horiz Line
CGContextMoveToPoint(context, 25, 275);
CGContextAddLineToPoint(context, 275, 275);
CGContextStrokePath(context);
//----- END AXIS
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment