Created
January 22, 2013 07:43
-
-
Save HungHuynh/4592820 to your computer and use it in GitHub Desktop.
draw line, circle, triangle
This file contains 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
#pragma mark - Draw Oxyz | |
-(void)drawOxyz:(CGPoint)_pointO withX:(CGPoint)_pointX withY:(CGPoint)_pointY withZ:(CGPoint)_pointZ withColor:(BOOL)isRed{ | |
[self drawLine:CGPointMake(_pointO.x + PADDLE, _pointO.y) with:_pointX withRed:isRed]; | |
[self drawLine:CGPointMake(_pointO.x , _pointO.y - PADDLE) with:_pointY withRed:isRed]; | |
[self drawLine:CGPointMake(_pointO.x + PADDLE/2, _pointO.y - PADDLE/2 ) with:_pointZ withRed:isRed]; | |
} | |
#pragma mark - Draw Line | |
-(void)drawLine:(CGPoint)_pointStart with:(CGPoint)pointEnd withRed:(BOOL)redColor { | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
// Draw them with a 2.0 stroke width so they are a bit more visible. | |
CGContextSetLineWidth(context, 3.5); | |
if (redColor) { | |
CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 1); | |
} | |
else | |
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 1.0, 1); | |
CGContextMoveToPoint(context, _pointStart.x, _pointStart.y); | |
CGContextAddLineToPoint( context, pointEnd.x, pointEnd.y); | |
CGContextStrokePath(context); | |
} | |
#pragma mark - Draw Circle | |
-(void)drawCircleWithFrame:(CGRect)framwCircle andColor:(CGColorRef)color{ | |
CGContextRef context= UIGraphicsGetCurrentContext(); | |
CGContextSetLineWidth(context, 2.0); | |
CGContextSetFillColorWithColor(context, color); | |
CGContextSetAlpha(context, 1.0); | |
//contend | |
CGContextFillEllipseInRect(context,framwCircle); | |
//bound | |
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor); | |
CGContextStrokeEllipseInRect(context,framwCircle); | |
} | |
#pragma mark - Draw Triangle | |
-(void)programWithA:(CGPoint)A B:(CGPoint)B WithLbl:(UILabel*)lblText withColor:(BOOL)isRed { | |
CGPoint C,D; | |
int value = abs(A.y - B.y); | |
if (value != 0) { | |
//Get : x = dy + e | |
CGFloat d = [self getConstand:A :B].x; | |
CGFloat e = [self getConstand:A :B].y; | |
//Get aX2 + bX + c = 0 | |
CGFloat a = 1 + d*d; | |
CGFloat b = (d*e - B.y - B.x*d)*2; | |
CGFloat c = B.y*B.y + B.x*B.x + e*e - 2*B.x*e - PADDLE_POINT; | |
CGFloat delta = b*b - 4*a*c ; | |
if (delta == 0.0 || delta < 0.0 ) { | |
C = CGPointMake(B.x - sqrt(PADDLE_POINT), B.y); | |
D = CGPointMake(B.x + sqrt(PADDLE_POINT), B.y); | |
}else { | |
C = CGPointMake(d*((-b+sqrt(delta))/(2*a)) + e,(-b+sqrt(delta))/(2*a)); | |
D = CGPointMake(d*((-b-sqrt(delta))/(2*a)) + e, (-b-sqrt(delta))/(2*a)); | |
if (abs(C.y - D.y) == 0) { | |
C = CGPointMake(B.x - sqrt(PADDLE_POINT), C.y); | |
D = CGPointMake(B.x + sqrt(PADDLE_POINT), D.y); | |
} | |
} | |
} | |
else { | |
C = CGPointMake(B.x - sqrt(PADDLE_POINT), B.y); | |
D = CGPointMake(B.x + sqrt(PADDLE_POINT), B.y); | |
} | |
if ([self distandPoit:A :B] > [self distandPoit:A :C]) { | |
[self drawTriangle:D :C withColor:isRed]; | |
[lblText setFrame:CGRectMake(D.x, D.y, lblText.frame.size.width, lblText.frame.size.height)]; | |
} | |
else { | |
[self drawTriangle:C :D withColor:isRed]; | |
[lblText setFrame:CGRectMake(C.x, C.y, lblText.frame.size.width, lblText.frame.size.height)]; | |
} | |
} | |
-(CGPoint)getConstand:(CGPoint)pointA :(CGPoint)pointB { | |
return CGPointMake((pointA.x - pointB.x)/(pointA.y - pointB.y), (pointA.x*(pointA.y - pointB.y) - pointA.y*(pointA.x - pointB.x))/(pointA.y - pointB.y)); | |
} | |
-(void)drawTriangle:(CGPoint)pointA :(CGPoint)pointB withColor:(BOOL)isRed { | |
CGPoint pointC = CGPointMake((pointB.x + (pointA.y - pointB.y)/2),( pointB.y + (pointB.x - pointA.x)/2)); | |
CGPoint pointD = CGPointMake(pointB.x + (pointB.y - pointA.y)/2, pointB.y + (pointA.x - pointB.x)/2); | |
[self drawTrianglePointA:pointA B:pointC C:pointD andRed:isRed]; | |
} | |
-(void)drawTrianglePointA:(CGPoint)pointA B:(CGPoint)pointB C:(CGPoint)pointC andRed:(BOOL)isRed{ | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
CGContextBeginPath(context); | |
CGContextMoveToPoint(context, pointA.x, pointA.y); | |
CGContextAddLineToPoint( context, pointB.x, pointB.y); | |
CGContextAddLineToPoint( context, pointC.x, pointC.y); | |
CGContextClosePath(context); | |
if (isRed) { | |
CGContextSetRGBFillColor(context, 1, 0, 0, 1); | |
} | |
else | |
CGContextSetRGBFillColor(context, 0, 0, 1, 1); | |
CGContextFillPath(context); | |
} | |
#pragma mark - Distand Point to Point | |
-(float)distandPoit:(CGPoint)_pointA :(CGPoint)_pointB { | |
return sqrtf(( powf((_pointA.x - _pointB.x), 2) + powf((_pointA.y - _pointB.y), 2))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment