How to add 3th moving ball

2 次查看(过去 30 天)
dmfwlansejr
dmfwlansejr 2021-9-10
回答: Rik 2021-9-10
2 Ball is moving
I want to add 3th moving ball (start position (0,1))
How to is it?
Following is My Code :(For operate, Copy & Paste)
x1_st=0;y1_st=0.5;
x2_st=0;y2_st=1.5;
%plot
h_fg=figure;
h_ax=axes(h_fg);
h_p1=plot(h_ax,x1_st,y1_st,'b.','MarkerSize',40); hold(h_ax,'on');
h_p2=plot(h_ax,x2_st,y2_st,'r.','MarkerSize',40); hold(h_ax,'on');
% change Properties
set(h_ax,'XLim',[-5 5]);
set(h_ax,'YLim',[0 5]);
h_ax.YLim=[0 2];
h_ax.XLim=[-5 5];
% for loop
dx=[1:0.1:5, -5:0.1:0];
for ii=1:length(dx)
x1_new=x1_st+dx(ii);
x2_new=x2_st-dx(ii);
set(h_p1,'XData',x1_new);
set(h_p2,'XData',x2_new);
pause(0.02);
end

回答(1 个)

Rik
Rik 2021-9-10
If you replace numbered variables with arrays you can use simple loops with indexing. With your current setup you will have to copy and paste every line of code referring to a ball.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by