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
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
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 件のコメント
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
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
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
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!