【问题标题】:moving train of cars simulation移动火车模拟
【发布时间】:2016-04-26 17:32:24
【问题描述】:

我们正在尝试使用 Matlab 模拟移动的汽车这是我们编写的代码:

function []=moving_cars()
    figure('units','normalized','outerposition',[0 0 1 1])
    x = linspace(0,30,10);
    y2=0.6;
    y3=0.2;
    y4=-0.2;
    axis([0,20,-0.4,1.5])
    ax = gca;
    hold on
    //%road
    plot(x,y2*ones(size(x)), 'LineWidth', 1,'Color','black')
    hold on
    plot(x,y3*ones(size(x)),'--', 'LineWidth', 1,'Color','black')
    hold on
    plot(x,y4*ones(size(x)), 'LineWidth', 1,'Color','black')
    hold off
    axis off
    title('( Moving cars Simulation )')
    set(gcf,'color','w')
    //%load image data
    [imageData0, map, alpha] = imread('00.png', 'png');
    [imageData1, map, alpha] = imread('20.jpg', 'jpg');
    [imageData2, map, alpha] = imread('27.jpg', 'jpg');
    //%create Figure and Axes objects
    f1 = figure(1);
    a0 = axes('Parent', f1);
    a1 = axes('Parent', f1);
    a2 = axes('Parent', f1);
    //%Add Image objects to axes
    image(imageData0, 'Parent', a0);
    image(imageData1, 'Parent', a1);
    image(imageData2, 'Parent', a2);
    //%Resize and move the Image objects
    set(a0, 'Units', 'Pixel', 'Position', [-160 150 150 70],'Box','off','Visible','off');
    set(a1, 'Units', 'Pixel', 'Position', [-160 150 150 70],'Box','off','Visible','off');
    set(a2, 'Units', 'Pixel', 'Position', [-160 150 150 70],'Box','off','Visible','off');
    axis off
    //%generate the train
    Train= car_train()
    //%moving the cars
    check_type(Train,a0,a1,a2);
end
function [Cars]= car_train()
    //%create random car train (10 cars between 1:2)
    Cars = round((2-1).*rand(5,1) + 1);
end
function[]= check_type(Cars,a0,a1,a2,a3,a6)
    for k = 1:length(Cars)
        if(Cars(k)==1)
            for i=1:2:1800
                set(a1, 'Position', get(a0,'Position') + [i 0 0 0]);
                drawnow
            end
        elseif(Cars(k)==2)
            for i=1:2:1800
                set(a2, 'Position', get(a0,'Position') + [i 0 0 0]);
                drawnow
            end
        end
    end
end

问题是我们需要一列汽车而不是一个车一个车地移动(在汽车移动 200 次后,Car 数组中的下一个类型的汽车会移动,依此类推,直到 Car 数组的末尾)

有人可以帮忙吗?

提前致谢

【问题讨论】:

    标签: matlab simulation


    【解决方案1】:

    您需要交换这些 for 循环:

    for k = 1:length(Cars)
        if(Cars(k)==1)
        for i=1:2:1800
            ...
    

    因为现在您要为每辆车准备好每一个动作。你需要做的,为每个位置移动曾经的火车车厢。

    function[]= check_type(Cars,a0,a1,a2,a3,a6)
       for i=1:2:1800
           for k = 1:length(Cars)
               if(Cars(k)==1)
                   set(a1, 'Position', get(a0,'Position') + [i 0 0 0]);
                   drawnow
               elseif(Cars(k)==2)
                    set(a2, 'Position', get(a0,'Position') + [i 0 0 0]);
                    drawnow
              end
          end
       end
    end
    

    【讨论】:

    • @user3332603 当然可以。这是未经测试的。您需要自己完成测试腿工作。
    • @Ben 根据汽车类型移动汽车的if语句呢??
    • @user3332603 你什么意思?我删除的if 语句只是为了将汽车 1 和汽车 2 分开。你试过了吗?
    • @user3332603 抱歉,我现在明白你的意思了,已修复。
    • 是的,我试过你的代码,第一辆车只在屏幕上移动。我们有两种类型的汽车(1&2),if 语句用于检查函数“car_train”随机生成的汽车类型并存储在数组“Cars”中,因此无法删除
    猜你喜欢
    • 2011-01-04
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    相关资源
    最近更新 更多