필터 지우기
필터 지우기

how to make a smooth spline connection at endpoints?

조회 수: 4 (최근 30일)
mark palmer
mark palmer 2020년 3월 26일
댓글: mark palmer 2020년 3월 26일
I can draw a smooth spline across points but when I try to join the end to the beginning I get a sharp corner instead of a smooth connection. How can I smooth out the endpoint connection to make a smoothly shaped blob? Here's my code:
x1 = [0 2 1 4 3 2];
y1 = [1 0 2 1 3 2];
tnum = 1000;
t = linspace(0,1,tnum);
x1 = [x1 x1(1)]; % ADDING THE FIRST POINT TO THE END
y1 = [y1 y1(1)]; % ADDING THE FIRST POINT TO THE END
[xt yt] = ParametricSpline(x1, y1);
xref = ppval(xt, t);
yref = ppval(yt, t);
plot(x1, y1, 'o', xref, yref);
function [xtF, ytF] = ParametricSpline(x,y)
arc_length = 0;
n = length(x);
t = zeros(n, 1);
for i=2:n
arc_length = sqrt((x(i)-x(i-1))^2 + (y(i)-y(i-1))^2);
t(i) = t(i-1) + arc_length;
end
t=t./t(length(t));
xtF = spline(t, x);
ytF = spline(t, y);
end
  댓글 수: 2
darova
darova 2020년 3월 26일
What happens if you do spline twice (add the same points)?
mark palmer
mark palmer 2020년 3월 26일
I thought of that too last night, will try, but I suspect there should be a more elegant solution.

댓글을 달려면 로그인하십시오.

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 3월 26일
Try csape:
x1 = [0 2 1 4 3 2];
y1 = [1 0 2 1 3 2];
tnum = 1000;
t = linspace(0,1,tnum);
x1 = [x1 x1(1)]; % ADDING THE FIRST POINT TO THE END
y1 = [y1 y1(1)]; % ADDING THE FIRST POINT TO THE END
[xt yt] = ParametricSpline(x1, y1);
xref = ppval(xt, t);
yref = ppval(yt, t);
plot(x1, y1, 'o', xref, yref);
function [xtF, ytF] = ParametricSpline(x,y)
arc_length = 0;
n = length(x);
t = zeros(n, 1);
for i=2:n
arc_length = sqrt((x(i)-x(i-1))^2 + (y(i)-y(i-1))^2);
t(i) = t(i-1) + arc_length;
end
t=t./t(length(t));
xtF = csape(t, x, 'perodic');
ytF = csape(t, y, 'perodic');
end
Result:
  댓글 수: 1
Akira Agata
Akira Agata 2020년 3월 26일
Or you can use spline function, like:
x1 = [0 2 1 4 3 2];
y1 = [1 0 2 1 3 2];
x1 = [x1 x1(1)]; % ADDING THE FIRST POINT TO THE END
y1 = [y1 y1(1)]; % ADDING THE FIRST POINT TO THE END
t = linspace(0,2*pi,numel(x1));
pp = spline(t,[0 x1 0; -1 y1 -1]); % Assuming an endslope at (0,1) is (0, -1)
xy2 = ppval(pp, linspace(0,2*pi));
figure
scatter(x1,y1)
hold on
plot(xy2(1,:),xy2(2,:))

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spline Postprocessing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by