如何获得任何曲线的点(上)

时间:2016-01-12 12:02:08

标签: android canvas draw

我想要这样的东西,如下图所示

enter image description here

我通过画布创建了一条曲线。但是现在我没有得到如何获得圆圈的坐标。如果它可以通过弧完成,那么解释我做了多少

enter image description here

我的尝试如下:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    PointF curvePoint = new PointF(580, 120); //curve point
    PointF mPoint2 = new PointF(60, 700); //Last point

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(4);
    paint.setColor(Color.GRAY);


    Path myPath1 = drawCurve(curvePoint, mPoint2);
    canvas.drawPath(myPath1, paint);

}

private Path drawCurve(PointF mPointa, PointF mPointb) {

    Path myPath = new Path();
    myPath.moveTo(600, 60);  // starting point

    myPath.quadTo(mPointa.x, mPointa.y, mPointb.x, mPointb.y);

    return myPath;
}

请帮忙!

1 个答案:

答案 0 :(得分:4)

在二次贝塞尔曲线上得到一个点。

private PointF getPointOnQuad(PointF p1, PointF p2, PointF p3, float p){    
        float x1 = (p2.x - p1.x) * p + p1.x;
        float y1 = (p2.y - p1.y) * p + p1.y;
        float x2 = (p3.x - p2.x) * p + p2.x;
        float y2 = (p3.y - p2.y) * p + p2.y;
        PointF point = new PointF((x2 - x1) * p + x1, (y2 - y1) * p + y1);        
        return point;
}

返回曲线p = 0 to 1p1p2p3的点。如果p = 0是曲线的起点,p = 0.5是中途,p = 1就是结束。点p1是开头,p2是控制点,p3是结束点。

让你的曲线得到中点,

PointF pointOnCurve = getPointOnQuad(new PointF(600,60), mPointa, mPointb, 0.5);