How to add 3th moving ball

1 view (last 30 days)
dmfwlansejr
dmfwlansejr on 10 Sep 2021
Answered: Rik on 10 Sep 2021
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

Answers (1)

Rik
Rik on 10 Sep 2021
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.

Community Treasure Hunt

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

Start Hunting!