# How do I get distance between two curves that cannot be described with a polynomial?

3 views (last 30 days)
Jacinth Gudetti on 15 Mar 2022
Commented: Sam Chak on 19 Mar 2022
Hi guys so I have a unique problem. I'm comparing the distance between two path planning algorithms and these are the results I get in the MATLAB figure. I want to compare the distance between the two paths and output a single graph that has the offset of the distance between the curves, but as you can see, I cannot really use fit with smoothingspline ("fit(X,Y,"smoothingspline")") because both plots cannot be described in a single equation, trust me, I have tried.
I also tried using movmean to average the functions but that made the two graphs fall apart even further sicne the X and Y values don't exactly line up. I figure I will need to use a 3rd dimension like time to orient it but I would like to avoid doing that. My last resort could be running computer vision to get a white space between the two and grab the area between them but that is not a very exact analysis.
Robert U on 16 Mar 2022
If it is just about the total length travelled, I do not think there is a need for generalization. You can assume that the way travelled between coordinates is piecewise linear and add all the linear way elements.
If in any case that would be an estimation that is too rough, you can choose other trajectory assumptions ending up in polynomials that must be described by transition conditions for smooth curves (i doubt that is really necessary) and add a lot of complexity to the trajectory that usually does not lead to a much improved accuracy.
BTW: You did not answer any other question concerning the task you would like to tackle.
Kind regards,
Robert
Jacinth Gudetti on 17 Mar 2022
Edited: Jacinth Gudetti on 17 Mar 2022
@Sam Chak @Robert U @John D'Errico I have taken some more time to analyze my problem and I believe time should not be considered. rather I think it should be a function of perpendicular distance from the planned (red) path to the actual (blue) path. Time may in fact complicate matters even further as what I am looking for is for the algorithm's accuracy to stay close to the red line rather than anything with respect to time.

Sam Chak on 15 Mar 2022
Edited: Sam Chak on 15 Mar 2022
Here is an example. Say, there are two paths: one is an onion-shaped path and the other is a circular path.
The onion-shaped path with a center at (, ) can be described with
, and ,
while the cicular path with a radius r and the same center is given by
, and .
t = 0:pi/50:2*pi;
r = 1;
xc = 2;
yc = 3;
% Onion-shaped path
x1 = r*(cos(t)).^3 + xc;
y1 = r*sin(t) + yc;
h1 = plot(x1, y1, xc, yc, 'o');
daspect([1, 1, 1])
f1 = @(t) sqrt((-3*((cos(t)).^2).*sin(t)).^2 + cos(t).^2);
len1 = integral(f1, 0, 2*pi)
hold on
% Circular path
x2 = r*cos(t) + xc;
y2 = r*sin(t) + yc;
h2 = plot(x2, y2, xc, yc, 'o');
daspect([1, 1, 1])
f2 = @(t) sqrt(sin(t).^2 + cos(t).^2);
len2 = integral(f2, 0, 2*pi)
hold off
Results:
len1 = 5.8290
len2 = 6.2832
The onion-shaped path is shorter than the circular path. You can also verify that circumference for the circle is .
Also check out @John D'Errico's amazing works on the File Exchange.
Jacinth Gudetti on 19 Mar 2022
Alright, so I solved the problem! Here is my code, p and a correspond to the planned path of the vehicle and the actual path the vehicle actually travelled:
******************************************************************************************************************
planned = [l1x, l1y];
actual = [l2x, l2y];
for p = 0:1024
display("Trial #: " + p)
d1 = 50;
d2 = 50;
a1x = 0; a1y = 0;
a2x = 0; a2y = 0;
for a = 0:1024
d = distance([l1x(p),l1y(p)], [l2x(a),l2y(a)]);
if(d < d1)
d1 = d;
a1x = l2x(a);
a1y = l2y(a);
continue;
end
if(d < d2)
d2 = d;
a2x = l2x(a);
a2y = l2y(a);
end
end
d3 = point_to_line([l1x(p), l1y(p), 0], [a1x, a1y, 0], [a2x, a2y, 0]);
d_f(p) = min([d1,d2,d3]);
hold on;
end
hold off;
figure();
plot([0:1024],d_f(0:1024));
hold off;
function d = point_to_line(pt, v1, v2)
a = v1 - v2;
b = pt - v2;
d = norm(cross(a,b)) / norm(a);
end
******************************************************************************************************************
Sam Chak on 19 Mar 2022
Nothing works without steadfastness in pursuing what you desire. Kudos to you!

### Categories

Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange

R2021b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!