Azzera filtri
Azzera filtri

Creating several images of a random set of circles

3 visualizzazioni (ultimi 30 giorni)
Matlab_Novice
Matlab_Novice il 28 Nov 2017
Modificato: KSSV il 28 Nov 2017
I wrote the following code to create a set of images where each image is a random assortment of circles between 1-9. So, for example, the code I am pasting below specifies 25 images where 9 circles are distributed randomly in each of the 25 images.
N = 25;
for i=1:N
x = rand(1, 1000);
y = rand(1, 1000);
minAllowableDistance = 0.05;
numberOfPoints = 8;
% Initialize first point.
keeperX = x(1);
keeperY = y(1);
% Try dropping down more points.
counter = 2;
for k = 2 : numberOfPoints
% Get a trial point.
thisX = x(k);
thisY = y(k);
% See how far is is away from existing keeper points.
distances = sqrt((thisX-keeperX).^2 + (thisY - keeperY).^2);
minDistance = min(distances);
if minDistance >= minAllowableDistance
keeperX(counter) = thisX;
keeperY(counter) = thisY;
counter = counter + 1;
end
end
plot(keeperX, keeperY, 'bo', 'MarkerSize', 30, 'MarkerFaceColor', 'b');
grid on;
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
axis off;
figure=gca;
temp=['Qty', num2str(k),'__', num2str(i),'.png'];
saveas(gca,temp);
end
I need help with 2 problems I am having with this code:
  1. Item oneWhen I run this code, I don't always get 9 circles. Sometimes, I get 8 or 7. What am I missing or not specifying?
  2. Item twoThe minimum distance I've specified is 0.05 which allows me to get 9 circles most of the time. I've tried increasing the distance to 0.55 and higher to sometimes avoid getting two circles merging into each other but then I run into the problem of the circles being too far apart and not showing up in the frame.
I feel like both these problems could probably be solved by specifying a border limit for the frame in which the circle are displayed but I'm not sure how to go about doing that. Or maybe, there is something more problematic about the code I've written. Can you please help me so I can resolve the 2 issues mentioned above?
Mona

Risposte (1)

KSSV
KSSV il 28 Nov 2017
Modificato: KSSV il 28 Nov 2017
N = 25;
for i=1:N
coor = rand(4000,2) ;
minAllowableDistance = 0.05;
numberOfPoints = 8;
% Try dropping down more points.
data = repmat(coor(1,:),[length(coor),1])-coor ;
dist = sqrt(data(:,1).^2+data(:,2).^2);
idx = find(abs(dist-minAllowableDistance)<=10^-2) ;
idx = randsample(idx, numberOfPoints) ;
keeperX = coor([1; idx],1) ; keeperY = coor([1; idx],2) ;
plot(keeperX, keeperY, 'bo', 'MarkerSize', 30, 'MarkerFaceColor', 'b');
grid on;
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
axis off;
figure=gca;
temp=['Qty', num2str(i),'__', num2str(i),'.png'];
saveas(gca,temp);
end

Community Treasure Hunt

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

Start Hunting!

Translated by