A question about motion plot

1 次查看(过去 30 天)
Hello
I've written a MATLAB function such below:
function motionplot3D(p,next_pos)
x1=[p(1) next_pos(1)];
x2=[p(4) next_pos(4)];
x3=[p(7) next_pos(7)];
x4=[p(10) next_pos(10)];
x5=[p(13) next_pos(13)];
y1=[p(2) next_pos(2)];
y2=[p(5) next_pos(5)];
y3=[p(8) next_pos(8)];
y4=[p(11) next_pos(11)];
y5=[p(14) next_pos(14)];
z1=[p(3) next_pos(3)];
z2=[p(6) next_pos(6)];
z3=[p(9) next_pos(9)];
z4=[p(12) next_pos(12)];
z5=[p(15) next_pos(15)];
totaltime = seconds(0.3);
steptime = totaltime/length(x1);
figure;
for ii = 1:length(x1)-1
npoints = 200;
pointsx1 = linspace(x1(ii),x1(ii+1),npoints);
pointsy1 = linspace(y1(ii),y1(ii+1),npoints);
pointsz1 = linspace(z1(ii),z1(ii+1),npoints);
pointsx2 = linspace(x2(ii),x2(ii+1),npoints);
pointsy2 = linspace(y2(ii),y2(ii+1),npoints);
pointsz2 = linspace(z2(ii),z2(ii+1),npoints);
pointsx3 = linspace(x3(ii),x3(ii+1),npoints);
pointsy3 = linspace(y3(ii),y3(ii+1),npoints);
pointsz3 = linspace(z3(ii),z3(ii+1),npoints);
pointsx4 = linspace(x4(ii),x4(ii+1),npoints);
pointsy4 = linspace(y4(ii),y4(ii+1),npoints);
pointsz4 = linspace(z4(ii),z4(ii+1),npoints);
pointsx5 = linspace(x5(ii),x5(ii+1),npoints);
pointsy5 = linspace(y5(ii),y5(ii+1),npoints);
pointsz5 = linspace(z5(ii),z5(ii+1),npoints);
pointsx6 = linspace(x6(ii),x6(ii+1),npoints);
pointsy6 = linspace(y6(ii),y6(ii+1),npoints);
pointsz6 = linspace(z6(ii),z6(ii+1),npoints);
pointsx7 = linspace(x7(ii),x7(ii+1),npoints);
pointsy7 = linspace(y7(ii),y7(ii+1),npoints);
pointsz7 = linspace(z7(ii),z7(ii+1),npoints);
pointsx8 = linspace(x8(ii),x8(ii+1),npoints);
pointsy8 = linspace(y8(ii),y8(ii+1),npoints);
pointsz8 = linspace(z8(ii),z8(ii+1),npoints);
pointsx9 = linspace(x9(ii),x9(ii+1),npoints);
pointsy9 = linspace(y9(ii),y9(ii+1),npoints);
pointsz9 = linspace(z9(ii),z9(ii+1),npoints);
for jj = 1:npoints
pause(seconds(steptime)/npoints);
plot3([pointsx1(jj),pointsx1(jj)],[pointsy1(jj),pointsy1(jj)],[pointsz1(jj),pointsz1(jj)],'-*',[pointsx2(jj),pointsx2(jj)],[pointsy2(jj),pointsy2(jj)],[pointsz2(jj),pointsz2(jj)],'-o',...
[pointsx3(jj),pointsx3(jj)],[pointsy3(jj),pointsy3(jj)],[pointsz3(jj),pointsz3(jj)],'-o',...
[pointsx4(jj),pointsx4(jj)],[pointsy4(jj),pointsy4(jj)],[pointsz4(jj),pointsz4(jj)],'-o',...
[pointsx5(jj),pointsx5(jj)],[pointsy5(jj),pointsy5(jj)],[pointsz5(jj),pointsz5(jj)],'-o',...
[pointsx6(jj),pointsx6(jj)],[pointsy6(jj),pointsy6(jj)],[pointsz6(jj),pointsz6(jj)],'-o',...
[pointsx7(jj),pointsx7(jj)],[pointsy7(jj),pointsy7(jj)],[pointsz7(jj),pointsz7(jj)],'-o',...
[pointsx8(jj),pointsx8(jj)],[pointsy8(jj),pointsy8(jj)],[pointsz8(jj),pointsz8(jj)],'-o',...
[pointsx9(jj),pointsx9(jj)],[pointsy9(jj),pointsy9(jj)],[pointsz9(jj),pointsz9(jj)],'-o');
xlim([-5 30]);
ylim([-5 30]);
zlim([-5 30]);
grid on
xlabel('X Axis'), ylabel('Y Axis') , zlabel('Z Axis');
end
end
In this function I want to show motion plot of 5 points moving from a point to the next point. p , next_pos are 15*1 vectors that show coordinates of 5 points in 3D space. p is start and next_pos is next.
Now I want to add another item. I want to draw some lines between each pair of points to show moving path. For better understanding see below:
Up picture is a current shot of my motion plot and down picture is my desired. Please help me how to do this.
Thank you!

