How to initialize particles randomly in a circular structure?

I need to randomly initialize 256 particles inside a 2D circle.
something like this:

2 个评论

Particles are negatively charged and have a coulombic repulsion. So they will space out by themself.
The problem is, that is not random. It is quasi-uniform. But not remotely random.

请先登录,再进行评论。

 采纳的回答

That's not random. Do you want random, like raindrops on the ground? Or do you want them spaced apart within some distance range?
Try this, and adapt as needed:
% Demo to place points randomly, but with a minimum spacing from other points.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
%workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 20;
% Define how many points you want to place.
maxPoints = 500;
% Define counter for how many actually get placed.
numberOfPoints = 0;
% Define fail safe, how many iterations you want to keep trying before quitting.
maxIterations = 1000 * maxPoints;
loopCounter = 1;
% Define how close points can come to other points before being rejected.
minClosestDistance = 0.05;
maxRadius = 0.5;
% Declare arrays to hold the x and y coordinate values.
x = nan(1, numberOfPoints);
y = nan(1, numberOfPoints);
while numberOfPoints < maxPoints && loopCounter < maxIterations
% Get a random point.
xPossible = rand();
yPossible = rand();
% If more than specified radius away from center, then skip it.
radius = sqrt((xPossible-0.5)^2 + (yPossible-0.5)^2);
if radius > maxRadius
continue; % skip this point.
end
if numberOfPoints == 0
% First point automatically is valid.
numberOfPoints = numberOfPoints + 1;
x(numberOfPoints) = xPossible;
y(numberOfPoints) = yPossible;
continue;
end
% Find distances between this point and all others.
distances = sqrt((x-xPossible) .^ 2 + (y - yPossible) .^ 2);
if min(distances) >= minClosestDistance
% It's far enough away from all the other points, or it's the first point.
% Include it in our list of acceptable points.
numberOfPoints = numberOfPoints + 1;
x(numberOfPoints) = xPossible;
y(numberOfPoints) = yPossible;
end
% Increment the loop counter.
loopCounter = loopCounter + 1;
end
% Crop away points we were not able to get.
x = x(1:numberOfPoints); % Crop to valid points.
y = y(1:numberOfPoints); % Crop to valid points.
% Plot the points we were able to get.
plot(x, y, 'b.', 'MarkerSize', 15);
grid on;
axis equal;
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
caption = sprintf('Demo of Placing %d Random Points with Min Distance of %.2f', numberOfPoints, minClosestDistance);
title(caption, 'FontSize', fontSize);
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Tell user what we were able to achieve.
message = sprintf('Found %d points in %d tries', numberOfPoints, loopCounter)
message = 'Found 233 points in 500000 tries'
%helpdlg(message);

更多回答(1 个)

Depending on how you want the randomness to work, there would be different ways to do it. Here's a way that has the circle centered on the origin, and treats r (the distance of each point from the circle's center) and th (the angle that the vector from the circle's center to each point makes with the positive x-axis) as independent uniformly distributed random variables.
n = 256;
radius = 25;
r = radius*rand(1,n); % n uniformly distributed values between 0 and radius
th = 2*pi*rand(1,n); % n uniformly distributed values between 0 and 2*pi
x = r.*cos(th);
y = r.*sin(th);
figure();
plot(x,y,'*','MarkerSize',2);

类别

在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by