# Equally spaced points along a nonlinear path

44 Ansichten (letzte 30 Tage)
Kacper am 26 Mai 2024 um 17:56
Bearbeitet: John D'Errico am 27 Mai 2024 um 0:25
I have a path linear by parts that I need to equally divide with points. How do I do that?
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Manikanta Aditya am 26 Mai 2024 um 18:10
To equally divide a path that is linear by parts, you can follow these steps:
Calculate the total length of the path and determine the spacing between the points and place the points along the path.
Check the following code as a example for your understanding:
% Define the path as a list of points
path = [0, 0; 1, 1; 2, 0; 3, 1];
% Calculate the total length of the path
lengths = sqrt(sum(diff(path).^2, 2));
total_length = sum(lengths);
% Display the total length of the path
disp(['Total length of the path: ', num2str(total_length)])
Total length of the path: 4.2426
% Number of divisions
num_divisions = 10;
% Calculate the spacing
spacing = total_length / num_divisions;
% Display the spacing
disp(['Spacing between points: ', num2str(spacing)])
Spacing between points: 0.42426
% Place the points
points = [];
current_length = 0;
for i = 1:size(path, 1)-1
while current_length < lengths(i)
points = [points; path(i, :) + (path(i+1, :) - path(i, :)) * current_length / lengths(i)];
current_length = current_length + spacing;
end
end
points = [points; path(end, :)];
% Display the points
disp('Equally spaced points along the path:')
Equally spaced points along the path:
disp(points)
0 0 0.3000 0.3000 0.6000 0.6000 0.9000 0.9000 3.0000 1.0000
##### 2 KommentareKeine anzeigenKeine ausblenden
Kacper am 26 Mai 2024 um 18:30
Thanks. I thought there was a neat function to do it for me and I just coudn't find it, but as long as it works.
Manikanta Aditya am 26 Mai 2024 um 18:36
Thanks!

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (1)

John D'Errico am 27 Mai 2024 um 0:14
Bearbeitet: John D'Errico am 27 Mai 2024 um 0:25
Simpler yet, just download my interparc from the file exchange. It allows you to do the operation in a variety of ways, using several variations of spline interpolant, or assuming piecewise linear segments.
It does all the work for you, and it is free.
x = randn(6,1);
y = randn(6,1);
% 500 points, equally spaced along the curve in arclength
Pspline = interparc(500,x,y,'spline');
plot(x,y,'ro',Pspline(:,1),Pspline(:,2),'-')
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

R2024a

### Community Treasure Hunt

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

Start Hunting!

Translated by