0

Okay, I know that this has been asked here a lot, but I've read through ~10 other questions exactly like this one, and none of them have provided me with any useful information. In fact, 3 of the answers that I found were straight up inaccurate!

Anyway, my question is very simple (for me it's confusing, but it's probably a walk in the park for you guys), how do I find value Y on a cubic Bézier, given value X?

I have been previously using a time property of sorts to iterate over my bezier, but that gives improper results. I found some people suggest that I should be solving for t given X, but I have no idea how I would go about doing that or if that's even the best solution.

Let's say I have the following cubic bezier: (0,1,0.5,0.5,0.5,0.5,1,0) If I use t to find a point along said bezier, I receive non-linear results when incrementing t linearly. This is a huge problem for me and one that has been stumping me for quite some time.

What I need to do is use X in this case, where I can increase X from 0 to 1 linearly, and use that to sample for a given Y position on my curve, and I believe that this will solve my problem.

Can anybody explain to me how this would be done? Currently the only values that I have to work with are the anchor points + the control points of my bezier, I have no other information about it.

1 Answers1

1

I think what you need is covered in the answers to this question, or here or here.

A 2D Bézier curve is controlled by four 2D control points, say $\mathbf{P}_0$, $\mathbf{P}_1$, $\mathbf{P}_2$, $\mathbf{P}_3$. Its equation is $$ \mathbf{P}(t) = (1-t)^3\mathbf{P}_0 + 3t(1-t)^2\mathbf{P}_1 + 3t^2(1-t)\mathbf{P}_2 + t^3\mathbf{P}_3 \tag{1}\label{eq1} $$ Writing $x$ and $y$ coordinates separately, the curve is $$ x(t) = (1-t)^3 x_0 + 3t(1-t)^2 x_1 + 3t^2(1-t) x_2 + t^3 x_3 \tag{2}\label{eq2} $$ $$ y(t) = (1-t)^3 y_0 + 3t(1-t)^2 y_1 + 3t^2(1-t) y_2 + t^3 y_3 \tag{3}\label{eq3} $$ where $\mathbf{P}_i = (x_i,y_i)$ for $i=0,1,2,3$.

So, suppose you have a given $x$ value, say $x=k$, and you want to calculate the corresponding $y$ value. The first problem is that there might be more than one corresponding $y$ value, but let’s ignore that issue, for now.

To get the $y$ value, you need two steps:

(A) Find $t$ such that $(1-t)^3 x_0 + 3t(1-t)^2 x_1 + 3t^2(1-t) x_2 + t^3 x_3 =k$.

(B) Substitute this $t$ value into equation (2) to find $y(t)$.

Step (A) is the difficult part, because you will need to solve a cubic equation to do this, which requires either nasty unstable formulas or numerical methods like Newton-Raphson.

bubba
  • 44,617
  • 1
    Another approach is to determine the degree $3$ polynomial relation between $X$ and $Y$. This is a nasty formula in general, but can be deduced from any $9$ different points on the curve. Once this is done, you easily get a cubic equation in $Y$ for each given $X$. Still requires some numerical approach to solve. – WimC Apr 13 '21 at 07:53
  • @WimC. I’d love to know the details. You’re saying that we can express y as a cubic function of x?? And I don’t understand the “9 different points” comment. AFAIK, you can only interpolate 6 points with a parametric cubic curve, and even that is not easy. – bubba Apr 13 '21 at 09:42
  • Or do you mean we derive an implicit equation $f(x,y)=0$ by eliminating $t$. – bubba Apr 13 '21 at 09:43
  • I guess that implicit equation has 10 coefficients, so we could make it interpolate 9 points. – bubba Apr 13 '21 at 09:53
  • Actually, there are very simple ways to get implicit equations of Bézier curves. See chapter 17 of these notes: https://scholarsarchive.byu.edu/facpub/1/ – bubba Apr 13 '21 at 09:55
  • Then, if we fix $x$ in the implicit equation, we have a cubic function of $y$ whose root(s) we can find by formula or numerical methods. I think I get it. Not clear that this approach has any great advantages, though ?? – bubba Apr 13 '21 at 09:59
  • You get the idea. In the end it still boils down to solving a cubic, so no improvement there. – WimC Apr 13 '21 at 10:42