Circle packed with Circles

46 ビュー (過去 30 日間)
carlas
carlas 2011 年 12 月 23 日
コメント済み: Image Analyst 2020 年 1 月 17 日
Dear all,
I am wondering how to generate a circle filled with smaller circles? Do I have to calculate all the circle centers or is there a general way how do this numerically? I have the Image Processing Toolbox.
Kind regards,
Carlas Smith
  1 件のコメント
Chandra Kurniawan
Chandra Kurniawan 2011 年 12 月 23 日
You don't need image processing toolbox.

サインインしてコメントする。

採用された回答

Chandra Kurniawan
Chandra Kurniawan 2011 年 12 月 24 日
Here the code :
First, you need to create a m-file
function [X Y] = circle(center,radius,n)
THETA = linspace(0, 2 * pi, n);
RHO = ones(1, n) * radius;
[X Y] = pol2cart(THETA, RHO);
X = X + center(1);
Y = Y + center(2);
Save this code with filename 'circle.m'
Then create a new m-file
function circles(R);
axis([0 2*R 0 2*R]); axis off; grid off; hold on;
[xoc yoc] = circle([R R], R, 1000); % outer circle
plot(xoc, yoc, '-','linewidth',2,'color',0.5.*rand(1,3));
[xcc ycc] = circle([R R], 1, 1000); % center circle
plot(xcc, ycc, '-','linewidth',2,'color',0.5.*rand(1,3));
numlapis = ((2*R) - (R+1)) / 2;
for cnt1 = 1 : numlapis
lapis(cnt1) = cnt1 * 6;
[xcoor ycoor] = circle([R R], cnt1*2, lapis(cnt1)+1);
for cnt2 = 1 : lapis(cnt1)
[xc yc] = circle([xcoor(cnt2) ycoor(cnt2)], 1, 1000);
plot(xc, yc, '-','linewidth',2,'color',0.5.*rand(1,3));
end
end
Save this code with filename 'circles.m'
And about how to use it.
After you save the files, you can call it from command window.
Just type 'circles(any diameter)'
Eq : circles(5);
Note that this function works well with odd number.
Just try :)
  3 件のコメント
