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.
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')
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')
So that point lies at a distance along the curve of 2 units.
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?
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.
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: