2012-06-30 203 views
6

我想弄清楚如何在CoreGraphics中绘制圆弧。我明白在下面的场景中调用哪种方法以及如何计算角度。iOS CoreGraphics:绘制圆弧,从交叉弦定理确定圆弧角度

---------- 
|  | 
*--------* 

当点都在矩形的底部。但是当两点在其他位置时,我不知道如何计算正确的角度。

---------* 
|  | 
*--------- 

看到我的形象的底部。

enter image description here

雷Wenderlich有great tutorial大约只在第一次提到的点位置产生电弧的。

// sample code for creating arc for path from bottom of rect 
CGMutablePathRef createArcPathFromBottomOfRect(CGRect rect, CGFloat arcHeight) { 
    CGRect arcRect = CGRectMake(rect.origin.x, rect.origin.y + rect.size.height 
    - arcHeight, rect.size.width, arcHeight); 
    CGFloat arcRadius = (arcRect.size.height/2) + (pow(arcRect.size.width, 2)/
    (8 * arcRect.size.height)); 
    CGPoint arcCenter = CGPointMake(arcRect.origin.x + arc.size.width/2, 
    arcRect.origin.y + arcRadius); 
    CGFloat angle = acos(arcRect.size.width/ (2*arcRadius)); 
    CGFloat startAngle = radians(180) + angle; 
    CGFloat endAngle = radians(360) - angle; 
    CGMutablePathRef path = CGPathCreateMutable(); 
    CGPathAddArc(path, NULL, arcCenter.x, arcCenter.y, arcRadius, startAngle, 
    endAngle, 0); 
    return path; 
} 

如何计算在我的图像底部所示的其他情况下的角度?

回答

9

我找到一个更简单的方法,使弧是使用:

void CGContextAddArcToPoint (
    CGContextRef c, 
    CGFloat x1, 
    CGFloat y1, 
    CGFloat x2, 
    CGFloat y2, 
    CGFloat radius 
); 

如果你看一下从雷Wenderlich的网站(http://www.raywenderlich.com/2134/core-graphics-101-glossy-buttons/cgcontextaddarctopoint),点(X1,Y1)这个图像是为曲线的起点和点(x2,y2)是你的终点。然后只需指定拐角半径,瞧!看起来这可能是一个更简单的API,可用于您正在寻找的内容。

0

您至少需要3分才能确定一个圆。

在你的第一个参数中,两个点位于矩形的底部,当arcHeight被知道时,顶部中间点隐式地成为第三个点。因此三点确定了圆圈,从而确定了圆弧。所以所有角度等都可以计算出来。

然而,在你第二次塞纳里奥没有定义第三点。因此,您可以绘制通过具有不同曲率的两点的无限数量的弧。您需要额外的要求才能修复弧线。例如半径或第三个点应该是一个圆弧。