如何在 MATLAB 中绘制杂乱的随机大小的圆圈?

How plot a messy random-size circles in MATLAB?

我打算在MATLAB R2014b中画下图:.

这个图形由许多不同(随机)颜色和随机大小的圆圈组成。

如何在 MATLAB R2014b 中绘制这样的图形?

不拼出代码:

  1. 选择初始圆圈,例如位置 [0,0] 和半径 1.
  2. 初始化位置和半径列表。
  3. 选择随机位置和半径 r
  4. 如果圆圈不是大圆圈(即sqrt(pos(1)^2+pos(2)^2) + r > 1)继续3.
  5. 如果与其他圆重叠(位置之间的距离 > 半径之和),继续 3
  6. 将圈子添加到列表,继续 3

更新:例子

好的,所以我只是想试试这个。我确定这不是最好的实现方式,但是:

% set number of circles to plot
n = 200;
radii = zeros(n, 1);
pos = zeros(n, 2);
allColours = lines(n);
% main loop
for idx = 1:n
    is_good = false;
    % generate random positions and radii until we have a hit
    while ~is_good
        pos(idx, :) = rand(1, 2)*2 - 1;
        radii(idx) = rand * (1 - max(radii));
        if ((sqrt(sum(pos(idx, :).^2)) + radii(idx) ) < 1) ... % ensure we're inside the big circle
                && ((idx == 1) || ... %  and either it's the first circle, or
                all(sqrt(sum((pos(1:(idx-1), :) - repmat(pos(idx, :), idx-1, 1)).^2, 2)) > radii(1:(idx-1))+radii(idx))) % all distances are bigger than sum of radii of existing circles
            is_good = true;
        end
    end
end
%% plot
figure(2);
clf;

hold on
set(gca, 'visible', 'off')
daspect([1, 1, 1])
rectangle(...
    'Position',[-1 -1 2 2],...
    'Curvature', [1 1],...
    'FaceColor', 'none',...
    'EdgeColor', [ 0, 0, 0]);
for idx = 1:n
    rectangle(...
        'Position',[pos(idx, 1) - radii(idx), pos(idx, 2) - radii(idx), 2*radii(idx), 2*radii(idx)],...
        'Curvature', [1 1],...
        'EdgeColor','none',...
        'FaceColor', allColours(idx,:));
end

大致思路如下。您需要修改它以确保选择的圆心和颜色适合您的特定目的。

% Define parameters
maxAxis = 100;
maxRadius = 10;
nCircles = 20;
% Random centres
xLoc = randi(maxAxis,nCircles);
yLoc = randi(maxAxis,nCircles);
% Random radii
radius = randi(maxRadius,nCircles);
% Random colours
allColours = rand(nCircles,3);
% Transform the data into position = [left bottom width height]
pos = [xLoc(:)-radius(:) yLoc(:)-radius(:) 2*radius(:)*[1 1]];
% Create and format the axes
ha = axes;
hold on;
axis equal;
box on;
set(ha,'XTickLabel',[],'YTickLabel',[]);
% Create the circles (must be done in loop)
for idx = 1:nCircles
    rectangle(...
        'Position',pos(idx,:),...
        'Curvature',[1 1],...
        'FaceColor',allColours(idx,:),...
        'EdgeColor','none');
end

>> doc retangle

了解更多信息。