采纳的回答

Aditya Singh
Aditya Singh 2023-7-11
编辑:Aditya Singh 2023-7-11
Hi Sina,
I understand you want to add line between points in the motion plot.
You can do it by plotting a line between the desired points in the for loop itself. You can refer to the below code
x1 = [p(1) next_pos(1)];
x2 = [p(4) next_pos(4)];
x3 = [p(7) next_pos(7)];
x4 = [p(10) next_pos(10)];
x5 = [p(13) next_pos(13)];
y1 = [p(2) next_pos(2)];
y2 = [p(5) next_pos(5)];
y3 = [p(8) next_pos(8)];
y4 = [p(11) next_pos(11)];
y5 = [p(14) next_pos(14)];
z1 = [p(3) next_pos(3)];
z2 = [p(6) next_pos(6)];
z3 = [p(9) next_pos(9)];
z4 = [p(12) next_pos(12)];
z5 = [p(15) next_pos(15)];
totaltime = seconds(0.3);
steptime = totaltime / length(x1);
figure;
hold on; % Add this line to enable plotting lines between points
for ii = 1:length(x1)-1
npoints = 200;
pointsx1 = linspace(x1(ii), x1(ii+1), npoints);
pointsy1 = linspace(y1(ii), y1(ii+1), npoints);
pointsz1 = linspace(z1(ii), z1(ii+1), npoints);
pointsx2 = linspace(x2(ii), x2(ii+1), npoints);
pointsy2 = linspace(y2(ii), y2(ii+1), npoints);
pointsz2 = linspace(z2(ii), z2(ii+1), npoints);
pointsx3 = linspace(x3(ii), x3(ii+1), npoints);
pointsy3 = linspace(y3(ii), y3(ii+1), npoints);
pointsz3 = linspace(z3(ii), z3(ii+1), npoints);
pointsx4 = linspace(x4(ii), x4(ii+1), npoints);
pointsy4 = linspace(y4(ii), y4(ii+1), npoints);
pointsz4 = linspace(z4(ii), z4(ii+1), npoints);
pointsx5 = linspace(x5(ii), x5(ii+1), npoints);
pointsy5 = linspace(y5(ii), y5(ii+1), npoints);
pointsz5 = linspace(z5(ii), z5(ii+1), npoints);
for jj = 1:npoints
pause(seconds(steptime) / npoints);
plot3([pointsx1(jj), pointsx1(jj)], [pointsy1(jj), pointsy1(jj)], [pointsz1(jj), pointsz1(jj)], '-*',...
[pointsx2(jj), pointsx2(jj)], [pointsy2(jj), pointsy2(jj)], [pointsz2(jj), pointsz2(jj)], '-o',...
[pointsx3(jj), pointsx3(jj)], [pointsy3(jj), pointsy3(jj)], [pointsz3(jj), pointsz3(jj)], '-o',...
[pointsx4(jj), pointsx4(jj)], [pointsy4(jj), pointsy4(jj)], [pointsz4(jj), pointsz4(jj)], '-o',...
[pointsx5(jj), pointsx5(jj)], [pointsy5(jj), pointsy5(jj)], [pointsz5(jj), pointsz5(jj)], '-o');
% Add lines between points
line([pointsx1(jj), pointsx2(jj)], [pointsy1(jj), pointsy2(jj)], [pointsz1(jj), pointsz2(jj)]);
line([pointsx2(jj), pointsx3(jj)], [pointsy2(jj), pointsy3(jj)], [pointsz2(jj), pointsz3(jj)]);
line([pointsx3(jj), pointsx4(jj)], [pointsy3(jj), pointsy4(jj)], [pointsz3(jj), pointsz4(jj)]);
line([pointsx4(jj), pointsx5(jj)], [pointsy4(jj), pointsy5(jj)], [pointsz4(jj), pointsz5(jj)]);
xlim([-5 30]);
ylim([-5 30]);
zlim([-5 30]);
grid on;
xlabel('X Axis');
ylabel('Y Axis');
zlabel('Z Axis');
end
end
For more information, please refer to
Hope it helps!

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by