2012-06-30 123 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被知道時,頂部中間點隱式地成爲第三個點。因此三點確定了圓圈,從而確定了圓弧。所以所有角度等都可以計算出來。

然而,在你第二次塞納里奧沒有定義第三點。因此,您可以繪製通過具有不同曲率的兩點的無限數量的弧。您需要額外的要求才能修復弧線。例如半徑或第三個點應該是一個圓弧。