Borrar filtros
Borrar filtros

How do I change the squares in this code to circles?

1 visualización (últimos 30 días)
Jordan Means
Jordan Means el 9 de Oct. de 2018
Respondida: Cris LaPierre el 11 de Nov. de 2018
I'm trying to change the shapes this code runs to circles:
clear all
close all
clc
x1 = [-1 1 1 -1 -1]*3-5;
x2 = [-1 1 1 -1 -1]*3+5;
y1 = [-1 -1 1 1 -1]*3;
y2 = [-1 -1 1 1 -1]*3;
dx1=0.4;
dx2=-0.05;
dy1=0.05;
dy2=0.2;
Lx=max(x1)-min(x1);
Ly=max(y1)-min(y1);
for i=1:1000
figure(1),plot(x1,y1,x2,y2)
axis equal
set(gca,'xlim',[-20 20]);
set(gca,'ylim',[-20 20]);
x1=x1+dx1;
x2=x2+dx2;
y1=y1+dy1;
y2=y2+dy2;
if max(x1)>=20
x1=x1-(max(x1)-20);
dx1=-dx1;
elseif min(x1)<=-20
x1=x1-(min(x1)+20);
dx1=-dx1;
end
if max(y1)>=20
y1=y1-(max(y1)-20);
dy1=-dy1;
elseif min(y1)<=-20
y1=y1-(min(y1)+20);
dy1=-dy1;
end
if max(x2)>=20
x2=x2-(max(x2)-20);
dx2=-dx2;
elseif min(x2)<=-20
x2=x2-(min(x2)+20);
dx2=-dx2;
end
if max(y2)>=20
y2=y2-(max(y2)-20);
dy2=-dy2;
elseif min(y2)<=-20
y2=y2-(min(y2)+20);
dy2=-dy2;
end
if abs(mean(x1)-mean(x2))<Lx && abs(mean(y1)-mean(y2))<Ly
title(['close to each other, iteration:' num2str(i)])
if max(x1)>min(x2) || max(x2)>min(x1)
a=dy2;
dy2=dy1;
dy1=a;
end
if max(y1)>min(y2) || max(y2)>min(y1)
a=dx2;
dx2=dx1;
dx1=a;
end
else
title(['far from each other, iteration:' num2str(i)]);
end
pause(0.05)
end
I tried using this code for creating circles, but I don't know how to implement it in the previous code.
r=5;
x=-r:0.1:r;
y1=sqrt(r^2-x.^2);
%y2=-sqrt(r^2-x.^2);
y2=-y1;
figure(1), plot(x,y1,x,y2)
axis equal

Respuestas (1)

Cris LaPierre
Cris LaPierre el 11 de Nov. de 2018
The variables x1, x2, y1 and y2 are what create the box shapes. Replace the values with points that create a circle and run your code. It should work as well on the new vectors as it did on the old.
I tested it with the following:
r = 3;
theta = 0:10:360;
x1 = r*cosd(theta)-5;
x2 = r*cosd(theta)+5;
y1 = r*sind(theta);
y2 = y1;
dx1=0.4;
dx2=-0.05;
dy1=0.05;
dy2=0.2;
Lx=max(x1)-min(x1);
Ly=max(y1)-min(y1);
for i=1:1000
...
end
and got the following figure

Categorías

Más información sobre Graphics Object Programming en Help Center y File Exchange.

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by