Wikipedia:Reference desk/Archives/Mathematics/2024 February 2

= February 2 =

Draw a segment of a cubic function exactly using a cubic Bezier curve
http://en.wikipedia.org/wiki/B%C3%A9zier_curve#Second-order_curve_is_a_parabolic_segment describes how a segment of a parabola or quadratic curve on a Cartesian plane can be drawn exactly with a quadratic Bezier curve.

Is it possible to draw a segment of a cubic curve exactly with one (preferably) or more cubic Bezier curves?

I.e. given f(x) = ax³ + bx² + cx + d, and endpoints (p, f(p)) and (q, f(q)), what are the coordinates of the two control points?

Thanks, cm&#610;&#671;ee&#9094;&#964;a&#671;&#954; 15:18, 2 February 2024 (UTC)


 * With a cubic Bezier curve $$P(t) = (1-t)^{3}P_{0}+3(1-t)^{2}tP_{1} + 3(1-t)t^{2}P_{2} + t^{3}P_{3}$$, in order for you to be able to represent $$f(x) = ax^{3} + bx^{2} + cx + d$$, you need to have it so that $$f(P_{x}(t)) = P_{y}(t)$$ for all $$t$$. I don't specify a domain on $$t$$ here but regardless of the domain we can just assume that in order to do so, we need for the coefficients of $$f(P_{x}(t))$$ to match those of $$P_{y}(t)$$. We have to solve for four unknowns here, which are the coordinates of $$P_{1} = (x_{1}, y_{1})$$ and $$P_{2} = (x_{2}, y_{2})$$, since $$P_{0} = (p, f(p))$$ and $$P_{3} = (q, f(q))$$.
 * If you expand $$P_{x}(t)$$ into a third-order polynomial, you can notice that $$f(P_{x}(t)) = aP_{x}(t)^{3} + bP_{x}(t)^{2} + cP_{x}(t) + d$$ is a ninth-order polynomial, while $$P_{y}(t)$$ is third-order. The coefficient of the ninth-order term is just that of the third-order term of $$P_{x}(t)$$ cubed, which means that said third-order term must be $$0$$ in order to match coefficients. In other words, $$P_{x}(t)$$ is at most quadratic. When this is the case, $$f(P_{x}(t)) = aP_{x}(t)^{3} + bP_{x}(t)^{2} + cP_{x}(t) + d$$ is still sixth-order, with the sixth-order term being the second-order term of $$P_{x}(t)$$ cubed. Again, to match coefficients, this means that the second order term is also $$0$$, and $$P_{x}(t)$$ is at most linear. This time, coefficient matching works, and you can get that $$x_{1} = 2p/3 + q/3$$ and $$x_{2} = p/3 + 2q/3$$. In other words, the control points would be equidistantly spaced along the x-axis, which isn't particularly surprising. $$P_{x}(t)$$ is naturally equal to $$p + (q-p)t$$.
 * The next part is finding values of $$y_{1}$$ and $$y_{2}$$. I don't know of any tricks here, I just found the values of $$y_{1}$$ and $$y_{2}$$ manually. After expanding $$f(p+(q-p)t)$$ and $$P_{y}(t)$$, through direct comparison I found the values
 * $$y_{1} = ap^{2}q + b(p^{2}/3 + 2pq/3) + c(2p/3 + q/3) + d$$
 * $$y_{2} = apq^{2} + b(q^{2}/3 + 2pq/3) + c(p/3 + 2q/3) + d$$
 * Or in other words, the two control points are:
 * $$(\frac{2p+q}{3}, ap^{2}q+b(\frac{p^{2}+2pq}{3})+c(\frac{2p+q}{3})+d)$$, $$(\frac{p+2q}{3}, apq^{2} + b(\frac{q^{2} + 2pq}{3}) + c(\frac{p+2q}{3}) + d)$$
 * GalacticShoe (talk) 00:49, 3 February 2024 (UTC)


 * Thank you so much, that's exactly what I needed. Cheers, cm&#610;&#671;ee&#9094;&#964;a&#671;&#954; 07:56, 3 February 2024 (UTC)
 * Glad I could be of help :) GalacticShoe (talk) 08:00, 3 February 2024 (UTC)


 * Here's a somewhat different take on the problem giving an equivalent result: Write
 * $$F(t)=A(1-t)^3+3Bt(1-t)^2+3Ct^2(1-t)+Dt^3.$$
 * Then:
 * $$F'(t)=-3A(1-t)^2+3B(1-t)^2-6Bt(1-t)+6Ct(1-t)-3Ct^2+3Dt^2,$$
 * $$F''(t)=6A(1-t)-12B(1-t)+6Bt-12Ct+6C(1-t)+6Dt,$$
 * $$F'''(t)=-6A+18B-18C+6D.$$
 * Evaluate at t=0 to get
 * $$F(0)=A,$$
 * $$F'(0)=-3A+3B,$$
 * $$F''(0)=6A-12B+6C,$$
 * $$F'''(0)=-6A+18B-18C+6D.$$
 * You can easily solve for A, B, C, D in terms of F and its derivatives at 0:
 * $$A=F(0),$$
 * $$B=F(0)+\frac{1}{3}F'(0),$$
 * $$C=F(0)+\frac{2}{3}F'(0)+\frac{1}{6}F''(0),$$
 * $$D=F(0)+F'(0)+\frac{1}{2}F(0)+\frac{1}{6}F'(0)$$
 * Note that the last expression is the third order Maclaurin approximation for F(1), and this is exact since F is a cubic polynomial. If
 * $$(x, y) = (F_x(t), F_y(t))$$
 * is any cubic parametric curve then these formulas generate the endpoints and control points from t=0 to t=1. In particular, if the curve is y=f(x) from x=P to x=Q, where f is a cubic polynomial, take
 * $$F_x(t) = P(1-t)+Qt, F_y(t) = f(x)= f(P(1-t)+Qt).$$
 * Then:
 * $$F_x(0) = P,$$
 * $$F_x'(0) = (Q-P),$$
 * $$F_x(0) = F_x'(0) = 0,$$
 * $$F_y(0) = f(P),$$
 * $$F_y'(0) = (Q-P)f'(P),$$
 * $$F_y(0) = (Q-P)^2f(P),$$
 * $$F_y(0) = (Q-P)^3f(P).$$
 * This gives the endpoints and control points as:
 * $$(P, f(P)),$$
 * $$(\frac{2P+Q}{3}, f(P)+\frac{(Q-P)}{3}f'(P)),$$
 * $$(\frac{P+2Q}{3},f(P)+\frac{2(Q-P)}{3}f'(P)+\frac{(Q-P)^2}{6}f''(P)),$$
 * $$(Q, f(P)+(Q-P)f'(P)+\frac{(Q-P)^2}{2}f(P)+\frac{(Q-P)^3}{6}f'(P)).$$
 * The expression for the y-coordinate at Q is the Taylor series approximation of f(Q), and it's exact since f is a cubic polynomial. I don't think it would be hard to generalize these formulas to nth order Bezier curves, and you might say that the expressions for the y coordinates are actually generalizations of Taylor appromations since the last one is, in fact, the actual Taylor approximation. Since the formulas use derivatives instead of coefficients, you can use them to generate accurate Bezier approximations for any parametric curve of sufficient smoothness. --RDBury (talk) 11:08, 4 February 2024 (UTC)
 * Thank you very much for the general solution, cm&#610;&#671;ee&#9094;&#964;a&#671;&#954; 16:37, 4 February 2024 (UTC)