圈内的包装圈

时间:2014-05-12 19:29:52

标签: matlab geometry packing nonlinear-optimization

给定单位圆,以及一组半径为r的M个较小圆。找到较小圆的最大半径,使其全部适合单位圆内而不重叠。

我在多边形示例link

中打包了以下圆圈

我想改变所有圆都在多边形内的方程式

theta = 2*pi/N;       % Angle covered by each side of the polygon

phi = theta*(0:N-1)'; % Direction of the normal to the side of the polygon

polyEq = ( [cos(phi) sin(phi)]*x <= cdist-r );

表示所有圆圈都在圆圈内的方程式,但我不知道如何。有人可以帮帮我吗?

亲切的问候。

1 个答案:

答案 0 :(得分:0)

在你的情况下,没有“多边形的边”,因此没有theta的模拟,你需要改变引用的每个地方theta,以及所有引用theta的变量{1}}(例如phi)。

以下内容应该有效。我刚刚从您的链接中复制粘贴了代码,删除了thetaphi,重新定义了cdistpolyEq,并使其在回答而不是多边形。请询问是否有任何这些选择不清楚。

M = 19; % number of circles to fit

toms r              % radius of circles
x = tom('x', 2, M); % coordinates of circle centers

clear pi % 3.1415...

cdist = 1;          % radius of unit circle

%%%%%% equations saying all circles are inside of unit circle
polyEq = (sqrt(x(1,:).^2 + x(2,:).^2) + r <= cdist);

% create a set of equations that say that no circles overlap
circEq = cell(M-1,1);
for i=1:M-1
    circEq{i} = ( sqrt(sum((x(:,i+1:end)-repmat(x(:,i),1,M-i)).^2)) >= 2*r );
end

% starting guess
x0 = { r == 0.5*sqrt(1/M), x == 0.3*randn(size(x)) };

% solve the problem, maximizing r
options = struct;
% try multiple starting guesses and choose best result
options.solver = 'multimin';
options.xInit = 30; % number of different starting guesses
solution = ezsolve(-r,{polyEq,circEq},x0,options);

% plot result
x_vals = (0:100)./100;
plot(sqrt(1 - x_vals.^2),'-') % top of unit circle
plot(-1.*sqrt(1 - x_vals.^2),'-') % bottom of unit circle
axis image
hold on
alpha = linspace(0,2*pi,100);
cx = solution.r*cos(alpha);
cy = solution.r*sin(alpha);
for i=1:M
    plot(solution.x(1,i)+cx,solution.x(2,i)+cy) % circle number i
end
hold off
title(['Maximum radius = ' num2str(solution.r)]);