我有一个三角形,我试图drawRect
在 a中绘制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];
}