2016-12-01 58 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); 
    }