基于MATLAB的二维圆形随机骨料生成程序

基于MATLAB的二维圆形随机骨料生成程序。这个程序利用随机数生成算法来创建随机分布的圆形骨料,同时确保骨料之间不重叠。你可以根据需要调整骨料的数量、大小范围和分布区域。

MATLAB程序代码

function generate_random_aggregates(numAggregates, minRadius, maxRadius, boxSize)
    % 参数说明:
    % numAggregates: 骨料的数量
    % minRadius: 骨料的最小半径
    % maxRadius: 骨料的最大半径
    % boxSize: 骨料分布的区域大小(正方形区域的边长)

    % 初始化骨料数组
    aggregates = [];

    % 循环生成骨料
    for i = 1:numAggregates
        % 生成随机半径
        radius = minRadius + (maxRadius - minRadius) * rand();
        
        % 生成随机中心位置
        while true
            % 随机生成骨料的中心位置
            centerX = radius + (boxSize - 2 * radius) * rand();
            centerY = radius + (boxSize - 2 * radius) * rand();
            
            % 检查是否与已有骨料重叠
            overlap = false;
            for j = 1:size(aggregates, 1)
                % 计算与已有骨料的距离
                distance = sqrt((centerX - aggregates(j, 1))^2 + (centerY - aggregates(j, 2))^2);
                % 如果距离小于两个骨料半径之和,则重叠
                if distance < radius + aggregates(j, 3)
                    overlap = true;
                    break;
                end
            end
            
            % 如果不重叠,则接受这个位置
            if ~overlap
                aggregates(i, :) = [centerX, centerY, radius];
                break;
            end
        end
    end

    % 绘制骨料分布
    figure;
    hold on;
    for i = 1:size(aggregates, 1)
        viscircles([aggregates(i, 1), aggregates(i, 2)], aggregates(i, 3), 'EdgeColor', 'b');
    end
    axis equal;
    xlim([0 boxSize]);
    ylim([0 boxSize]);
    title('Random Aggregates Distribution');
    hold off;
end

程序说明

  1. 参数输入

    • numAggregates:骨料的数量。
    • minRadius:骨料的最小半径。
    • maxRadius:骨料的最大半径。
    • boxSize:骨料分布的区域大小(正方形区域的边长)。
  2. 随机骨料生成

    • 使用rand()函数生成随机半径和随机中心位置。
    • 检查新生成的骨料是否与已有骨料重叠。如果重叠,则重新生成中心位置,直到找到不重叠的位置。
  3. 绘制结果

    • 使用viscircles函数绘制圆形骨料。
    • 设置坐标轴范围以显示整个分布区域。

使用示例

在MATLAB命令窗口中调用该函数,例如生成20个骨料,最小半径为5,最大半径为15,分布区域大小为100:

generate_random_aggregates(20, 5, 15, 100);

运行程序后,你将看到一个图形窗口,显示随机分布的圆形骨料。

posted @ 2025-05-28 13:16  令小飞  阅读(46)  评论(0)    收藏  举报