Draw a quadratic Bézier curve through three given points

前端 未结 3 939
抹茶落季
抹茶落季 2020-12-04 15:47

I have three points in 2D and I want to draw a quadratic Bézier curve passing through them. How do I calculate the middle control point (x1 and y1

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-04 16:19

    Let P0, P1, P2 be the control points, and Pc be your fixed point you want the curve to pass through.

    Then the Bezier curve is defined by

    P(t) = P0*t^2 + P1*2*t*(1-t) + P2*(1-t)^2
    

    ...where t goes from zero to 1.

    There are an infinite number of answers to your question, since it might pass through your point for any value of t... So just pick one, like t=0.5, and solve for P1:

    Pc = P0*.25 + P1*2*.25 + P2*.25
    
    P1 = (Pc - P0*.25 - P2*.25)/.5
    
       = 2*Pc - P0/2 - P2/2
    

    There the "P" values are (x,y) pairs, so just apply the equation once for x and once for y:

    x1 = 2*xc - x0/2 - x2/2
    y1 = 2*yc - y0/2 - y2/2
    

    ...where (xc,yc) is the point you want it to pass through, (x0,y0) is the start point, and (x2,y2) is the end point. This will give you a Bezier that passes through (xc,yc) at t=0.5.

提交回复
热议问题