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

circlecoordinatesgridgrid linesintersectrandom

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

Best Answer

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