2016-09-19 55 views
1

我正在使用CoreGraphics繪製一個圓角矩形,我熟悉用CG API準備繪製圓角矩形,但我不想使用它,因爲矩形不是完全一致的圓角矩形,它的一部分將是一個圓角矩形,其他部分將是一組連接的路徑,例如,左上角和右上角將是一個圓角矩形邊,但底邊是一組連接的貝塞爾路徑。計算addCurveToPoint控制點

我的問題是,如果我想繪製整個形狀爲貝塞爾路徑,我應該如何計算addCurveToPoint中的控制點的角落?我知道點的半徑和座標(也基於半徑)。

(UPDATE)

I have a sample code, I am trying to understand the math behind it: 

UIBezierPath * rectangle = [UIBezierPath bezierPath]; 
[rectangle moveToPoint:CGPointMake(0, 8)]; 
[rectangle addCurveToPoint:CGPointMake(8.01, 0) controlPoint1:CGPointMake(0, 3.58) controlPoint2:CGPointMake(3.59, 0)]; 
[rectangle addLineToPoint:CGPointMake(208, 0)]; 
[rectangle addCurveToPoint:CGPointMake(224, 16.01) controlPoint1:CGPointMake(216.84, 0) controlPoint2:CGPointMake(224, 7.16)]; 
[rectangle addLineToPoint:CGPointMake(224, 175)]; 
[rectangle addCurveToPoint:CGPointMake(192, 207) controlPoint1:CGPointMake(224, 192.67) controlPoint2:CGPointMake(209.67, 207)]; 
[rectangle addLineToPoint:CGPointMake(64, 207)]; 
[rectangle addCurveToPoint:CGPointMake(0, 142.99) controlPoint1:CGPointMake(28.65, 207) controlPoint2:CGPointMake(0, 178.35)]; 
[rectangle addLineToPoint:CGPointMake(0, 8)]; 
[rectangle closePath]; 

拐角半徑是8,16,32和64爲左上,右上,右下和左下

由於

回答

1

我假設你想爲圓角增加一個90度弧。使用addArc而不是addCurveToPoint

在斯威夫特3:

var path = UIBezierPath() 

... 

let center = CGPoint(x: topLeft.x + width - radius, y: topLeft.y) 
path.addArc(withCenter: center, radius: radius, startAngle: CGFloat.pi, endAngle: CGFloat.pi * CGFloat(1.5), clockwise: true) 

當然,你的參數會有所不同。

更新

基於您的代碼,它應該是這樣的:

UIBezierPath * rectangle = [UIBezierPath bezierPath]; 
[rectangle moveToPoint:CGPointMake(0, 8)]; 
[rectangle addArcWithCenter:CGPointMake(8, 8) radius:8 startAngle:M_PI endAngle:M_PI*1.5 clockwise:YES]; 
[rectangle addLineToPoint:CGPointMake(208, 0)]; 
[rectangle addArcWithCenter:CGPointMake(208, 16) radius:16 startAngle:M_PI*1.5 endAngle:0 clockwise:YES]; 
[rectangle addLineToPoint:CGPointMake(224, 175)]; 
[rectangle addArcWithCenter:CGPointMake(208, 175) radius:32 startAngle:0 endAngle:M_PI*0.5 clockwise:YES]; 
[rectangle addLineToPoint:CGPointMake(64, 207)]; 
[rectangle addArcWithCenter:CGPointMake(64, 175) radius:64 startAngle:M_PI*0.5 endAngle:M_PI clockwise:YES]; 
[rectangle closePath]; 
+0

謝謝,我已經有我想要的示例代碼,但我不知道它背後的數學: – Nader

+0

@Nader看到我上面的更新...'center'是弧的中心,即它從矩形拐角偏移半徑。如果您有興趣瞭解弧如何近似爲貝塞爾曲線,請參見[使用立方貝塞爾曲線逼近圓弧](http://hansmuller-flex.blogspot.ch/2011/04/approximating-circular-arc-與-cubic.html)。 – Codo

+0

謝謝!這真的工作,但我不得不稍微修改它:) – Nader

0

基於@Codo答案,這是最終的解決方案:

UIBezierPath * rectangle = [UIBezierPath bezierPath]; 
[rectangle moveToPoint:CGPointMake(0, 8)]; 
[rectangle addArcWithCenter:CGPointMake(8, 8) radius:8 startAngle:M_PI endAngle:M_PI*1.5 clockwise:YES]; 
[rectangle addLineToPoint:CGPointMake(208, 0)]; 
[rectangle addArcWithCenter:CGPointMake(208, 16) radius:16 startAngle:M_PI*1.5 endAngle:0 clockwise:YES]; 
[rectangle addLineToPoint:CGPointMake(224, 175)]; 
[rectangle addArcWithCenter:CGPointMake(192, 175) radius:32 startAngle:0 endAngle:M_PI*0.5 clockwise:YES]; 
[rectangle addLineToPoint:CGPointMake(64, 207)]; 
[rectangle addArcWithCenter:CGPointMake(64, 143) radius:64 startAngle:M_PI*0.5 endAngle:M_PI clockwise:YES]; 
[rectangle addLineToPoint:CGPointMake(0, 8)]; 
[rectangle closePath]; 
+0

我還沒有發現你和我的代碼之間的區別呢。你可以幫我嗎? – Codo