MATLAB 水管动画应用程序 - 循环移动效果

本应用程序使用 MATLAB App Designer 创建了一个简单的水管动画,演示了水流在管道中循环移动的效果。

代码:

classdef shuiguan < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        WaterPipeAnimationApp  matlab.ui.Figure
        Button_2               matlab.ui.control.Button
        Button                 matlab.ui.control.Button
        UIAxes                 matlab.ui.control.UIAxes
    end

    
    properties (Access = private)
        Timer_id;       % 定时器
        pipePosition;   % 水管位置
        waterColor;     % 水流颜色
        waterWidth;     % 水流宽度
        waterSpeed;     % 水流速度
        isBlueColor;    % 用于切换水流颜色
    end
        methods (Access = public)
        
    end
    
    methods (Access = private)
        
 function timer_init(app)
            app.Timer_id = timer;
            app.Timer_id.StartDelay = 0;
            app.Timer_id.Period = 1;  % 设置定时器间隔,控制水流速度
            app.Timer_id.ExecutionMode = 'fixedSpacing';
            app.Timer_id.TimerFcn = @(~, ~) timer_handler(app);
        end
    
        % 定时器启动
        function timer_start(app)
            start(app.Timer_id);
        end
    
        % 定时停止
        function timer_stop(app)
            stop(app.Timer_id);
        end
    
        % 删除定时器
        function timer_delete(app)
            delete(app.Timer_id);
        end
    
        % 定时器回调
        function timer_handler(app)
    cla(app.UIAxes); % 清空图形以便绘制新的水流
    
    numRectangles = 5; % 设置要绘制的矩形数量
    rectangleWidth = 1.5; % 每个矩形的宽度
    rectangleGap = 0.5; % 矩形之间的间距
    
    % 更新每个矩形的位置
    for i = 1:numRectangles
        % 计算每个矩形的位置
        rectPosition = [(i - 1) * (rectangleWidth + rectangleGap) + app.pipePosition(i), -0.5, rectangleWidth, 1];
        rectangle(app.UIAxes, 'Position', rectPosition, 'FaceColor', 'blue', 'EdgeColor', 'none');
        
        % 更新水管位置
        app.pipePosition(i) = app.pipePosition(i) + app.waterSpeed;
        
        % 检查水管是否越界,如果越界则重置位置
        if app.pipePosition(i) > app.UIAxes.XLim(2)
            app.pipePosition(i) = app.pipePosition(i) - (numRectangles - 1) * (rectangleWidth + rectangleGap);
        end
    end
    
    title(app.UIAxes, 'Water Pipe Animation');
    xlabel(app.UIAxes, 'X');
    ylabel(app.UIAxes, 'Y');
end
        end
        

    % Callbacks that handle component events
    methods (Access = private)

        % Code that executes after component creation
        function startupFcn(app)
            % 初始化水管的位置
            app.pipePosition = zeros(1, 5);
            % 设置水流的颜色、宽度和速度
            app.waterColor = 'white';
            app.waterWidth = 1;
            app.waterSpeed = 1;
            app.isBlueColor = true; % 初始颜色为蓝色
            
            % 初始化定时器
            timer_init(app);


        end

        % Button pushed function: Button
        function ButtonPushed(app, event)
           timer_start(app);
         
        end

        % Button pushed function: Button_2
        function Button_2Pushed(app, event)
          timer_stop(app);
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create WaterPipeAnimationApp and hide until all components are created
            app.WaterPipeAnimationApp = uifigure('Visible', 'off');
            app.WaterPipeAnimationApp.Position = [100 100 640 480];
            app.WaterPipeAnimationApp.Name = 'MATLAB App';

            % Create UIAxes
            app.UIAxes = uiaxes(app.WaterPipeAnimationApp);
            title(app.UIAxes, 'Title')
            xlabel(app.UIAxes, 'X')
            ylabel(app.UIAxes, 'Y')
            zlabel(app.UIAxes, 'Z')
            app.UIAxes.XLim = [0 10];
            app.UIAxes.ALim = [0 1];
            app.UIAxes.XTick = [0 1 2 3 4 5 6 7 8 9 10];
            app.UIAxes.Position = [1 271 609 185];

            % Create Button
            app.Button = uibutton(app.WaterPipeAnimationApp, 'push');
            app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
            app.Button.Position = [24 187 100 24];
            app.Button.Text = '开始';

            % Create Button_2
            app.Button_2 = uibutton(app.WaterPipeAnimationApp, 'push');
            app.Button_2.ButtonPushedFcn = createCallbackFcn(app, @Button_2Pushed, true);
            app.Button_2.Position = [176 187 100 24];
            app.Button_2.Text = '停止';

            % Show the figure after all components are created
            app.WaterPipeAnimationApp.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = shuiguan

            % Create UIFigure and components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.WaterPipeAnimationApp)

            % Execute the startup function
            runStartupFcn(app, @startupFcn)

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.WaterPipeAnimationApp)
        end
    end
end

主要修改:

  1. startupFcn 函数中,将 pipePosition 初始化为一个包含 5 个元素的数组,每个元素表示一个矩形的位置。
  2. timer_handler 函数中,对每个矩形的位置进行计算和更新,以实现每个单独的蓝色矩形移动到右边边界后,从左边边界重新出现并继续向右移动。

使用方法:

  1. 将上述代码保存为 shuiguan.m 文件。
  2. 在 MATLAB 中运行 shuiguan 函数,即可启动水管动画应用程序。
  3. 点击“开始”按钮启动动画,点击“停止”按钮停止动画。

效果:

该应用程序将会显示一个水平的水管,并模拟水流在管道中循环移动的效果。每个单独的蓝色矩形将会在到达右边边界之后,从左边边界重新出现并继续向右移动。

注意:

  • 水流的颜色、宽度和速度可以通过修改 waterColorwaterWidthwaterSpeed 属性进行调整。
  • 矩形的数量可以通过修改 numRectangles 属性进行调整。
  • 水流速度可以通过修改定时器 Period 属性进行调整。

希望以上修改和解释可以帮助你理解并实现你想要的效果。如果你还有其他问题,请随时提出。

MATLAB 水管动画应用程序 | 水流循环效果

原文地址: http://www.cveoy.top/t/topic/RHU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录