0

I want to get the parametric distance (the "$t$" value) at a location on a quadratic Bezier curve, given the "$x$" and "$y$" coordinates of the point.

I have start point, end point and control point of the curve.

How can I get it? Is there any direct formula to get the "$t$" value?

Please advice.

bubba
  • 43,483
  • 3
  • 61
  • 122
vaidya
  • 21
  • Do you mean a Bezier arc ? –  Aug 07 '14 at 12:34
  • What you are asking about (probably) is a process called "inversion". You are given the $(x,y)$ coordinates of a point on a quadratic Bezier curve, and you want to find the corresponding parameter (the "$t"$ value). This answer provides several ways to solve this problem for cubic curves http://math.stackexchange.com/questions/841169/fastest-way-to-obtain-the-parametric-value-t-of-a-bezier-curve-for-a-given-set/843286#843286. Quadratic ones are simpler, of course. – bubba Aug 09 '14 at 08:43
  • I edited the question. Please check that I didn't change the meaning. – bubba Aug 10 '14 at 05:56
  • Suppose the control points are $\mathbf{a}$, $\mathbf{b}$, $\mathbf{c}$, as in the answer below. For the given point $\mathbf{q}$, you can obtain barycentric coordinates in the triangle $\mathbf{abc}$. Specifically, you can find $u$, $v$, $w$ such that $\mathbf{q} = u\mathbf{a} + v\mathbf{b} + w\mathbf{c}$. Then the "$t$" value of the point $\mathbf{q}$ is just $\sqrt{v}$. I'll give more details if the question is re-opened. – bubba Aug 10 '14 at 06:05
  • @bubba Pinging to notify you that the question is reopened. If you still feel like giving more details, the way is free now. – Daniel Fischer Aug 18 '14 at 08:58
  • @DanielFischer -- Thanks for the ping. Very nice of you. First shot at an answer is given below. More later. Does the OP automatically get notified about the re-opening, somehow? – bubba Aug 18 '14 at 11:09

3 Answers3

2

Assuming you want to compute the shortest distance between a point $\vec P$ and a quadratic Bezier arc $\vec Q(1-t)^2+2t(1-t)\vec R+t^2\vec S$, $0<t<1$, you need to minimize $$\delta^2(t)=\min_{t,0<t<1}\left(\vec{PQ}(1-t)^2+2t(1-t)\vec{PR}+t^2\vec{PS}\right)^2.$$ Deriving with respect to $t$, you have $$\frac{d\delta^2(t)}{dt}=4\left(\vec{PQ}(t-1)+(1-2t)\vec{PR}+t\vec{PS}\right)\cdot\left(\vec{PQ}(1-t)^2+2t(1-t)\vec{PR}+t^2\vec{PS}\right).$$ Expanding, you will find a third degree polynomial that has one or three real roots. Keep the roots in $(0,1)$, compute the corresponding distances, and find the smallest among them, plus the distances to the endpoints, $\vec{PQ}^2$ and $\vec{PS}^2$.

  • 1
    In floating -point arithmetic, the given point will never lie exactly on the curve. So, instead of finding the $t$ value of the given point, it makes more sense to find the $t$ value of the closest point on the curve, as explained in this answer. – bubba Aug 10 '14 at 08:13
2

Please see this answer. It covers the case of a cubic Bezier curve, but quadratic curves are even simpler, of course.

As that answer states, this "inversion" problem is covered thoroughly in these notes by Tom Sederberg. He provides techniques for "inversion" of polynomial and rational Bezier curves, based on the use of resultants.

Specifically, on page 200, you will find a formula for doing "inversion" of a quadratic curve written in Bezier form -- equation (17.5). If you look closely at the formulae for his quantities $l_{ij}$, you will see that they represent areas of triangles. This is to be expected, in view of my comment about barycentric coordinates above.

For both quadratic and cubic curves, the inversion equation is linear, so it just involves simple algebraic operations -- no quadratic equation solving, and no square roots.

Suppose we have a quadratic Bezier curve with control points $\mathbf{P}_i = (x_i, y_i, z_i)$, where $i=0,1,2$, and we have a given point $\mathbf{P} = (x, y, z)$. Then, using Sederberg's techniques, the "$t$" value corresponding to the point $\mathbf{P}$ can be calculated using the following simple code:

 c10 = (x - x1)*(y - y0) - (x - x0)*(y - y1)
 c20 = (x - x2)*(y - y0) - (x - x0)*(y - y2)
 t = c10/(c10 - 0.5*c20)

Here we use the symbols $c_{ij}$ instead of the $l_{ij}$ in Sederberg's notes, because the letter "l" causes confusion in code. Note that the computation requires nothing more than a few subtractions and multiplications, and one division.

bubba
  • 43,483
  • 3
  • 61
  • 122
1

Bezier curves of a particular order have a closed form based on their control points. In the case of quadratics, that form is: $p = (1-t)^2 \cdot a + 2t(1-t) \cdot b + t^2 \cdot c$, where a, b, and c are the control points in order. Given the x value, you can solve $p_x = (1-t)^2 \cdot a_x + 2t(1-t) \cdot b_x + t^2 \cdot c_x$ for t. You can also do this for y; if the t values match up, then the point is on the quadratic curve.

Note that you can get 0, 1, or 2 values for t from each coordinate, most likely 2. This is perfectly okay: remember that from most directions, you will cross parabolas twice. Simply choose the t value that's in both.

Dan Uznanski
  • 11,025