Sonal Gahlawat
Sonal Gahlawat 2020 年 1 月 16 日
Thank you for this code.
I tried using this code and it works great. However, when I change the radius of the inner circle, it doesn't work. Either the small circles become sparse or overlap if I decrease or increase the radius, respectively.
I would really appreciate any help.
Image Analyst
Image Analyst 2020 年 1 月 17 日
Show your code and error (or why it didn't work), a screenshot (if one was created), and say or show what you were expecting.

サインインしてコメントする。

その他の回答 (4 件)

Chandra Kurniawan
Chandra Kurniawan 2011 年 12 月 23 日
Hello,
Did you meant??
With diameter = 3
Or with diameter = 5
Or with diameter = 7
Or any diameter
  7 件のコメント
Pooja Kolati
Pooja Kolati 2016 年 2 月 21 日
編集済み: Pooja Kolati 2016 年 2 月 21 日
thank you! But it is still not helping me for packing even number of circles.
Image Analyst
Image Analyst 2016 年 2 月 21 日
Did you write any code using the FAQ at all?
You forgot to post it.

サインインしてコメントする。


Junaid
Junaid 2011 年 12 月 23 日
Dear Carlas,
save circle.m in your working directory. Then give input. In example below I will draw three circles.
circle([50,50],15,1000,':');hold on;
circle([50,50],10,1000,':');
circle([50,50],5,1000,':');
If I keep the center same and just change the radius of the circle, we can easily make circle inside the circles.
I hope I understand you well. let me know about this if worked.
  3 件のコメント
carlas
carlas 2011 年 12 月 23 日
Like:
http://wn.com/Packing_problem_Circles_in_circle and http://en.wikipedia.org/wiki/Packing_problem
Image Analyst
Image Analyst 2011 年 12 月 23 日
Interesting. Looks like the math might be tricky since some cases weren't solved (or "proved optimal") until the late 1960's. Have you tried to get the code from the Wolfram site? Would you be interested in a Monte Carlo solution to get a random configuration?

サインインしてコメントする。


Image Analyst
Image Analyst 2011 年 12 月 23 日
編集済み: Image Analyst 2020 年 1 月 17 日
Carlas: Someone asked this a year or two ago in the newsgroup. Here is the demo I made for him. It should be a simple matter to check for overlap before plopping down the next circle.
% M-file to place multiple small circles inside a big circle.
% Clean up
close all;
clc;
% Initialize some parameters.
numberOfSmallCircles = 25; % Number of small circles
smallCircleOutsideValue = 0.2;
smallCircleInsideValue = 0.8;
smallCircleRadius = 25; % small circle radius
bigImageWidth = 500;
bigImageHeight = 500; % square area 0f 500*500
bigCircleRadius = 250; % big circle radius
% Initialize an image to hold one single big circle.
bigCircleImage = zeros(bigImageHeight, bigImageWidth);
[x, y] = meshgrid(1:bigImageWidth, 1:bigImageHeight);
bigCircleImage((x - bigImageWidth/2).^2 + (y - bigImageHeight/2).^2 <= bigCircleRadius.^2) = 1;
% Display it in the upper left plot.
subplot(3,2,1);
imshow(bigCircleImage, []);
title('Big Circle Mask');
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
% Initialize an image to hold one single small circle.
smallCircleImage = zeros(2*smallCircleRadius, 2*smallCircleRadius);
[x, y] = meshgrid(1:smallCircleRadius*2, 1:smallCircleRadius*2);
singleCircleImage = zeros(2*smallCircleRadius, 2*smallCircleRadius);
singleCircleImage((x - smallCircleRadius).^2 + (y - smallCircleRadius).^2 <= smallCircleRadius.^2) = smallCircleInsideValue;
% Display it in the upper right plot.
subplot(3,2,2);
imshow(singleCircleImage, []);
title('Single Small Circle (scaled to fit)');
singleWidth = size(singleCircleImage, 2);
singleHeight = size(singleCircleImage, 1);
% Get random coordinates in the big image where
% we will place the upper left corner of the small circle.
widthThatWillFit = bigImageWidth - 2 * smallCircleRadius;
heightThatWillFit = bigImageHeight - 2 * smallCircleRadius;
smallUpperLeftX = widthThatWillFit * rand(numberOfSmallCircles, 1);
smallUpperLeftY = heightThatWillFit * rand(numberOfSmallCircles, 1);
% Initialize an output image to hold many small overlapping circles.
manySmallCircles = zeros(bigImageHeight, bigImageWidth);
% Place the small circles one by one.
for k = 1 : numberOfSmallCircles
% Find the square in the big image where we're going to add a small circle.
x1 = int16(smallUpperLeftX(k));
y1 = int16(smallUpperLeftY(k));
x2 = int16(x1 + singleWidth - 1);
y2 = int16(y1 + singleHeight - 1);
% Add in one small circle to the existing big image.
manySmallCircles(y1:y2, x1:x2) = manySmallCircles(y1:y2, x1:x2) + singleCircleImage;
end
% Make outside the circles the outside color.
manySmallCircles(manySmallCircles == 0) = smallCircleOutsideValue;
% Display it in the lower left plot.
subplot(3,2,3);
imshow(manySmallCircles);
title('Many Small Overlapping Circles');
% Multiply the big circle mask by the many small circles image to clip
% those small circles that lie outside the big circle.
maskedByBigCircle = bigCircleImage .* manySmallCircles;
% Display it in the lower right plot.
subplot(3,2,4);
imshow(maskedByBigCircle);
title('Many Small Circles Masked by Big Circle');
% Take the histogram and display it in the bottom row.
subplot(3,2,5);
hist(maskedByBigCircle(:));
grid on;
title('Histogram of image', 'FontSize', fontSize);
0000 Screenshot.png
  1 件のコメント
carlas
carlas 2011 年 12 月 24 日
This is interesting, but what I want to do is what Chandra plotted.

サインインしてコメントする。


MOHAMMED ALMUBAYEDH
MOHAMMED ALMUBAYEDH 2017 年 1 月 18 日
is there a way to input the small circle diameter then input the large circle diameter and then get the output of how many circles I can fit
  1 件のコメント
Image Analyst
Image Analyst 2017 年 1 月 18 日
As a partial answer, you can use patch() or fill() to place colored rectangles (with some opacity) over the image.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by