2016-12-01 27 views
2

我想根據控制點列表繪製曲線。 這是我所期待的: enter image description here如何根據3點以上的控制點列表繪製曲線

這裏是控制點: (0,90) (1100) (-3,145) (10150) (23155) (73108) (80,120) (86131) (40210) (50220) (60230) (148185) (140180) (131175) (23188) (0190)

這裏是我的代碼:

public List<PointType> controlPoints; 
public void render(Canvas canvas, Paint paint) { 
     int size = controlPoints.size(); 
     if (size < 2) { 
      return; 
     } 

     paint.setColor(this.color); 
     paint.setStyle(this.style); 

     Path curvePath = new Path(); 
     PointType firstPoint = null; 
     PointType beginPoint = null; 

     for (PointType point : controlPoints) { 
      if (firstPoint == null) { 
       firstPoint = point; 
      } else if (beginPoint == null) { 
       beginPoint = point; 
      } else { 
       curvePath.moveTo(firstPoint.x, firstPoint.y); 
       curvePath.quadTo(beginPoint.x, beginPoint.y, point.x, point.y); 
       firstPoint = beginPoint; 
       beginPoint = point; 
      } 
     } 

     canvas.drawPath(curvePath, paint); 
    } 

但結果是這樣的:

enter image description here

什麼是錯的,我怎麼能得出正確的曲線?

+0

看到http://stackoverflow.com/a/3813349/794088 – petey

+0

你需要平滑的曲線出來,用線總是會接近它類似的東西。看看貝塞爾樣條作爲第一次嘗試 –

+0

https://github.com/autotrace也許你可以從這個鏈接拿東西..看起來你需要更多的點,因爲它畫直線... – Setar

回答

0

我已經解決由下面的代碼的問題:

public void render(Canvas canvas, Paint paint) { 
     int size = controlPoints.size(); 
     if (size < 2) { 
      return; 
     } 

     paint.setColor(this.color); 
     paint.setStyle(this.style); 

     Path curvePath = new Path(); 
     curvePath.moveTo(controlPoints.get(0).x, controlPoints.get(0).y); 
     for (int idx = 1; idx < controlPoints.size(); idx += 3) { 
      curvePath.cubicTo(controlPoints.get(idx).x, 
        controlPoints.get(idx).y, controlPoints.get(idx+1).x, 
        controlPoints.get(idx+1).y, controlPoints.get(idx+2).x, 
        controlPoints.get(idx+2).y); 
     } 

     canvas.drawPath(curvePath, paint); 
    } 
相關問題