Dienstag, 1. April 2014

I asked myself, is it the only way to compute the distance from a point to a quadratic bezier curve on complex formula?
Why don´t we solve the nearest distance in a recursion?

public Float getNearestPoint(float dt) {

d = dist(pos(ct).x, pos(ct).y, distP.x, distP.y);

while ((next = dist(pos(ct + dt).x, pos(ct + dt).y, distP.x, distP.y)) < d
&& (ct + dt) < 1) {
    d = next;
    ct += dt;
Float nearP = pos(ct);

if (res-- < 1) {
    res = startres;
    ct = .0f;
    return nearP;

 if (ct - dt > 0)
ct -= dt;
return getNearestPoint(dt /= 2);


ct = the current t-value of the bezier

dt = the divided t-value to aproxximate 

The pos() method gives the curve-position by the t-value. Of course,  it´s expensive, but what is wrong with a recursive approximation to the next point?
I  read some codes about this stuff and all of them had up to 100 operations to get the distance, maybe this code get some more. But if you want, please comment what´s your opinion to do it this way?

With the result, I´m satisfied. I can calculate the distance with a simple code.
For this purpose, I first created a simple JFrame, with which you can check the results.
It has been shown that in extreme bends, the wrong t-value is calculated.
Normally, at lower curves this error does not occur.