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!