Locating a point along a curve some distance away from an initial point.

32 views (last 30 days)
So I have a curve, but I don't have the equation for this curve, just a set of data points. Given an initial point on this curve I want to find another point along this curve at a specified distance away.
As of right now my idea at a solution method involves defining a region in front of the initial point, calucating the distance between every point in this region and my initial point, selecting the 2 points that are closest to my desired distance away, and interpolating between those to points. However this is exteremely inefficient, and would take ages to run in my larger simulation.
Is there a more efficient way to do this?

Accepted Answer

John D'Errico
John D'Errico on 5 Apr 2019
Edited: John D'Errico on 6 Apr 2019
Easy, peasy. That is, assuming you are asking about a distance in terms of arclength along the curve. If not, then the answer is of course different. Still entirely possible to solve, but completely different.
Use my interparc tool, combined with my arclength tool. They are both on the file exchange. (Links provided below.)
x = linspace(0,2*pi,20);
y = sin(x);
Find a new point at a distance of 2 units along the curve. Use a spline to interpolate the points. The total arclength of the curve is 7.6403... units.
L = arclength(x,y,'spline')
L =
7.6403
The first point on the curve lies at arclength 0. Interparc uses a relative arclength.
format long g
xy = interparc(2/L,x,y,'spline')
xy =
1.66061053630525 0.995974772830712
So that point lies at a distance along the curve of 2 units.
plot(x,y,'o',xy(1),xy(2),'r*')
Or, suppose I have some other point on the curve, and I want to find a new point that lies exactly 2 units further along the curve? Use my distance2curve utility first, as we need to locate it on the curve in terpms of arclength.
x0 = 3;
y0 = sin(x0);
So (x0,y0) should lie along the curve. But where does it lie in terms of arclenth along that curve from the start?
[xyc,d0,t0] = distance2curve([x;y]',[x0,y0],'spline')
xyc =
2.99999755728184 0.141117541012307
d0 =
3.4717712602551e-06
t0 =
0.473835095624448
So the point lies on the curve, but it did not lie EXACTLY on the spline itself. No matter, as it was only a tiny amount off. The closest point on the spline is very close. There are two neighbors to that point, located at a distance of 2 units along the spline interpolant.
xyneighbors = interparc(t0 + [-2/L,2/L],x,y,'spline')
xyneighbors =
1.28471554283348 0.959412594091491
4.60259974918216 -0.993988748347396
So the green star is on the curve. The pair of red stars lie at a distance of 2 units along the curve in either direction.
No loops, searches, or iteration is required. (Well, no explicit loops or any work on your part.)
Note that if extreme efficiency is required, you can use all of those tools with linear interpolation instead of a spline. They will all be highly efficient then, although the results will be somewhat less accurate when based on a piecewise linear interpolant.
Find the tools I have used on the file exchange, here:
  6 Comments
bahar gharehpapagh
bahar gharehpapagh on 8 Sep 2021
dear John D'Errico
If the problem is about points of a 2d curve that exist just coordinates, how can we find points with a defined distance from those coordinates? I handle it by locating circles on the points and finding intersection points. Is there another efficient method?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!