How to randomly plot circles on grid lines and determine coordinates of intersection?
조회 수: 3 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
KSSV
2017년 5월 17일
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
download InterX from the link: https://in.mathworks.com/matlabcentral/fileexchange/22441-curve-intersections?focused=5165138&tab=function
댓글 수: 3
KSSV
2017년 5월 18일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Polar Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!