File Exchange

image thumbnail


version 1.0 (3.27 KB) by

Generate evenly spaced points along an existing curve in 2D or 3D.

11 Ratings



View License

CURVSPACE(P,N) generates N points that interpolates a curve (represented by a set of points) with an equal spacing. Each row of P defines a point, which means that P should be a n x 2 (2D) or a n x 3 (3D) matrix.
x = -2*pi:0.5:2*pi;
y = 10*sin(x);
z = linspace(0,10,length(x));
N = 50;
p = [x',y',z'];
q = curvspace(p,N);

The above example creates 50 evenly spaced points along a sinusoidal curve.

Comments and Ratings (14)

Ginny Catania


Emily (view profile)


Alize (view profile)


This function is very good! You solved me a very big problem! Thank you!

John D'Errico

John D'Errico (view profile)

Not vectorized, and purely linear interpolation. Better is my own interparc, also here on the fex.

For example, given 4 points around a circle, try this:

t = linspace(0,2*pi,5)';
xy = [cos(t),sin(t)];

tic,xyi = interparc(100,xy(:,1),xy(:,2),'lin');toc
Elapsed time is 0.001540 seconds.

tic,xyc = curvspace(xy,100);toc
Elapsed time is 0.004692 seconds.

So the unvectorized code is slower. And if we plot the points, we see a diamond results, thus connect the dots interpolation.


You can use interparc to get a nice smooth curve instead. Of course, it will be slower, since it is actually finding the true distance along the interpolated spline curve.

xyi = interparc(100,xy(:,1),xy(:,2),'cs');

The help is good enough, and I see plentiful internal comments, so well done there. The code is reasonable, although a bit loopy.

ted p teng

ted p teng (view profile)


TCH (view profile)

great job. I have implemented an equivalent function in the following link, :-.
purely could be a reference.


Erhong (view profile)

thanks a lot, I didn't want to lose time re-implementing it!

Sven Mensing

Thank you for your excellent function. If you replace the distance by

function l = distance(x,y)
l = sqrt(sum((x-y).^2,2));

and calculate the distance between the points as follows:

dist_bet_pts = distance ( p(1:(length(p)-1),:), p(2:length(p),:));

you can increase the speed of curvspace by 300%.

Thanks again

Sven Mensing

Daniel Daniel

Works Great! This is an incredibly useful tool. Unlike Matlab's interp functions the X-data does not have to be distint.

Thankyou for this handy function.

Valer Jurcak

Works fine, thaks. Please make N input also as percentual legth.
if N < 1 % e.g. 0.1 make new point every 10% of totaldist.
N = N * totaldist;

meng mingyang

It's not as my imagination,but it illumined me .Thank you!

meng mingyang

useful program! It's exact what I want!
Thank you very much!



BSD License added.

MATLAB Release
MATLAB 5.3.1 (R11.1)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video