【玩具】ANU Matlab Toy-Robot Movement Simulation

给朋友做的,当是放松了。。。本人真心不会matlab,下面的代码不代表个人水平,纯属堆砌。

这个程序模拟了一些机器人在一个方形空间中的移动轨迹。

clc;
clear all;
close all;
 
%--------------------Input parameters------------------
%R=input('Number of Robots: ');
%L=input('Length of the Room (m): ');
%N=input('Number of Steps: ');
%S=input('Maximum Size of each Step (cm):');
%typeN=input('Type of the North Wall (Reflecting(0)/Absorbing(1)): ');
%typeS=input('Type of the South Wall (Reflecting(0)/Absorbing(1)): ');
%typeW=input('Type of the West Wall (Reflecting(0)/Absorbing(1)): ');
%typeE=input('Type of the East Wall (Reflecting(0)/Absorbing(1)): ');
 
%-----------random----------------
%R = round(unifrnd(3,10,1,1));
%L = round(unifrnd(1,10,1,1));
%N = round(unifrnd(10,100,1,1));
%S = round(unifrnd(10,100,1,1));
 
%---------pre_assignment------------------
R = 8;
L = 1;
N = 10;
S = 50;
typeN = 0;
typeS = 0;
typeW = 0;
typeE = 0;
 
 
%B=zeros(N+1,3);
B=[];
B(1,:)=[0 0 0];
save result B;
M=N;
for i=1:R
    N = M;
    t = 1;
    position=[0 0 0];
while N>0
    B(M-N+1,:)=position;
    save result B;
    step = rand(1,1)*S;
    d = floor(rand(1,1)*4);
    [nextposition,N] = newposition(d,step,position,L,typeN,typeS,typeW,typeE,N);
    position=nextposition;
    N=N-1;
    t=t+1;
   %disp(position);
end
 B(t,:)=position;
 disp(B);
 plot3(B(:,1),B(:,2),B(:,3),'*-','color',[mod(floor((i-1)/4),2),mod(floor((i-1)/2),2),mod((i-1),2)]);
 hold on;
 title('Position of the Robots');
 text(0,0,0,'Origin');
 text(0,50*L,0,'North');
 text(0,-50*L,0,'South');
 text(50*L,0,0,'West');
 text(-50*L,0,0,'East');
 axis([-50*L 50*L -50*L 50*L 0 M]);
 grid on
end
复制代码
function [ nextposition ,N] = newposition( d,step,position,L,typeN,typeS,typeW,typeE,N )
    %disp(position);
    if d==0
        nextposition = position + [0 step 1];
            if nextposition(2)>50*L
                if typeN==0
                    new=100*L-nextposition(2);
                    nextposition(2)=new;
                else 
                    nextposition(2)=50*L;
                    N=0;
                end
            end
            
    elseif d==1
        nextposition = position + [0 -step 1];
         if nextposition(2)<-50*L
             if typeS==0
                new=-100*L-nextposition(2);
                nextposition(2)=new;
             else
                nextposition(2)=- 50*L;
                N=0;
             end
        end 
    elseif d==2
        nextposition = position + [step 0 1];
        if nextposition(1)>50*L
            if typeE==0
                new=100*L-nextposition(1);
                nextposition(1)=new;  
            else
                nextposition(1)=50*L;
                N=0;
            end
        end
    else
        nextposition = position + [-step 0 1];
         if nextposition(1)<-50*L
             if typeW==0
                new=-100*L-nextposition(1);
                nextposition(1)=new;
             else
                 nextposition(1)=-50*L;
                 N=0;
             end
         end
    end
end
复制代码

posted @   花考拉  阅读(326)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示