避免形状重叠

时间:2020-07-13 06:06:13

标签: matlab

我是MATLAB的新手,我正在编程,四个形状在同一平面上并且随机移动,但是它们不能相互接触。我无法弄清楚如何使形状彼此之间不接触地随机移动,我发布了代码并寻找建议。

%ploting a Rectangle
xrec=[-2 3 3 -2 -2] ;
yrex=[-4 -4 0 0 -4];
figure
plot(xrex,yrex).  
%ploting a triangle
close all
xtri=[0 2 0 0 0];
ytri=[1 1 5 1 5];
figure
plot(xtri, ytri)
%ploting a square
close all
xsq=[-2 2 2 -2 -2];
ysq=[-2 -2 2 2 -2];
Figure
plot(xsq,ysq)
%plotting a circle
close all
theta=linspace(0,2*pi);
r=1
xcir=r*cos(theta);
ycir=r*sin(theta);
plot(xcir,ycir)
%put four shapes in top right, top left, bottom right, bottom left.
close all
d=5;
Figure
plot(xcir+d,ycir+d)
hold on
plot(xsq-d, ysq+d)
Plot(xrec-d, yrec-d)
Plot(xtri+d, ytri-d)
% animation
close all
for i= 25
plot(xRec-d+(-1+2*rand(1))/(d),yRec+d+(-1+2*rand(1))/d,'r','LineWidth',2);
hold on
plot(xTri+d+(-1+2*rand(1))/(d),yTri+d+(-1+2*rand(1))/d,'r','LineWidth',2);
plot(xSq-d+(-1+2*rand(1))/(d),ySq-d+(-1+2*rand(1))/d,'r','LineWidth',2);
plot(xCir+d+(-1+2*rand(1))/(d),yCir-d+(-1+2*rand(1))/d,'r','LineWidth',2)

0 个答案:

没有答案