How to randomly plot circles on grid lines and determine coordinates of intersection?

5 Ansichten (letzte 30 Tage)
I'm trying to create my own vertical and horizontal grid lines and then plot multiple circles randomly over it. I then want to determine the points where the grid lines and circles intersect. The diameter of the circle should not exceed the length of one grid. All circles should be of equal size to each other. This is the code that I have at the moment (that I took from other sources) does not output what I want and is completely wrong. I don't fully understand the code so any help would be great! Thanks in advance.
x = 0:30;
y = 0:30;
figure(1)
plot(repmat(x,2,length(x)), [0 length(y)-1])
hold on
plot([0 length(x)-1], repmat(y,2,length(y)))
hold on
r=10;
center=[20,30];
t=-pi:0.001:pi;
x=r*cos(t)+center(1);
y=r*sin(t)+center(2);
plot(x,y)
hold off
axis equal

Akzeptierte Antwort

KSSV
KSSV am 17 Mai 2017
N = 20 ;
x = linspace(0,30,N) ;
y = linspace(0,30,N) ;
[X,Y] = meshgrid(x,y) ;
figure
hold on
plot(X,Y,'r') ;
plot(Y,X,'r')
%%draw circles
nc = 1 ;
R = 2. ;
th = linspace(0,2*pi) ;
iwant = cell(nc,1) ;
for i = 1:nc
% get random centre of circle
cx0 = (max(x)-min(x)).*rand + min(x);
cy0 = (max(y)-min(y)).*rand + min(y);
% circle coordinates
cx = cx0+R*cos(th) ;
cy = cy0+R*sin(th) ;
plot(cx,cy,'k')
% get intersection
P = InterX([cx ;cy],[X(:)' ; Y(:)']) ;
iwant{i} = P ;
plot(P(1,:),P(2,:),'.b')
end
  3 Kommentare
KSSV
KSSV am 18 Mai 2017
N = 20 ;
x = linspace(0,30,N) ;
y = linspace(0,30,N) ;
[X,Y] = meshgrid(x,y) ;
figure
hold on
plot(X,Y,'r') ;
plot(Y,X,'r')
%%draw circles
nc = 10 ;
R = 3. ;
th = linspace(0,2*pi) ;
iwant = cell(nc,1) ;
for i = 1:nc
% get random centre of circle
cx0 = (max(x)-min(x)).*rand + min(x);
cy0 = (max(y)-min(y)).*rand + min(y);
% circle coordinates
cx = cx0+R*cos(th) ;
cy = cy0+R*sin(th) ;
plot(cx,cy,'k')
% get intersection
Q = cell(1,[]) ;
count = 0 ;
% loop along lines parallel to x-axes
for j = 1:N
P = InterX([cx ;cy],[X(j,:) ; Y(j,:)]) ;
if ~isempty(P)
count = count+1 ;
Q{count} = P ;
end
end
% loop along lines parallel to y-axes
for k = 1:N
P = InterX([cx ;cy],[X(:,k)' ; Y(:,k)']) ;
if ~isempty(P)
count = count+1 ;
Q{count} = P ;
end
end
Q = cell2mat(Q) ;
plot(Q(1,:),Q(2,:),'.b')
iwant{i} = Q ;
end
Adam Podbielski
Adam Podbielski am 21 Feb. 2024
how can this be incorporated with a circle in polar coordinates?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Polar Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by