MATLAB: Generate uniformly distributed points inside a hexagon

hexagonrandom number generator

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

Best 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]
Related Question