Quadratic Bézier Curve: Calculate Points

后端 未结 5 1576
别那么骄傲
别那么骄傲 2020-11-28 01:57

I\'d like to calculate a point on a quadratic curve. To use it with the canvas element of HTML5.

When I use the quadraticCurveTo() function in JavaScript, I have a s

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 02:34

    Use the quadratic Bézier formula, found, for instance, on the Wikipedia page for Bézier Curves:

    quadratic Bezier formula

    In pseudo-code, that's

    t = 0.5; // given example value
    x = (1 - t) * (1 - t) * p[0].x + 2 * (1 - t) * t * p[1].x + t * t * p[2].x;
    y = (1 - t) * (1 - t) * p[0].y + 2 * (1 - t) * t * p[1].y + t * t * p[2].y;
    

    p[0] is the start point, p[1] is the control point, and p[2] is the end point. t is the parameter, which goes from 0 to 1.

提交回复
热议问题