How to randomly plot circles on grid lines and determine coordinates of intersection?
5 ビュー (過去 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!