how to generate multiple curves with the same track length

3 次查看(过去 30 天)
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 个评论
J. Alex Lee
J. Alex Lee 2022-10-26
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.

请先登录,再进行评论。

采纳的回答

Mathieu NOE
Mathieu NOE 2022-10-26
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 个评论
lei kong
lei kong 2022-10-27
Thanks for your help! The way you calculate the length of the curve is very useful to me.
Mathieu NOE
Mathieu NOE 2022-10-27
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)

请先登录,再进行评论。

更多回答(1 个)

Matt J
Matt J 2022-10-26
编辑:Matt J 2022-10-26
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 个评论
lei kong
lei kong 2022-10-27
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!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by