簡體   English   中英

使用Paul de Casteljau算法在目標C中創建貝塞爾曲線

[英]Create Bezier Curve in Objective C using Paul de Casteljau algorithm

我正在嘗試使用Paul de Casteljau算法為我的作業繪制Bezier曲線,但似乎並不完美,這是代碼

 - (void)recursive_bezier :(double)x1 :(double)y1 :(double)x2 :(double)y2 :(double)x3 :(double)y3 :(double)x4 :(double)y4 { count = count+1; // Calculate all the mid-points of the line segments //---------------------- double x12 = (x1 + x2) / 2; double y12 = (y1 + y2) / 2; double x23 = (x2 + x3) / 2; double y23 = (y2 + y3) / 2; double x34 = (x3 + x4) / 2; double y34 = (y3 + y4) / 2; double x123 = (x12 + x23) / 2; double y123 = (y12 + y23) / 2; double x234 = (x23 + x34) / 2; double y234 = (y23 + y34) / 2; double x1234 = (x123 + x234) / 2; double y1234 = (y123 + y234) / 2; if(isFlat) { // // Draw and stop // //---------------------- [self drawLine:x1 :y1 :x4 :y4]; } else { // Continue subdivision //---------------------- if (count == 5) { isFlat=true; } [self recursive_bezier:x1 :y1 :x12 :y12 :x123 :y123 :x1234 :y1234]; [self recursive_bezier:x1234 :y1234 :x234 :y234 :x34 :y34 :x4 :y4]; } } - (void)drawLine :(double)x1 :(double)y1 :(double)x4 :(double)y4{ countDraw = countDraw+1; NSLog(@"============%d============",countDraw); NSLog(@"x1 = %f y1 = %f",x1, y1); NSLog(@"x4 = %f y4 = %f",x4, y4); UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:CGPointMake(x1, y1)]; [path addLineToPoint:CGPointMake(x4, y4)]; CAShapeLayer *shapeLayer = [CAShapeLayer layer]; shapeLayer.path = [path CGPath]; shapeLayer.strokeColor = [[UIColor blueColor] CGColor]; shapeLayer.lineWidth = 2.0; shapeLayer.fillColor = [[UIColor clearColor] CGColor]; [self.view.layer addSublayer:shapeLayer]; } 

在此處輸入圖片說明

誰能幫我為什么不計算右側以及如何使曲線平滑?

ps:我得到了貝塞爾曲線算法的算法

問題在於,一旦將isFlat設置為true, isFlat細分細分,即使是在頂級也是如此。 這是因為它是與特定級別的遞歸調用相關的信息,而不是整個事物的全局信息。 如果逐步執行代碼,則應該更加清楚。

更好的方法是將count聲明為recursive_bezier的int參數,並在調用它時在頂層傳遞0,而在recursive_bezier調用時傳遞count + 1 您可以擺脫isFlat變量,僅測試count >= 5

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM