generate uniformly distributed points inside a hexagon

am trying to generate uniformly distributed points inside a hexagon, but am stuck can any one help???

 Accepted Answer

%In the code, I will create a hexagon centered at (0,0) with radius R. The snipplets can be used in mobile capacity predicts and general systems level simulation of cellular networks.
N = 400; %Number of users
R = 10; %Radius of Hexagon
%Define the vertexes of the hexagon. They for angles 0, 60, 120, 180, 240 and 300 withe origin.
%Vertexes
v_x = R * cos((0:6)*pi/3);
v_y = R * sin((0:6)*pi/3);
%The method used here is to generate many points in a square and choose N points that fall within the hexagon
%Generate 3N random points with square that is 2R by 2R
c_x = R-rand(1, 3*N)*2*R;
c_y = R-rand(1, 3*N)*2*R;
%There is a command in MATLAB inploygon.
%The command finds points within a polygon region.
%get the points within the polygon
IN = inpolygon(c_x, c_y, v_x, v_y);
%drop nodes outside the hexagon
c_x = c_x(IN);
c_y = c_y(IN);
%choose only N points
idx = randperm(length(c_x));
c_x = c_x(idx(1:N));
c_y = c_y(idx(1:N));
plot(c_x, c_y, 'r*');
hold on;
plot(v_x,v_y);
axis square;
hold off
[EDITED, code formatted, Jan]

7 Comments

How have you executed this code, because when I try to run it, it shows me an error at the idx?? It doesn't execute. Which are the outputs of this code?
@Jenny: This answer has been posted 3 years ago. This was the only contribution of the author. The code is not formatted correctly, so if you just copy&paste it, it is expected to crash. If you have done some further cleaning of the code, please post the version you use. If it produces an error message, post a copy of the complete message, such that the readers can reconsider, what you are doing and observing. Thanks.
The code worked successfully after being formatted by @Jan.
After running the code, it gave the following output as shown in the following image;
What changes should be done for a hexagon with a specific centre (other than the origin) please? Thanks in advance.
@Mystery Mystery: Use the original code and add the offset afterwards.
Note: The code gives uniformly randomly distributed points, NOT uniformly distributed (like a grid or hexagonal honeycomb array) like the question asked for, where no randomness was mentioned. There IS a difference.
Thank you peeps! but im getting ''index exceeds matrix dimensions'' for the line c_x= c_x(idx(1:N));

Sign in to comment.

More Answers (1)

Here is a generating method without rejection
R = 3;
centerx = 3;
centery = 7;
n = 10000;
% Generate uniform points in the simplex
% convex combination of 3points in R^3: (1,0,0) (0,1,0) (0,0,1)
m = 3;
X = rand(m-1,n) .^ (1./(m-1:-1:1)'); % use bsxfun(@power,...) for old release
X = cumprod([ones(1,n);X]).*[ones(m,n)-[X;zeros(1,n)]];
% use X as a barycentric of the triangle (0,1,z6) in the complex plane
% so point Z is uniform in this triangle
z6 = exp(2i*pi/6);
Z = [0, 1, z6]*X;
% multiply by random 6th-roots of 1 to map into unit hexagonal
Z = Z .* (z6.^floor(6*rand(1,n)));
% shift and scale
x = centerx+R*real(Z);
y = centery+R*imag(Z);
plot(x,y,'.')
axis equal

6 Comments

Thank you so much sir, could you please add a few comments in the code so that i get a clear view of the method? thanks again.
Hi Bruno, I want to store the coordinates of these points in a two column csv file. The first column will have the x-coordinate while the second will have the y-coordinate. I tried with putting these instructions right before plot(x,y,'.'):
a = [x y] dlmwrite('file.csv',a,'-append','delimiter',',','newline', 'pc')
but the generated csv file has all the points stored in the fashion (x1, x2, x3, ..., xn, y1, y2, y3, ..., yn).
Could you please help!
a = [x(:),y(:)]
before write it.
Thank you so much Bruno!
hi bruno
if I want to make the points move randomly and continuously in this hexagonal what should i do?

Sign in to comment.

Categories

Find more on Random Number Generation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!