Created
August 30, 2011 22:08
-
-
Save projectxcappe/1182236 to your computer and use it in GitHub Desktop.
with regression
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GOCustomView.m | |
// CustomViewExample | |
// | |
// Created by Cassidy Pangell on 8/26/11. | |
// Copyright (c) 2011 __MyCompanyName__. All rights reserved. | |
// | |
#import "CPCustomView.h" | |
#include "math.h" | |
#define LENGTH 248 | |
#define HEIGHT 250 | |
@implementation CPCustomView | |
@synthesize bgColor = _bgColor; | |
@synthesize MVO_U_Points = _MVO_U_Points; | |
@synthesize MVO_D_Points = _MVO_D_Points; | |
@synthesize TCP_U_Points = _TCP_U_Points; | |
@synthesize TCP_D_Points = _TCP_D_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)regression{ | |
} | |
-(void) graphPoints:(NSMutableArray *)nsArray{ | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
CGContextSetLineWidth(context, 2.0); | |
NSUInteger count = [nsArray count]; | |
//NSLog(@"COUNT %i",count); | |
CGFloat prevValX; | |
CGFloat prevValY; | |
CGFloat currValX; | |
CGFloat currValY; | |
CGFloat firstValX; | |
CGFloat deltaX = LENGTH / (count - 1); | |
CGFloat changeDeltaX; | |
NSValue runningAvg; | |
CGFloat sumXYN = 0.0; | |
CGFloat sumX = 0.0; | |
CGFloat sumY = 0.0; | |
CGFloat sumXsqN = 0.0; | |
CGFloat sumXsq = 0.0; | |
CGFloat slope; //b | |
CGFloat intercept; //a | |
CGFloat regPointOne; // a + bx => x being the point | |
CGFloat regPointTwo; | |
//allocate data points for graph | |
for(int i=1; i<count; i++){ | |
changeDeltaX = deltaX; | |
// NSLog(@"DELTA %f", deltaX); | |
NSValue *currentPointValue = [nsArray objectAtIndex:i]; | |
CGPoint currentPoint = [currentPointValue CGPointValue]; | |
NSValue *previousPointValue = [nsArray objectAtIndex:i-1]; | |
CGPoint previousPoint = [previousPointValue CGPointValue]; | |
NSValue *firstPointValue = [nsArray objectAtIndex:0]; | |
CGPoint firstPoint = [firstPointValue CGPointValue]; | |
while (i<15) { | |
runningAvg += currentPoint.y / count; | |
if(i == 14) { | |
[nsArray replaceObjectAtIndex:0 withObject:runningAvg]; | |
} | |
} | |
if((deltaX - changeDeltaX) != 0) | |
{ | |
NSLog(@"change"); | |
// [nsArray replaceObjectAtIndex:i withObject:x]; | |
} | |
//NSLog(@"RT %f", runningAvg); | |
currValX = (currentPoint.x + (deltaX * (i))); | |
currValY = 250 - (currentPoint.y); | |
NSLog(@"currValY %f", currValY); | |
prevValX = (previousPoint.x + ((deltaX) * (i-1))); | |
prevValY = 250 - (previousPoint.y); | |
firstValX = firstPoint.y; | |
//NSLog(@"FIRST %f", firstValX); | |
//Calculate regression line for points | |
sumX += currValX; | |
sumY += currValY; | |
sumXYN += (currValX * currValY) * count; | |
sumXsqN += (currValX * currValX) * count; | |
sumXsq += sumX * sumX; | |
slope = (sumXYN - (sumX)*(sumY))/(sumXsqN - (sumX)); //b | |
intercept = (sumY - (slope * (sumX))/count); //a | |
regPointOne = intercept + slope * firstValX; | |
regPointTwo = intercept + slope * currValY; | |
NSLog(@"pt1 %f pt2 %f", regPointOne, regPointTwo); | |
//Draw Points (better way to do this?) | |
if (nsArray == _MVO_D_Points) { | |
CGContextSetFillColorWithColor(context, [UIColor yellowColor].CGColor); | |
//CGContextFillEllipseInRect(context, CGRectMake(regPointOne-1.5, regPointTwo-1.5, 3, 3)); | |
CGContextFillEllipseInRect(context, CGRectMake(currValX-1.5, currValY-1.5, 3, 3)); | |
CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor); | |
}else if (nsArray == _MVO_U_Points){ | |
CGContextSetFillColorWithColor(context, [UIColor orangeColor].CGColor); | |
CGContextFillEllipseInRect(context, CGRectMake(currValX-1.5, currValY-1.5, 3, 3)); | |
CGContextSetStrokeColorWithColor(context, [UIColor orangeColor].CGColor); | |
}else if (nsArray == _TCP_D_Points){ | |
CGContextSetFillColorWithColor(context, [UIColor purpleColor].CGColor); | |
CGContextFillEllipseInRect(context, CGRectMake(currValX-1.5, currValY-1.5, 3, 3)); | |
CGContextSetStrokeColorWithColor(context, [UIColor purpleColor].CGColor); | |
}else{ //_TCP_U_Point | |
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); | |
CGContextFillEllipseInRect(context, CGRectMake(currValX-1.5, currValY-1.5, 3, 3)); | |
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); | |
} | |
CGContextBeginPath(context); | |
CGContextMoveToPoint(context, prevValX, prevValY); | |
//CGContextAddCurveToPoint(context, prevValX, prevValY, currValX, currValY, currValX, currValY); | |
CGContextAddLineToPoint(context, currValX, currValY); | |
CGContextSetLineJoin(context, kCGLineJoinRound); | |
CGContextStrokePath(context); | |
} | |
//----- AXIS | |
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor); | |
CGContextBeginPath(context); | |
//Vert Line | |
CGContextMoveToPoint(context, 0, 0); | |
CGContextAddLineToPoint(context, 0, 300); | |
//Horiz Line | |
CGContextMoveToPoint(context, 0, 300); | |
CGContextAddLineToPoint(context, 300, 300); | |
CGContextStrokePath(context); | |
//----- END AXIS | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment