在UIView中绘制一个三角形

时间:2013-04-18 01:53:41

标签: iphone ios objective-c drawrect

我有一个三角形,我试图通过drawRect中的UIView绘制,除了当我尝试绘制超出界限的三角形时,一切似乎都起作用了的观点。我不认为我采取正确的方法,我目前正在尝试将三角形缩放到视图,而不是将视图缩放到三角形,但我不知道如何做得更好......任何帮助,这里是我目前的drawRect

- (void)drawRect:(CGRect)rect
{
IMTriangle *triangle = [self.dataSource triangle];
if (!triangle) return;
NSLog(@"incoming triangle %@", triangle);
IMTriangle *drawTri = [[IMTriangle alloc] init];
drawTri.shouldUseDegrees = NO;

CGRect bounds = [self bounds];

/*Find point A and B, 
 pointA is the lower left side, pointB is the lower right side*/
CGPoint pointA = CGPointMake(  bounds.origin.x + bounds.size.width * .05 , bounds.origin.y+bounds.size.height - bounds.size.height*.05  );
CGPoint pointB = CGPointMake(  bounds.origin.x + bounds.size.width * .95 , bounds.origin.y+bounds.size.height - bounds.size.height*.05  );

//find the distance between Point A and B, (sideC)
CGFloat c = pointB.x - pointA.x;
NSLog(@"side c display length = %f" , c);
NSLog(@"point A = %f, %f, point B = %f, %f", pointA.x, pointA.y, pointB.x ,pointB.y);

//use distance from of the new side C and the angles of the passed in triangle to find pointC
drawTri.sideC = c;
drawTri.angleA = triangle.angleA;
drawTri.angleB = triangle.angleB;
[drawTri solve];

NSLog(@"drawTri %@" , drawTri);
CGPoint pointC = CGPointMake( (pointA.x + (drawTri.sideB * cos(drawTri.angleA )) ) , (pointA.y - ([drawTri height])));

/*****************************************************************************************************/
//THIS SEEMS LIKE THE APROPRIATE POINT TO SCALE THE TRIANGLE (AGAIN) IF IT IT WILL EXCEED IT'S BOUNDS//
/*****************************************************************************************************/

if (pointC.x - pointB.x > drawTri.sideC) {
    //do something, 
}
else if (pointB.x - pointA.x > drawTri.sideC)
{
    //do something else. 
}


//Draw the triangle. 

//get the currant drawing context, 
CGContextRef ctx = UIGraphicsGetCurrentContext();

CGContextSetLineWidth(ctx, LINE_WIDTH);
[[UIColor blackColor] setStroke];

//plot it's path
CGContextMoveToPoint (ctx, pointA.x, pointA.y);
CGContextAddLineToPoint(ctx, pointB.x, pointB .y);

CGContextAddLineToPoint(ctx, pointC.x, pointC.y);
CGContextClosePath(ctx);
CGContextStrokePath(ctx);

[self setNeedsDisplay];

}

0 个答案:

没有答案