2016-06-21 108 views
-1

我們試圖模擬使用不同顏色和大小的點(汽車類型1:紅色尺寸20,汽車類型2:綠色尺寸40等)的移動汽車,問題是如果有是它們重疊,他們應該遵循相互相同類型的兩輛車,這是使用代碼:移動火車點數(汽車)

x = linspace(0,30,1000); 
axis([0,20,-0.4,1.5]); 
ax = gca; 
h = hgtransform('Parent',ax); 
type1=plot(-1,0.4,'s','Parent',h,'MarkerFaceColor','red','MarkerSize',20); 
type2=plot(-1,0.4,'s','Parent',h,'MarkerFaceColor','green','MarkerSize',40); 
car=[1 2 2 1 1]; 
for k = 1:10:700 
    for i = 1:length(car) 
     if(car(i)==1) 
      set(type1,'XData',x(k),'YData',0.4); 
      pause(0.1); 
     elseif(car(i)==2) 
      set(type2,'XData',x(k)-3,'YData',0.4); 
      pause(0.1); 
     end 
    end 
end 

如何保持序列,每一個(X = 3)新車啓動時不刪除或過度研磨移動在它前面的汽車。

Thanks inadvance

回答

1

您可能想要下面的代碼。

x = linspace(0,30,1000); 
axis([0,20,-0.4,1.5]); 
ax = gca; 
car=[1 2 2 1 1]; 
h = hgtransform('Parent',ax); 
Ncar=length(car); 

for n=1:Ncar %generate 5 cars having one type among type1 and type2 
    if car(n)==1; 
     types(n)=plot(-1,0.4,'s','Parent',h,'MarkerFaceColor','red','MarkerSize',20); 
    else 
     types(n)=plot(-1,0.4,'s','Parent',h,'MarkerFaceColor','green','MarkerSize',40); 
    end 
end 

for k = 1:10:700 %plotting the cars sequencially 
    for i = 1:length(car) 
      set(types(i),'XData',x(k)-3*(i-1),'YData',0.4); 
     pause(0.1); 
    end 
end 
+0

這很好,它運作良好 – user3332603