how to generate multiple curves with the same track length

3 visualizzazioni (ultimi 30 giorni)
I want to know how to generate multiple curves with the same track length. In other words, how can we get the curve of a rope with a certain length (such as 1m) after its random bending.
  1 Commento
J. Alex Lee
J. Alex Lee il 26 Ott 2022
It seems like the harder part of this question is how to make the rules for randomly bending...after that, measuring the track length (arc length) of a curve is relatively easy, just us pythagoreas on each segment making up the curve and add it all up.

Accedi per commentare.

Risposta accettata

Mathieu NOE
Mathieu NOE il 26 Ott 2022
hello
try this
% rope at rest (straight) would have length = 1 (= max(t))
n = 1000;
t = (0:n-1)./n;
max_length = 0.7; % we remove x , y points from curve s that exceed max_length (should be < 1)
figure(1)
hold on
for ck = 1:10
w = 2*pi*rand(1);
x = t;
y = rand(1).*t+cos(w.*t);
% distance formula : ds² = dx² + dy² , then s = cumsum of ds
dx = diff(x);
dy = diff(y);
ds = sqrt(dx.^2 + dy.^2);
s = [0 cumsum(ds)];
id = (s>max_length);
x(id) = [];
y(id) = [];
% recompute s after points removal (to check it works);
dx = diff(x);
dy = diff(y);
ds = sqrt(dx.^2 + dy.^2);
s = sum(ds) % should remains below or equal to max_length
plot(x,y);
end
hold off
  2 Commenti
lei kong
lei kong il 27 Ott 2022
Thanks for your help! The way you calculate the length of the curve is very useful to me.
Mathieu NOE
Mathieu NOE il 27 Ott 2022
My pleasure !
if my suggestion has fullfilled your expectation, don't hesitate to accept it (or someone else answer if it better deserves your project)

Accedi per commentare.

Più risposte (1)

Matt J
Matt J il 26 Ott 2022
Modificato: Matt J il 26 Ott 2022
Using interparc from the File Exchange
format long
for i=1:4
[Px,Py,TrackLength]=makeRope(2);
TrackLength
plot(Px,Py);hold on
end
TrackLength =
1.999999999963130
TrackLength =
1.999999999957910
TrackLength =
1.999999999541550
TrackLength =
1.999999999896211
function [Px,Py,Lfinal]=makeRope(targetLength)
L=targetLength; %target length
N=100; %number of knots
px=sqrt(L)*rand(1,N); py=sqrt(L)*rand(1,N);
t=linspace(1,N,1e5);
Px=interp1(px,t,'spline');
Py=interp1(py,t,'spline');
Lc=cumsum(vecnorm(diff([Px;Py],1,2),2,1));
tc=find(L<Lc,1);
Px=Px(1:tc+1); Py=Py(1:tc+1);
Ltot=sum(vecnorm(diff([Px;Py],1,2),2,1));
pt=interparc(L/Ltot, Px,Py)';
P=[Px;Py];
[~,k]=mink( vecnorm(P-pt,2,1) ,2);
k=min(k)-1;
Px=[Px(1:k),pt(1)]-Px(1); %final points
Py=[Py(1:k),pt(2)]-Py(1);
Lfinal=sum(vecnorm(diff([Px;Py],1,2),2,1)); %confirm the length
end
  2 Commenti
lei kong
lei kong il 27 Ott 2022
Thank you for your help. The matlab functions you used are strange to me. I need a little time to learn, and the results look good! thank you!

Accedi per commentare.

Categorie

Scopri di più su Programming in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by