【问题标题】:How to draw a curve according to a control points list that has more than 3 points如何根据超过 3 个点的控制点列表绘制曲线
【发布时间】:2016-12-05 21:55:17
【问题描述】:

我想根据控制点列表绘制曲线。 这是我所期望的:

以下是控制点: (0,90) (1,100) (-3,145) (10,150) (23,155) (73,108) (80,120) (86,131) (40,210) (50,220) (60,230) (148,185) (140,180) (131,175) (23,188) (0,190)

这是我的代码:

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);
    }

但是结果是这样的:

出了什么问题,我怎样才能画出正确的曲线?

【问题讨论】:

  • 你需要平滑曲线,用线条近似它总是看起来像这样。第一次尝试贝塞尔样条线
  • github.com/autotrace 也许你可以从这个链接中获取一些东西......看起来你需要更多的点,因为它画直线......

标签: android


【解决方案1】:

我已经通过以下代码解决了这个问题:

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);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多